var a,b,t,i:longint;
begin
assign(input, 'rook.in');
reset(input);
readln(t);
for i:=1 to t do begin;
read (a,b);
if (a=1) and (b=1) then writeln (0);
if (a=1) or (b=1) then writeln (1);
if (a<>1) and (b<>1) then writeln (2);
readln;
end;
end.