import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int d = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < d; i++) { int n1 = sc.nextInt(); int n2 = sc.nextInt(); int m = sc.nextInt(); int[] customers = new int[m]; for (int j = 0; j < m; j++) { customers[j] = sc.nextInt(); } Arrays.sort(customers); if (customers[0] > n1 && customers[0] > n2) { sb.append(0).append("\n"); continue; } ArrayList>> dp = new ArrayList<>(); for (int j = 0; j < m; j++) { dp.add(new HashMap<>()); } int left = 0; int right = m; while (right - left > 1) { int x = (left + right) / 2; if (dfw(x, n1, n2, customers, dp)) { left = x; } else { right = x; } } sb.append(right).append("\n"); } System.out.print(sb); } static boolean dfw(int idx, int small, int large, int[] customers, ArrayList>> dp) { if (small < 0 || large < 0) { return false; } if (idx < 0) { return true; } if (small > large) { return dfw(idx, large, small, customers, dp); } if (dp.get(idx).containsKey(small)) { if (dp.get(idx).get(small).containsKey(large)) { return dp.get(idx).get(small).get(large); } } else { dp.get(idx).put(small, new HashMap<>()); } boolean next = (dfw(idx - 1, small - customers[idx], large, customers, dp) || dfw(idx - 1, small, large - customers[idx], customers, dp)) ; dp.get(idx).get(small).put(large, next); return next; } }