import java.util.Scanner; import java.util.HashMap; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0){ int N = sc.nextInt(); int M = sc.nextInt(); int ans1 = (N+3)/4; M -= N%4==0?0:4-N%4; M -= ans1*4; System.out.println(ans1+Math.max(0,(M+7)/8)); } } }