using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var t = NN; var ans = new int[t]; for (var u = 0; u < t; ++u) { var c = NList; var (n, m) = (c[0], c[1]); var edge = (n + 3) / 4; var center = edge * 4 + (4 - n % 4) % 4; if (center >= m) ans[u] = edge; else ans[u] = edge + (m - center + 7) / 8; } WriteLine(string.Join("\n", ans)); } }