program ROOK;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n,m,i,t:integer;
begin
assignfile(input,'ROOK.IN');
reset(input);
readln(t);
for i:=1 to t do begin
read(n,m);
if (n=m)and (n<>1) then writeln('2');
if (n=1) and (m=1) then writeln('0');
if n<>m then writeln('1');
end;
  close(input);
end.
