#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define int long long
const int N = 5e4 + 10;
int n, k, a[N], b[35], c[35], d[35];
bool check(int mid) {
for(int i = 0; i <= 25; i ++ ) c[i] = 0;
for(int i = 0; i <= 25; i ++ ) d[i] = b[i];
priority_queue<int> q;
for(int i = 1; i <= n ; i ++ ) {
int x = mid * a[i];
q.push(x);
}
for(int i = 19; ~i ; i -- ) {
if(q.size() && q.top() <= (1 << i) && d[i]) {
q.pop();
d[i] -- ;
}
while(q.size() && q.top() >= (1 << i) && d[i]) {
int t = q.top();
int tt = t / (1 << i);
int ttt = min(tt, d[i]);
tt -= ttt, d[i] -= ttt;
q.pop();
if(t - ttt * (1 << i) == 0) continue;
q.push(t - ttt * (1 << i));
}
while(q.size() && q.top() <= (1 << i) && d[i]) {
q.pop();
d[i] -- ;
}
}
int j = 19;
while(q.size()) {
if(j <= 0) break;
while(j >= 0 && !d[j]) j -- ;
while(q.size() && j >= 0 && (1 << j) > q.top() && d[j]) {
if(j < 0) break;
d[j] -- ;
q.pop();
}
j -- ;
}
if(q.size()) return false;
return true;
}
void solve() {
cin >> n >> k;
memset(b, 0, sizeof b);
int per = 0, sum = 0;
for(int i = 1; i <= n ; i ++ ) cin >> a[i], per += a[i];
for(int i = 0; i < k ; i ++ ) cin >> b[i], sum += (1 << i) * b[i];
int l = 0, r = sum / per;
while(l < r) {
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
cout << l << endl;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int T = 1;
cin >> T;
while(T -- ) solve();
return 0;
}