from collections import * from itertools import * from functools import cache, partial from pprint import pprint import sys from typing import Any, Final try: from icecream import ic except ImportError: # Graceful fallback if IceCream isn't installed. ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # noqa debug = partial(print, file=sys.stderr) dpprint = partial(pprint, stream=sys.stderr) sys.setrecursionlimit(10**6) MOD = 998244353 T: Final = int(input()) def f(N: int, M: int) -> int: s = (N + 3) // 4 if s * 8 - N >= M: return s return (N + M + 7) // 8 # x = (M - s * 8) // 2 # return (x + 7) // 8 + s for _ in range(T): N, M = map(int, input().split()) print(f(N, M))