import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); for (int z = 0; z < t; z++) { String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); int v1 = (n + 3) / 4; int v2 = 4 - n % 4; if (v2 == 4) { v2 = 0; } int m2 = m - v2; int v3 = v1 * 4; int m3 = m2 - v3; if (m3 <= 0) { pw.println(v1); } else { pw.println(v1 + (m3 + 7) / 8); } } pw.flush(); br.close(); } }