King's Quest

No attempts yetTime limit1sMemory limit128 MB

Problem

Once upon a time there lived a king who had NN sons. In his kingdom there were also NN beautiful girls, and the king knew, for each of his sons, which of those girls he liked. The sons were young and light-headed, so a single son could like several girls.

The king asked his wizard to pick, for each son, one girl that he likes so that the son could marry her — and the wizard did it. Each son was assigned a girl he likes, and of course each girl may marry only one son.

Looking at the list, the king said: “I like the list, but I am not fully satisfied. For each son I want to know every girl he could marry. Naturally, after he marries any one of them, it must still be possible to assign every other son a different girl he likes.”

Formally, for each son ii you must report all girls gg such that ii likes gg and there exists a perfect matching (each son paired with a distinct girl he likes) in which son ii is matched to girl gg. Solve this problem to save the wizard's head.

Input

The first line contains NN — the number of the king's sons (1N20001 \le N \le 2000).

Each of the next NN lines describes the girls one son likes: first KiK_i — the number of girls — followed by KiK_i distinct integers between 11 and NN denoting those girls. The sum of all KiK_i does not exceed 200000200\,000.

The last line contains the wizard's original list: NN distinct integers, where the ii-th number is the girl that son ii marries in that list. The list is guaranteed to be valid, i.e. every son likes the girl assigned to him.

Output

Output NN lines. For son ii, first print LiL_i — the number of distinct girls he likes and could marry so that every other son can still be matched to a girl he likes. Then, on the same line, print those girls' numbers in ascending order.

(The original statement allowed printing the girls in any order; to make the answer unique for automatic grading, print them in ascending order.)