#pragma GCC optimize ("O3") #pragma GCC target ("avx") // yukicoder // #pragma GCC target ("sse4") // SPOJ, codechef #include #include #include #include #include #include #include #include #include #include #include #include #define _rep(_1, _2, _3, _4, name, ...) name #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, b) rep4(i, a, b, 1) #define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c)) #define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__) using namespace std; using i8 = signed char; using i64 = long long; using u8 = unsigned char; using u32 = unsigned; using u64 = unsigned long long; using f80 = long double; int A[100]; i8 dp[100010]; void solve() { int T; scanf("%d", &T); rep(_, T) { int n1, n2; scanf("%d %d", &n1, &n2); int m; scanf("%d", &m); rep(i, m) scanf("%d", &A[i]); sort(A, A + m); if (n1 > n2) swap(n1, n2); const i8 inf = 100; fill(dp, dp + n1 + 1, inf); dp[0] = 0; int s = 0, ans = 0; rep(t, m) { int v = A[t]; s += v; if (s > n1 + n2) break; for (int i = min(n1, s); i >= v; --i) { dp[i] = min(dp[i], i8(dp[i - v] + 1)); } for (int i = min(n1, s); ; --i) { if (dp[i] < inf) { ans += (s - i) <= n2; break; } } } printf("%d\n", ans); } } int main() { clock_t beg = clock(); solve(); clock_t end = clock(); fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC); return 0; }