Program star(input,output);
Const
maxn=100000;
Var
n,i,j,k,l:longint;
a:array[1..maxn]of string;
b:array[1..maxn]of longint;
s:string;
Procedure qsort(l,r:longint);
var
i,j,t:longint;
s:string;
begin
if l<r then
begin
i:=l;
j:=r;
t:=b[i];
s:=a[i];
while i<j do
begin
while (i<j)and(t>b[j])do dec(j);
if i<j then
begin
b[i]:=b[j];
a[i]:=a[j];
inc(i);
end;
while (i<j)and(t<b[i])do inc(i);
if i<j then
begin
b[j]:=b[i];
a[j]:=a[i];
dec(j);
end;
end;
a[i]:=s;
b[i]:=t;
qsort(l,i-1);
qsort(i+1,r);
end;
end;
Begin
assign(input,'star.in');
assign(output,'star.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(a[i]);
b[i]:=0;
end;
readln(k);
for i:=1 to k do
begin
readln(s);
readln(l);
for j:=1 to n do
if s=a[j] then
begin
b[j]:=b[j]+l;
break;
end;
end;
qsort(1,n);
for i:=1 to n do
begin
writeln(a[i]);
writeln(b[i]);
end;
close(input);
close(output);
End.