#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char name[90],place[90];
int num;
}c[105];
bool cmp(node x,node y)
{
if(strcmp(x.place,y.place)<0) return true;
if(strcmp(x.place,y.place)==0&&strcmp(x.name,y.name)<0) return true;
return false;
}
int main()
{
int test,n;
scanf("%d",&test);
while(test--)
{
memset(&c,0,sizeof(&c));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s %s %d",c[i].name,c[i].place,&c[i].num);
sort(c,c+n,cmp);
for(int i=0;i<n;i++){
if(strcmp(c[i].place,c[i+1].place)==0)
{
if(strcmp(c[i].name,c[i+1].name)==0)
c[i+1].num+=c[i].num,c[i].num=0;
}
else
{
printf("%s
",c[i].place);
for(int j=0;j<n;j++)
{
if(strcmp(c[i].place,c[j].place)==0&&c[j].num)
printf(" |----%s(%d)
",c[j].name,c[j].num);
}
}
}
if(test!=0)
printf("
");
}
return 0;
}