t=int(input()) for _ in range(t): n,m=map(int,input().split()) v=n//4 m-=6*v m-=8-n%4 ans=v if 0