program gpaths;
type mas=array[1..50,1..50] of integer;
     mas1=array[1..50] of integer;
var a,c:mas;
    k,n:integer;
    b:mas1;
procedure create(var a:mas;var b:mas1);
var i,j:integer;
begin
read(n,k);
for i:=1 to n-1 do
    for j:=1 to k do read(a[i,j]);
for i:=1 to k do readln(b[i]);
end;
{function pyt(v:integer):integer;
var
begin
for i:=1 to n do begin
    for j:= 1 to k do begin}
procedure nameri(var c:mas);
var i,t:integer;
begin
for i:=1 to n do begin
                 t:=1;
                 if i<>b[t] then c[i,1]:=t
                            else begin
                                 repeat
                                 t:=t+1;
                                 until i<>b[t];
                                 c[i,1]:=t;
                                 end;
                 writeln(c[i,1],' ',c[i,2]);
                 end;
                 {c[i,2]:=pyt(1,i);    }
end;
BEGIN
create(a,b);
nameri(c);
readln;
end.