#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;
struct Team
{
string name;
int team_rank;
int total_point;
int game_played;
int wins;
int ties;
int losses;
int goal_difference;
int goal_scored;
int goal_against;
Team():name(""),total_point(0),game_played(0),wins(0),ties(0),losses(0),goal_difference(0),goal_scored(0),goal_against(0){}
};
bool cmp(const Team &a,const Team &b)
{
if(a.total_point!=b.total_point)re`turn a.total_point>b.total_point;
else if(a.wins!=b.wins)return a.wins>b.wins;
else if(a.goal_difference!=b.goal_difference)return a.goal_difference>b.goal_difference;
else if(a.goal_scored!=b.goal_scored)return a.goal_scored>b.goal_scored;
else if(a.game_played!=b.game_played)return a.game_played<b.game_played;
else
{
string sa(a.name),sb(b.name);
for(int i=0;i<sa.size();i++)sa[i]=tolower(sa[i]);
for(int j=0;j<sb.size();j++)sb[j]=tolower(sb[j]);
return sa<sb;
}
}
int main()
{
int num,i,j,k;
cin>>num;
cin.ignore();
while(num--)
{
int m,n;
Team team[35];
string game_name;
getline(cin,game_name);
cin>>m;
cin.ignore();
for(i=0;i<m;i++)
getline(cin,team[i].name);
cin>>n;
cin.ignore();
string temp;
for(i=0;i<n;i++)
{
getline(cin,temp);
string s1,s2,a,b;
int flag1,flag2,flag3,to_a,to_b;
j=0;
for(int p=0;p<temp.length();p++)
{
if(temp[p]=='@')
{
flag2=p;
break;
}
}
while(true)
{
if(temp[j]=='#'&&(j<flag2))flag1=j;
if(temp[j]=='#'&&(j>flag2))
{
flag3=j;
break;
}
j++;
}
s1=temp.substr(0,flag1);
s2=temp.substr(flag3+1,temp.length()-flag3);
a=temp.substr(flag1+1,flag2-flag1-1);
b=temp.substr(flag2+1,flag3-flag2-1);
if(a.length()==1)
to_a=a[0]-'0';
else
to_a=(a[0]-'0')*10 +(a[1]-'0');
if(b.length()==1)
to_b=b[0]-'0';
else
to_b=(b[0]-'0')*10 +(b[1]-'0');
for(k=0;k<m;k++)
{
if(team[k].name==s1)
{
team[k].game_played++;
team[k].goal_scored+=to_a;
team[k].goal_against+=to_b;
if(to_a>to_b)
{
team[k].wins++;
team[k].total_point+=3;
}
else if(to_a==to_b)
{
team[k].ties++;
team[k].total_point+=1;
}
else
{
team[k].losses++;
}
}
if(team[k].name==s2)
{
team[k].game_played++;
team[k].goal_scored+=to_b;
team[k].goal_against+=to_a;
if(to_b>to_a)
{
team[k].wins++;
team[k].total_point+=3;
}
else if(to_a==to_b)
{
team[k].ties++;
team[k].total_point+=1;
}
else
{
team[k].losses++;
}
}
}
}
for(i=0;i<m;i++)
team[i].goal_difference=team[i].goal_scored-team[i].goal_against;
sort(team,team+m,cmp);
cout<<game_name<<endl;
for(i=0;i<m;i++)
cout<<i+1<<") "<<team[i].name<<" "
<<team[i].total_point<<"p, "<<
team[i].game_played<<"g ("<<
team[i].wins<<"-"<<team[i].ties<<
"-"<<team[i].losses<<"), "<<
team[i].goal_difference<<"gd ("<<
team[i].goal_scored<<"-"<<
team[i].goal_against<<")"<<endl;
if(num!=0)cout<<endl;
}
return 0;
}