program trie;
type arr=record
col:longint;
next:array['A'..'z']of longint;
end;
var f:array[1..100000]of arr;
i,l,n,now,k,x,all,j,m:longint;
s:string;
name:array[1..100000]of string;
nice:array[1..100000]of longint;
procedure qsort(l,r:longint);
var mid,i,j,t:longint;
ss:string;
begin
i:=l;
j:=r;
mid:=nice[(l+r) div 2];
repeat
while nice[i]>mid do i:=i+1;
while nice[j]<mid do j:=j-1;
if i<=j then begin
t:=nice[i];
nice[i]:=nice[j];
nice[j]:=t;
ss:=name[i];
name[i]:=name[j];
name[j]:=ss;
i:=i+1;
j:=j-1;
end;
until i>=j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
begin
assign(input,'star.in');
assign(output,'star.out');
reset(input);
rewrite(output);
readln(n);
k:=1;
all:=0;
for i:=1 to n do begin
readln(s);
all:=all+1;
name[all]:=s;
l:=length(s);
now:=1;
for j:=1 to l do if f[now].next[s[j]]<>0 then now:=f[now].next[s[j]]
else begin
k:=k+1;
f[now].next[s[j]]:=k;
now:=k;
end;
end;
readln(m);
for j:=1 to m do begin
readln(s);
readln(x);
l:=length(s);
now:=1;
for i:=1 to l do now:=f[now].next[s[i]];
f[now].col:=f[now].col+x;
end;
for i:=1 to all do begin
l:=length(name[i]);
now:=1;
for j:=1 to l do now:=f[now].next[name[i][j]];
nice[i]:=f[now].col;
end;
qsort(1,all);
for i:=1 to all do begin
writeln(name[i]);
writeln(nice[i]);
end;
close(input);
close(output);
end.