T = int(input()) for _ in range(T): N,M = map(int,input().split()) x = (N+3)//4 if N+M <= 8*x: print(x) else: rem = N+M-8*x print(x+(rem+7)//8)