program scanword;
var
tree:array [1..10000] of record
left,right:integer;
s:string[100];
p:longint;
end;
tmp:string;
top,i,n,m:integer;
page:longint;
procedure find(root:integer);
begin
if root<>0 then begin
if tree[root].s=tmp then writeln(tree[root].p)
else begin
if tmp<tree[root].s then find(tree[root].left);
if tmp>tree[root].s then find(tree[root].right);
end;
end;
end;
procedure build(root:integer);
begin
if tmp<tree[root].s then begin
if tree[root].left=0 then begin
inc(top);
tree[root].left:=top;
tree[top].s:=tmp;
tree[top].p:=page;
end
else build (tree[root].left);
end
else begin
if tree[root].right=0 then begin
inc(top);
tree[root].right:=top;
tree[top].s:=tmp;
tree[top].p:=page;
end
else build(tree[root].right);
end;
end;
begin
assign (input,'scanword.in');
reset (input);
assign (output,'scanword.out');
rewrite (output);
readln (n);
for i:=1 to n do begin
tree[i].left:=0;
tree[i].right:=0;
tree[i].s:='';
tree[i].p:=0;
end;
top:=1;
readln (tree[1].s);
readln (tree[1].p);
for i:=2 to n do begin
readln (tmp);
readln (page);
build(1);
end;
readln (m);
for i:=1 to m do begin
readln (tmp);
find(1);
end;
close (input);
close (output);
end.