import sys import math from itertools import product, permutations, combinations, accumulate from heapq import heapify, heappush, heappop from collections import deque, defaultdict, Counter from bisect import bisect, bisect_left, bisect_right from copy import copy, deepcopy #from sortedcontainers import SortedSet, SortedList, SortedDict sys.setrecursionlimit(10**7) INF = float('inf') LINF = 1 << 60 MOD = 10**9+7 def mii(): return map(int, sys.stdin.readline().split()) T = int(input()) test = [] for i in range(T): test.append(list(mii())) for i in range(T): N, M = test[i] n = (N + 3) // 4 p = (4 - (N % 4)) % 4 if M <= 4*n + p: print(n) else: print(n + (M - 4*n - p + 7) // 8)