import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
class Traveling {
private static final int INPUT_LEN = 2 + 2 + 10 * (4 + 2 + 5000 * (50 + 1 + 50 + 1 + 5 + 2));
int T
= Integer.
parseInt(br.
readLine()); Map
<String, String
> fromMap
= new HashMap
<String, String
>(); Map
<String, String
> fromPriceMap
= new HashMap
<String, String
>(); Set<String> toMap = new HashSet<String>();
StringBuilder buff = new StringBuilder();
while (T-- > 0) {
int N
= Integer.
parseInt(br.
readLine()); long sum = 0;
{ // solve test case
fromMap.clear();
toMap.clear();
while (--N > 0) {
String[] parts
= br.
readLine().
split(" "); fromMap.put(parts[0], parts[1]);
fromPriceMap.put(parts[0], parts[2]);
toMap.add(parts[1]);
sum += parse(parts[2]);
}
// get first
Set<String> firstSet = new HashSet<String>(fromMap.keySet());
firstSet.removeAll(toMap);
String from
= firstSet.
iterator().
next(); // iterate and print
buff.setLength(0);
String next
= fromMap.
get(from
); while (next != null) {
buff.append(from).append(' ').append(next).append(' ').append(fromPriceMap.get(from)).append('\n');
from = next;
next = fromMap.get(from);
}
buff.append(sum).append("$\n");
System.
out.
println(buff.
toString()); }
}
}
private static long parse
(String price
) { return Integer.
parseInt(price.
substring(0, price.
length() - 1)); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklucHV0U3RyZWFtUmVhZGVyOwppbXBvcnQgamF2YS51dGlsLkhhc2hNYXA7CmltcG9ydCBqYXZhLnV0aWwuSGFzaFNldDsKaW1wb3J0IGphdmEudXRpbC5NYXA7CmltcG9ydCBqYXZhLnV0aWwuU2V0OwoKY2xhc3MgVHJhdmVsaW5nIHsKCiAgICBwcml2YXRlIHN0YXRpYyBmaW5hbCBpbnQgSU5QVVRfTEVOID0gMiArIDIgKyAxMCAqICg0ICsgMiArIDUwMDAgKiAoNTAgKyAxICsgNTAgKyAxICsgNSArIDIpKTsKCglw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