import java.io.*; import java.util.*; class tourist_jm { public static void main(String[] ss) throws Exception { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(stdin.readLine()); while (N-->0) { StringTokenizer st = new StringTokenizer(stdin.readLine()); int w = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); int map[][]=new int[h][w]; int m[][][]=new int[2][w][w]; for(int y=0;y0 || y>0) && (x==0 || map[y][x-1]==2) && (y==0 || map[y-1][x]==2)) map[y][x]=2; } } m[0][0][0]=map[0][0]==1?1:0; for(int step=1;step=w || y1>=h || map[y1][x1]==2) continue; if (x2<0 || y2<0 || x2>=w || y2>=h || map[y2][x2]==2) continue; for(int dx1=0;dx1<2;dx1++) for(int dx2=0;dx2<2;dx2++) { if (x1-dx1<0 || y1-1+dx1<0 || map[y1-1+dx1][x1-dx1]==2) continue; if (x2-dx2<0 || y2-1+dx2<0 || map[y2-1+dx2][x2-dx2]==2) continue; int v=m[(step-1)%2][x1-dx1][x2-dx2]; if (v>best) best=v; } int v=(map[y1][x1]==1?1:0)+(map[y2][x2]==1?1:0); if (v==2 && x1==x2) v=1; m[step%2][x1][x2]=best+v; } System.out.println(m[(w+h-2)%2][w-1][w-1]); } } }