Match Maker

No attempts yetTime limit1sMemory limit128 MB

Problem

You run the matchmaking agency ACM (Amazing Coupling Marriage), and your job is to pair men and women into happy couples.

NN men and NN women want to get married as soon as possible. Every man ranks all NN women from most to least preferred, and every woman ranks all NN men the same way. The most preferred person is first in the list, the next is second, and so on. The table below shows one possible set of preference lists for 4 men and 4 women.

Preference lists for four men and four women

Your task is to marry every man to a woman (a one-to-one pairing) so that the result is stable. A matching is unstable if there exist a man and a woman who are not married to each other, yet each of them prefers the other over their current partner. Such a pair would rather leave their partners for each other, so the marriage is fragile. A matching with no such pair is stable.

For example, the matching where man 1 marries woman 3, man 2 marries woman 1, man 3 marries woman 4, and man 4 marries woman 2 is unstable, because man 1 prefers woman 1 over woman 3 and woman 1 prefers man 1 over man 2.

For a given set of preference lists there are usually several stable matchings. Among them, output the man-optimal one: the unique stable matching in which every man gets the best partner (highest on his own preference list) that he could have in any stable matching. This is exactly the matching produced by the men-proposing Gale-Shapley procedure, where free men propose down their lists and each woman keeps her most preferred proposer so far.

Input

The first line contains the number of test cases TT.

Each test case begins with a line containing an integer NN with 1N<1001 \le N < 100: there are NN men and NN women, all numbered from 11 to NN.

The next NN lines give the men's preferences. Line ii contains a permutation of 1..N1..N: the preference list of man ii over the women, from most to least preferred (he prefers woman XX to woman YY if XX comes before YY).

The following NN lines give the women's preferences in the same format: line jj is the preference list of woman jj over the men.

Output

For each test case, print exactly one line describing the man-optimal stable matching.

The line lists the partners' woman ids ordered by increasing man id: the first number is the woman married to man 11, the second number is the woman married to man 22, and in general the ii-th number is the woman married to man ii. Separate the numbers with single spaces.