#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 (n == 1 && m == 1)
			printf("0\n");
		else if((n ==1 || m==1) || n%2 != m%2)
			printf("1\n");
		else
			printf("2\n");*/
		if(p==0) printf("0\n");
		else if(p%2 || (m==1 || n==1))printf("1\n");
		else printf("2\n");		
	}
}