Alchemy

Given starting substances and reactions that each require holding a whole set of substances to produce another set, find everything Josko can eventually obtain.

Medium7GraphBFSHash mapImplementationNo attempts yetTime limit1sMemory limit64 MB

Problem

Back when alchemists were still searching for gold, the world knew NN different substances, numbered 11 through NN. Years of work gave the alchemists a list of alchemical reactions. One reaction turns the substance set {X1,X2,,XL}\{X_1, X_2, \dots, X_L\} into another substance set {Y1,Y2,,YR}\{Y_1, Y_2, \dots, Y_R\}. For example, the set {1,4,5}\{1, 4, 5\} reacts once and produces the new set {2,6}\{2, 6\}.

Josko is a modern alchemist and holds MM different substances A1,A2,,AMA_1, A_2, \dots, A_M. He has an unlimited amount of each of them. He can run a reaction only while he holds every substance on its left side, and running it gives him every substance on its right side. A reaction consumes nothing, and a substance he has produced can be used in later reactions.

Find every substance Josko can obtain from the list of ancient reactions.

Input

The first line contains the integers NN and MM (1MN1000001 \le M \le N \le 100000).

The second line contains MM integers AiA_i (1AiN1 \le A_i \le N), the labels of the substances Josko has at the start.

The third line contains the integer KK (1K1000001 \le K \le 100000), the number of known reactions.

The following 3K3K lines contain the list of reactions. Each reaction is described with three lines.

  • The first line contains the integers LL and RR (1L,RN1 \le L, R \le N).
  • The second line contains LL distinct integers XiX_i (1XiN1 \le X_i \le N).
  • The third line contains RR distinct integers YiY_i (1YiN1 \le Y_i \le N).

This reaction turns the substance set {X1,X2,,XL}\{X_1, X_2, \dots, X_L\} into the substance set {Y1,Y2,,YR}\{Y_1, Y_2, \dots, Y_R\}.

The sum of all values of LL does not exceed 100000100000. The sum of all values of RR does not exceed 100000100000.

Output

The first line must contain the integer XX, the number of obtainable substances.

The second line must contain the XX labels BiB_i of those substances, sorted in ascending order and separated by single spaces. The substances Josko starts with count as obtainable.

Notes

The first example has two reactions. The first one turns the set {1,2}\{1, 2\} into {3}\{3\}, and the second one turns {1,3}\{1, 3\} into {4}\{4\}. Josko starts with {1,2}\{1, 2\}, so the first reaction gives him substance 33 and leaves him with {1,2,3}\{1, 2, 3\}. The second reaction then gives him substance 44 as well.

In the second example Josko starts with {1,4,5}\{1, 4, 5\}. The second reaction gives him substance 66, and the third reaction then gives him substance 22. The first reaction can never be used because he never has substance 33.