for _ in range(int(input())): N,M=map(int,input().split()) row=(N+3)//4;rem=N%4 if rem>0:M-=4-rem M-=4*row;M=max(M,0) if M>0:row+=(M+7)//8 print(row)