program star;
var
a:array[1..100000]of longint;
s:array[1..100000]of string;
t:string;
i,j,k,m,n,max,x,y:longint;
procedure qsort(l,r:longint);
var i,j,x,t:longint; e:string;
begin
i:=l;
j:=r;
x:=a[(l+r)div 2];
repeat
while a[i]>x do inc(i);
while a[j]<x do dec(j);
if not(i>j) then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
e:=s[i];
s[i]:=s[j];
s[j]:=e;
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if r>i then qsort(i,r);
end;
begin
assign(input,'star.in');reset(input);assign(output,'star.out');rewrite(output);
readln(n);
for i:=1 to n do
readln(s[i]);
readln(m);
for i:=1 to m do
begin
readln(t);
readln(x);
for j:=1 to n do
if t=s[j] then a[j]:=a[j]+x;
end;
qsort(1,n);
for i:=1 to n do
begin
writeln(s[i]);
writeln(a[i]);
end;
close(input);close(output);
end.