#include <iostream>

using namespace std;

long int t, n, m, ii;

int main(){
	freopen("ROOK.IN","r",stdin);
	cin >> t;
	for (ii=0; ii<t; ii++){
		cin >>n>>m ;
		if ((n==1)&&(m==1)){ cout<<0<<"\n";continue;}
		if((n==1)||(m==1)){cout<<1<<"\n";continue;}
		if(n%2==0){cout<<1<<"\n";continue;}
		if(n%2!=0){cout<<2<<"\n";continue;}
	}
	return 0;
}
