#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
int main()
{
	map<string, map<string, int>> M;
	int n, m, x;
	cin >> n;
	string s1, s2;
	while (n--)
	{
		M.clear();
		cin >> m;
		while (m--)
		{
			cin >> s1 >> s2 >> x;
			M[s2][s1] += x;
		}
		for (auto &x : M)
		{
			cout << x.first << endl;
			for (auto &y : x.second)
				cout << "   |----" << y.first << '(' << y.second << ')' << endl;
		}
		if (n)
			cout << endl;
	}
	return 0;
}