// Arup Guha
// 5/18/2018
// Solution to 2018 Code Jam Round 1C Problem B: Lollipop Shop

import java.util.*;

public class Solution {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		boolean bad = false;

		for (int loop=1; loop<=nC; loop++) {

			int n = stdin.nextInt();
			int[] freq = new int[n];
			boolean[] sold = new boolean[n];

			// Get all n peoples' choices.
			for (int i=0; i<n; i++) {

				// Log the valid choices and frequency of choices so far.
				int d = stdin.nextInt();
				int[] choices = new int[d];
				for (int j=0; j<d; j++) {
					choices[j] = stdin.nextInt();
					freq[choices[j]]++;
				}

				// They don't want anything.
				if (d == 0) {
					System.out.println("-1");
					System.out.flush();
				}

				else {

					// Here are the ones we can sell.
					ArrayList<Integer> canSell = new ArrayList<Integer>();
					for (int j=0; j<d; j++)
						if (!sold[choices[j]])
							canSell.add(choices[j]);

					// Oops, we ran out of all of theirs.
					if (canSell.size() == 0) {
						System.out.println("-1");
						System.out.flush();
					}

					// Definitely sell here.
					else {

						// Sell the one that's been asked for least...
						int minVal = 1000000000, sell = -1;
						for (Integer x: canSell) {
							if (sell == -1 || freq[x] < minVal) {
								sell = x;
								minVal = freq[x];
							}
						}
						System.out.println(sell);
						System.out.flush();
						sold[sell] = true;
					}

				}

			}
		}

	}

}