var
a,n,k,l,i,j:longint;
q:string;
p:array[0..100000]of longint;
m,c:array[0..100000]of string;
begin
assign(input,'star.in');
reset(input);
assign(output,'star.out');
rewrite(output);
readln(a);
for i:=1 to a do readln(m[i]);
readln(k);
for i:=1 to k do begin
readln(c[i]); readln(l);
for j:=1 to a do
if c[i]=m[j] then begin
p[j]:=p[j]+l;
end; end;
for i:=1 to a do
for j:=i+1 to a do
if p[i]<p[j] then begin
k:=p[i]; p[i]:=p[j]; p[j]:=k;
q:=m[i]; m[i]:=m[j]; m[j]:=q;
end;
for i:=1 to a do
begin
writeln(m[i]);
writeln(p[i]);
end;
close(input);
close(output);
end.