t=int(input()) for _ in range(t): n,m=map(int,input().split()) y=-(-n//4) s=y*8 if n+m<=s: print(y) else: r=n+m-s print(y-(-r//8))