#include <stdio.h>
int main()
{
	unsigned long t, n, m, i, p;
	freopen("rook.in", "r", stdin);
	scanf("%d", &t);
	for (i = 0; i < t; i++)
	{
		scanf("%d%d", &n, &m);
		p = n-1 + m-1;
		if(p==0) printf("0\n");
		else if(p%2 || (m==1 || n==1) || m!=n)printf("1\n");
		else printf("2\n");		
	}
}