// Arup Guha
// 8/31/2013
// Solution to 2013 UCF Locals Problem: Airways

import java.util.*;
import java.io.*;

public class airways {

	public static int[] ans;
	public static int numEdges;

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("airways.in"));
		int numCases = stdin.nextInt();

		// Get cases.
		for (int loop=0; loop<numCases; loop++) {

			// Read in graph.
			numEdges = stdin.nextInt();
			Edge[] eList = new Edge[numEdges];
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			int ID = 0;
			int[] degree = new int[2*numEdges];

			for (int i=0; i<numEdges; i++) {

				// Read in the edge data.
				String a = stdin.next();
				String b = stdin.next();
				int num = stdin.nextInt();
				eList[i] = new Edge(a,b,num);

				// Put these in our hash map, if necessary.
				if (!map.containsKey(a))
					map.put(a, ID++);
				if (!map.containsKey(b))
					map.put(b, ID++);

				// Update the degree of this vertex.
				degree[map.get(a)]++;
			}

			// Allocate space for edge list.
			GraphEdge[][] graph = new GraphEdge[ID][];
			for (int i=0; i<ID; i++)
				graph[i] = new GraphEdge[degree[i]];

			// Form edge list representation.
			int[] curIndex = new int[ID];
			int[] curDegrees = new int[ID];
			for (int i=0; i<numEdges; i++) {
				int from = map.get(eList[i].start);
				int to = map.get(eList[i].end);
				graph[from][curIndex[from]++] = new GraphEdge(to, eList[i].number);
				curDegrees[to]++;
			}

			ans = new int[numEdges];
			topSort(graph, curDegrees);

			// Output result.
			for (int i=0; i<ans.length-1; i++)
				System.out.print(ans[i]+" ");
			System.out.println(ans[ans.length-1]);
		}

		stdin.close();
	}

	public static void topSort(GraphEdge[][] graph, int[] curDegrees) {

		// Set up valid edges from which to build.
		PriorityQueue<GraphEdge> flightList = new PriorityQueue<GraphEdge>();
		for (int i=0; i<curDegrees.length; i++) {
			if (curDegrees[i] == 0) {
				for (int j=0; j<graph[i].length; j++)
					flightList.offer(graph[i][j]);
			}
		}

		// Iteratively place flights.
		for (int i=0; i<numEdges; i++) {
			GraphEdge place = flightList.poll();
			ans[i] = place.number;

			int next = place.dest;

			// Update degrees based on using this edge.
			curDegrees[next]--;

			// Add to our heap, if necessary...
			if (curDegrees[next] == 0) {
				for (int j=0; j<graph[next].length; j++)
					flightList.offer(graph[next][j]);
			}
		}
	}
}

class Edge {

	public String start;
	public String end;
	public int number;

	public Edge(String a, String b, int n) {
		start = a;
		end = b;
		number = n;
	}
}

class GraphEdge implements Comparable<GraphEdge> {

	public int dest;
	public int number;

	public GraphEdge(int where, int flight) {
		dest = where;
		number = flight;
	}

	public int compareTo(GraphEdge other) {
		return this.number - other.number;
	}
}