#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = 3007;
int n , m , a[maxN] , b[maxN] , c[maxN];
vector<pair<ii , ii>> tmp;
int t[maxN];
vector<int> g[maxN];
void prepare(){
for (int i = 1 ; i < maxN ; i++){
for (int x = i ; x < maxN ; x += x&-x) g[i].push_back(x);
}
}
void update(int x , int y){
for (; x < maxN ; x += x&-x) t[x] = max(t[x] , y);
}
int get(int x){
int ans = 0;
for (; x >= 1 ; x -= x&-x) ans = max(ans , t[x]);
return ans;
}
void solve(){
cin >> n >> m;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
for (int i = 1 ; i <= m ; i++) cin >> b[i];
sort(a + 1 , a + n + 1);
sort(b + 1 , b + m + 1);
for (int i = 1 ; i <= n ; i++){
for (int j = 1 ; j <= m ; j++){
int x = __gcd(a[i] , b[j]);
ii it = {a[i] / x , b[j] / x};
tmp.push_back({it , {i , j}});
}
}
sort(all(tmp));
int l = 0 , r = -1 , ans = 0;
while (l < sz(tmp)){
while (r + 1 < sz(tmp) && tmp[l].fi == tmp[r + 1].fi) r++;
int L = l , R = l - 1;
while (L <= r){
while (R + 1 <= r && tmp[L].se.fi == tmp[R + 1].se.fi) R++;
for (int i = R ; i >= L ; i--){
int x = tmp[i].se.se;
int y = get(x - 1) + 1;
update(x , y);
ans = max(ans , y);
}
L = R + 1;
}
for (int i = l ; i <= r ; i++){
int x = tmp[i].se.se;
for (int y : g[x]) t[y] = 0;
}
l = r + 1;
}
cout << n + m - ans << "\n";
tmp.clear();
}
#define name "G"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
prepare();
int t = 1; cin >> t;
while (t--) solve();
return 0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Pj4gYVtpXTsKICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBtIDsgaSsrKSBjaW4gPj4gYltpXTsKICAgIHNvcnQoYSArIDEgLCBhICsgbiArIDEpOwogICAgc29ydChiICsgMSAsIGIgKyBtICsgMSk7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkrKyl7CiAgICAgICAgZm9yIChpbnQgaiA9IDEgOyBqIDw9IG0gOyBqKyspewogICAgICAgICAgICBpbnQgeCA9IF9fZ2NkKGFbaV0gLCBiW2pdKTsKICAgICAgICAgICAgaWkgaXQgPSB7YVtpXSAvIHggLCBiW2pdIC8geH07CiAgICAgICAgICAgIHRtcC5wdXNoX2JhY2soe2l0ICwge2kgLCBqfX0pOwogICAgICAgIH0KICAgIH0KICAgIHNvcnQoYWxsKHRtcCkpOwogICAgaW50IGwgPSAwICwgciA9IC0xICwgYW5zID0gMDsKICAgIHdoaWxlIChsIDwgc3oodG1wKSl7CiAgICAgICAgd2hpbGUgKHIgKyAxIDwgc3oodG1wKSAmJiB0bXBbbF0uZmkgPT0gdG1wW3IgKyAxXS5maSkgcisrOwogICAgICAgIGludCBMID0gbCAsIFIgPSBsIC0gMTsKICAgICAgICB3aGlsZSAoTCA8PSByKXsKICAgICAgICAgICAgd2hpbGUgKFIgKyAxIDw9IHIgJiYgdG1wW0xdLnNlLmZpID09IHRtcFtSICsgMV0uc2UuZmkpIFIrKzsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IFIgOyBpID49IEwgOyBpLS0pewogICAgICAgICAgICAgICAgaW50IHggPSB0bXBbaV0uc2Uuc2U7CiAgICAgICAgICAgICAgICBpbnQgeSA9IGdldCh4IC0gMSkgKyAxOwogICAgICAgICAgICAgICAgdXBkYXRlKHggLCB5KTsKICAgICAgICAgICAgICAgIGFucyA9IG1heChhbnMgLCB5KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBMID0gUiArIDE7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSBsIDsgaSA8PSByIDsgaSsrKXsKICAgICAgICAgICAgaW50IHggPSB0bXBbaV0uc2Uuc2U7CiAgICAgICAgICAgIGZvciAoaW50IHkgOiBnW3hdKSB0W3ldID0gMDsKICAgICAgICB9CiAgICAgICAgbCA9IHIgKyAxOwogICAgfQogICAgY291dCA8PCBuICsgbSAtIGFucyA8PCAiXG4iOwogICAgdG1wLmNsZWFyKCk7Cn0KCiNkZWZpbmUgbmFtZSAiRyIKCmludCBtYWluKCl7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGlmIChmb3BlbihuYW1lIi5JTlAiICwgInIiKSl7CiAgICAgICAgZnJlb3BlbihuYW1lIi5JTlAiICwgInIiICwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4obmFtZSIuT1VUIiAsICJ3IiAsIHN0ZG91dCk7CiAgICB9CiAgICBwcmVwYXJlKCk7CiAgICBpbnQgdCA9IDE7IGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cgo=