import math for _ in [0]*int(input()): n, m = map(int, input().split(" ")) t_ans, mod = math.ceil(n/4), n%4 m -= (t_ans * 4) if mod > 0: m -= (4 - mod) m_ans = math.ceil(m/8) if m_ans > 0: t_ans += m_ans print(t_ans)