import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class Main
{
public static class city
{
city next;
int val;
{
c1 = a;
val = v;
next = null;
}
}
static int T,tot,x,cost,ans;
static HashMap
<String,city
> map
= new HashMap
<String,city
>(); static HashMap
<String,Integer
> head
= new HashMap
<String,Integer
>(); static List<String> sol = new ArrayList<String>();
{
T
= Integer.
parseInt(br.
readLine());
while(T-- >0)
{
tot
= Integer.
parseInt(br.
readLine()); tot--;
cost = ans = 0;
if(tot
==0) System.
out.
println("0$"); else
{
for(x=0;x<tot;x++)
{
city1 = t.nextToken();
city2 = t.nextToken();
ans += cost;
map.put(city1, new city(city1,-1));
if(map.containsKey(city2)) map.get(city2).val = cost;
else map.put(city2, new city(city2,cost));
map.get(city1).next = map.get(city2);
if(!head.containsKey(city1)) head.put(city1, 1);
head.put(city2, 2);
}
Iterator it
= head.
entrySet().
iterator(); while (it.hasNext())
{
{
start
= (String) (pairs.
getKey()); break;
}
}
for(x=0;x<tot;x++)
{
if(map.get(start).next!=null)
{
sol.add(start+" "+map.get(start).next.c1+" "+map.get(start).next.val+"$");
start = map.get(start).next.c1;
}
}
sol.add(ans+"$");
}
for(x
=0;x
<sol.
size();x
++) System.
out.
println(sol.
get(x
)); }
}}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklPRXhjZXB0aW9uOwppbXBvcnQgamF2YS5pby5JbnB1dFN0cmVhbVJlYWRlcjsKaW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuSGFzaE1hcDsKaW1wb3J0IGphdmEudXRpbC5JdGVyYXRvcjsKaW1wb3J0IGphdmEudXRpbC5MaXN0OwppbXBvcnQgamF2YS51dGlsLk1hcDsKaW1wb3J0IGphdmEudXRpbC5TdHJpbmdUb2tlbml6ZX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