//satyaki3794
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2*(n))
#define RIGHT(n) (2*(n)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
ll pwr(ll base, ll p, ll mod = MOD){
ll ans = 1;while(p){if(p&1)ans=(ans*base)%mod;base=(base*base)%mod;p/=2;}return ans;
}
ll gcd(ll a, ll b){
if(b == 0) return a;
return gcd(b, a%b);
}
int sieve[1000*1000+2], last[2][1000*1000+2];
ll reduced_form[1000*1000+2], complement[1000*1000+2];
ll cnt[2][1000*1000+2];
set<int> distinct;
int main(){
ios_base::sync_with_stdio(0);
for(int i=1;i<=1000*1000;i++)
reduced_form[i] = complement[i] = 1;
sieve[1] = 1;
int x = 1000*1000;
for(int i=2;i<=x;i++)
if(!sieve[i]){
for(int j=i;j<=x;j+=i){
sieve[j] = 1;
int cnt = 0, val = j;
while(val % i == 0){
val /= i;
cnt++;
}
cnt %= 3;
// if(j == 6) cout<<"got "<<cnt<<" for i = "<<i<<endl;
if(cnt != 0){
// reduced_form[j].pb({i, cnt});
// complement[j].pb({i, 3-cnt});
reduced_form[j] *= pwr(i, cnt, (ll)1e15);
complement[j] *= pwr(i, 3-cnt, (ll)1e15);
}
}
}
// cout<<"reduced_form: ";for(int i=1;i<=20;i++) cout<<i<<":"<<reduced_form[i]<<" ";cout<<endl;
// cout<<"complement: ";for(int i=1;i<=20;i++) cout<<i<<":"<<complement[i]<<" ";cout<<endl;
int T;
cin>>T;
// T=1;
for(int t=1;t<=T;t++){
distinct.clear();
int n1, n2;
cin>>n1>>n2;
while(n1--){
int a;
cin>>a;
int r = reduced_form[a];
if(last[0][r] != t){
last[0][r] = t;
cnt[0][r] = 0;
}
cnt[0][r]++;
distinct.insert(r);
}
while(n2--){
int a;
cin>>a;
int r = reduced_form[a];
if(last[1][r] != t){
last[1][r] = t;
cnt[1][r] = 0;
}
cnt[1][r]++;
distinct.insert(r);
}
// cout<<"distinct: ";for(auto it : distinct) cout<<it<<" ";cout<<endl;
ll ans = -1;
for(auto it : distinct){
if(last[0][it]==t && complement[it]<=1000*1000 && last[1][complement[it]]==t)
ans = max(ans, cnt[0][it]*cnt[0][it] + cnt[1][complement[it]]*cnt[1][complement[it]]);
if(last[1][it]==t && complement[it]<=1000*1000 && last[0][complement[it]]==t)
ans = max(ans, cnt[1][it]*cnt[1][it] + cnt[0][complement[it]]*cnt[0][complement[it]]);
}
cout<<ans<<endl;
}
return 0;
}
Ly9zYXR5YWtpMzc5NAojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIE1PRCAoMTAwMDAwMDAwN0xMKQojZGVmaW5lIExFRlQobikgKDIqKG4pKQojZGVmaW5lIFJJR0hUKG4pICgyKihuKSsxKQogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyB1bGw7CnR5cGVkZWYgcGFpcjxpbnQsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxpbnQsIGlpPiBpaWk7CiAKbGwgcHdyKGxsIGJhc2UsIGxsIHAsIGxsIG1vZCA9IE1PRCl7CmxsIGFucyA9IDE7d2hpbGUocCl7aWYocCYxKWFucz0oYW5zKmJhc2UpJW1vZDtiYXNlPShiYXNlKmJhc2UpJW1vZDtwLz0yO31yZXR1cm4gYW5zOwp9CiAKbGwgZ2NkKGxsIGEsIGxsIGIpewogICAgaWYoYiA9PSAwKSAgcmV0dXJuIGE7CiAgICByZXR1cm4gZ2NkKGIsIGElYik7Cn0KCgppbnQgc2lldmVbMTAwMCoxMDAwKzJdLCBsYXN0WzJdWzEwMDAqMTAwMCsyXTsKbGwgcmVkdWNlZF9mb3JtWzEwMDAqMTAwMCsyXSwgY29tcGxlbWVudFsxMDAwKjEwMDArMl07CmxsIGNudFsyXVsxMDAwKjEwMDArMl07CnNldDxpbnQ+IGRpc3RpbmN0OwoKCmludCBtYWluKCl7CgogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKCiAgICBmb3IoaW50IGk9MTtpPD0xMDAwKjEwMDA7aSsrKQogICAgICAgIHJlZHVjZWRfZm9ybVtpXSA9IGNvbXBsZW1lbnRbaV0gPSAxOwoKICAgIHNpZXZlWzFdID0gMTsKICAgIGludCB4ID0gMTAwMCoxMDAwOwogICAgZm9yKGludCBpPTI7aTw9eDtpKyspCiAgICAgICAgaWYoIXNpZXZlW2ldKXsKICAgICAgICAgICAgZm9yKGludCBqPWk7ajw9eDtqKz1pKXsKICAgICAgICAgICAgICAgIHNpZXZlW2pdID0gMTsKICAgICAgICAgICAgICAgIGludCBjbnQgPSAwLCB2YWwgPSBqOwogICAgICAgICAgICAgICAgd2hpbGUodmFsICUgaSA9PSAwKXsKICAgICAgICAgICAgICAgICAgICB2YWwgLz0gaTsKICAgICAgICAgICAgICAgICAgICBjbnQrKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGNudCAlPSAzOwogICAgICAgIC8vIGlmKGogPT0gNikgIGNvdXQ8PCJnb3QgIjw8Y250PDwiIGZvciBpID0gIjw8aTw8ZW5kbDsKICAgICAgICAgICAgICAgIGlmKGNudCAhPSAwKXsKICAgICAgICAgICAgICAgICAgICAvLyByZWR1Y2VkX2Zvcm1bal0ucGIoe2ksIGNudH0pOwogICAgICAgICAgICAgICAgICAgIC8vIGNvbXBsZW1lbnRbal0ucGIoe2ksIDMtY250fSk7CiAgICAgICAgICAgICAgICAgICAgcmVkdWNlZF9mb3JtW2pdICo9IHB3cihpLCBjbnQsIChsbCkxZTE1KTsKICAgICAgICAgICAgICAgICAgICBjb21wbGVtZW50W2pdICo9IHB3cihpLCAzLWNudCwgKGxsKTFlMTUpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKLy8gY291dDw8InJlZHVjZWRfZm9ybTogIjtmb3IoaW50IGk9MTtpPD0yMDtpKyspICAgY291dDw8aTw8IjoiPDxyZWR1Y2VkX2Zvcm1baV08PCIgIjtjb3V0PDxlbmRsOwovLyBjb3V0PDwiY29tcGxlbWVudDogIjtmb3IoaW50IGk9MTtpPD0yMDtpKyspICAgY291dDw8aTw8IjoiPDxjb21wbGVtZW50W2ldPDwiICI7Y291dDw8ZW5kbDsKCiAgICBpbnQgVDsKICAgIGNpbj4+VDsKICAgIC8vIFQ9MTsKICAgIGZvcihpbnQgdD0xO3Q8PVQ7dCsrKXsKCiAgICAgICAgZGlzdGluY3QuY2xlYXIoKTsKICAgICAgIAogICAgICAgIGludCBuMSwgbjI7CiAgICAgICAgY2luPj5uMT4+bjI7CgogICAgICAgIHdoaWxlKG4xLS0pewogICAgICAgICAgICBpbnQgYTsKICAgICAgICAgICAgY2luPj5hOwogICAgICAgICAgICBpbnQgciA9IHJlZHVjZWRfZm9ybVthXTsKICAgICAgICAgICAgaWYobGFzdFswXVtyXSAhPSB0KXsKICAgICAgICAgICAgICAgIGxhc3RbMF1bcl0gPSB0OwogICAgICAgICAgICAgICAgY250WzBdW3JdID0gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICBjbnRbMF1bcl0rKzsKICAgICAgICAgICAgZGlzdGluY3QuaW5zZXJ0KHIpOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUobjItLSl7CiAgICAgICAgICAgIGludCBhOwogICAgICAgICAgICBjaW4+PmE7CiAgICAgICAgICAgIGludCByID0gcmVkdWNlZF9mb3JtW2FdOwogICAgICAgICAgICBpZihsYXN0WzFdW3JdICE9IHQpewogICAgICAgICAgICAgICAgbGFzdFsxXVtyXSA9IHQ7CiAgICAgICAgICAgICAgICBjbnRbMV1bcl0gPSAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNudFsxXVtyXSsrOwogICAgICAgICAgICBkaXN0aW5jdC5pbnNlcnQocik7CiAgICAgICAgfQoKCi8vIGNvdXQ8PCJkaXN0aW5jdDogIjtmb3IoYXV0byBpdCA6IGRpc3RpbmN0KSAgY291dDw8aXQ8PCIgIjtjb3V0PDxlbmRsOwoKICAgICAgICBsbCBhbnMgPSAtMTsKICAgICAgICBmb3IoYXV0byBpdCA6IGRpc3RpbmN0KXsKCiAgICAgICAgICAgIGlmKGxhc3RbMF1baXRdPT10ICYmIGNvbXBsZW1lbnRbaXRdPD0xMDAwKjEwMDAgJiYgbGFzdFsxXVtjb21wbGVtZW50W2l0XV09PXQpCiAgICAgICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCBjbnRbMF1baXRdKmNudFswXVtpdF0gKyBjbnRbMV1bY29tcGxlbWVudFtpdF1dKmNudFsxXVtjb21wbGVtZW50W2l0XV0pOwoKICAgICAgICAgICAgaWYobGFzdFsxXVtpdF09PXQgJiYgY29tcGxlbWVudFtpdF08PTEwMDAqMTAwMCAmJiBsYXN0WzBdW2NvbXBsZW1lbnRbaXRdXT09dCkKICAgICAgICAgICAgICAgIGFucyA9IG1heChhbnMsIGNudFsxXVtpdF0qY250WzFdW2l0XSArIGNudFswXVtjb21wbGVtZW50W2l0XV0qY250WzBdW2NvbXBsZW1lbnRbaXRdXSk7CiAgICAgICAgfQoKICAgICAgICBjb3V0PDxhbnM8PGVuZGw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0KCgoKCg==