import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.NoSuchElementException; public class TwoBamboo { /* メモ化用Map */ private static int[][] calculatedArray; public static void main(String[] args) { FastScanner cin = new FastScanner(System.in); int d = cin.nextInt(); int[][][] allQuestions = new int[d][2][]; for (int i = 0; i < d; i++) { /* 2本の竹情報 */ allQuestions[i][0] = new int[2]; allQuestions[i][0][0] = cin.nextInt(); allQuestions[i][0][1] = cin.nextInt(); /* 要求数 */ int requestsNum = cin.nextInt(); /* 要求情報 */ allQuestions[i][1] = new int[requestsNum]; for (int j = 0; j < requestsNum; j++) { allQuestions[i][1][j] = cin.nextInt(); } } cin.close(); for (int[][] question : allQuestions) { System.out.println(answer(question[0], question[1])); } } /* 各データセット単位で解く */ private static int answer(int[] bamboo, int[] requests) { Arrays.sort(requests); Arrays.sort(bamboo); calculatedArray = new int[requests.length][bamboo[0] + 1]; return getNum(0, bamboo[0], bamboo[1], requests); } private static int getNum(int currentIndex, int shortBamboo, int longBamboo, int[] requests) { /* 要求されるのは長さのみで重みはないので、要求を小さい順にソートしたとき、R(n)の要求に応えずにR(n+1)の要求に応えたほうがよい、というケースはない。なので、必ず昇順ソートされた要求に対し、どちらかの竹から切リ出して次の要求を処理する */ if (calculatedArray[currentIndex][shortBamboo] != 0) { /* 計算済み */ return calculatedArray[currentIndex][shortBamboo]; } int shortResult = 0; if (requests[currentIndex] <= shortBamboo) { shortResult++; if (currentIndex + 1 < requests.length) { shortResult += getNum(currentIndex + 1, shortBamboo - requests[currentIndex], longBamboo, requests); } } int longResult = 0; if (requests[currentIndex] <= longBamboo) { longResult++; if (currentIndex + 1 < requests.length) { longResult += getNum(currentIndex + 1, shortBamboo, longBamboo - requests[currentIndex], requests); } } int result = shortResult > longResult ? shortResult : longResult; calculatedArray[currentIndex][shortBamboo] = result; return result; } static class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in) { this.in = in; } public void close() { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } }