#include <cstdio>
#include <algorithm>
#include <map>
#include <cctype>
using namespace std;
#define MAXN 100010
#define int long long
map<int, int> sws;
int a[MAXN], p[MAXN], d[MAXN], sw[MAXN], n, m, t, li[MAXN], rk[MAXN], rk2[MAXN], bak, p2[MAXN], li2[MAXN];
int gcd(int a, int b) {return b ? gcd(b, a % b): a;}
int lcm(int a, int b) {a /= gcd(a, b); return a * b;}
inline int mul(int a, int b, int p) {
int res = 0;
while (b) {
if (b & 1) (res += a) %= p;
(a <<= 1) %= p;
b >>= 1;
}
return res;
}
void exgcd(int a, int b, int & x, int & y) {
if (b) {exgcd(b, a % b, y, x); y -= a / b * x;}
else {x = 1; y = 0;}
}
int inv(int a, int p) {int x, y; exgcd(a, p, x, y); return (x + p) % p;}
inline bool cmp(int a, int b) {
return p[a] < p[b];
}
#define N 100000000
char bufin[N], *cur;
inline int getint() {
register char ch;
while(!isdigit(ch=*cur++));
register int x=ch^'0';
while(isdigit(ch=*cur++)) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
signed main() {
fread(bufin, 1, N, stdin);
cur = bufin;
int T; T = getint();
while (T--) {
sws.clear(); bak = 0;
n = getint(); m = getint();
bool all1 = true;
for (int i = 1; i <= n; ++i) a[i] = getint();
for (int i = 1; i <= n; ++i) all1 &= (p[i] = getint()) == 1;
for (int i = 1; i <= n; ++i) d[i] = getint();
for (int i = 1; i <= m; ++i) ++sws[getint()];
map<int, int>::iterator it;
int mincnt = 0, LCM = 1;
for (int i = 1; i <= n; ++i) {
if (sws.begin() -> first > a[i]) it = sws.begin();
else it = sws.upper_bound(a[i]), --it;
sw[i] = it -> first;
if (it -> second == 1) sws.erase(it);
else --it->second;
++sws[d[i]];
mincnt = max(mincnt, a[i] / sw[i] + (a[i] % sw[i] != 0));
int G = gcd (p[i], sw[i]), I = inv(sw[i] / G, p[i] / G);
if (a[i] % G != 0) {
puts("-1");
goto finallive;
} else a[i] /= G;
p[i] /= G; a[i] = mul(a[i], I, p[i]);;
li[i] = a[i] % p[i];
LCM = lcm(LCM, p[i]);
rk[i] = i;
}
if (all1) printf("%d\n", mincnt);
else if (n == 1) printf("%d\n", mincnt + (li[1] - mincnt % p[1] + p[1]) % p[1]);
else {
int fff = 0;
sort(rk + 1, rk + 1 + n, cmp);
rk2[++bak] = rk[1];
for (int i = 2; i <= n; ++i)
if (p[rk[i]] == p[rk[i - 1]]) {
if (li[rk[i]] != li[rk[i - 1]]) {
puts("-1");
goto finallive;
}
} else rk2[++bak] = rk[i];
for (int i = bak; i; --i)
p2[bak - i + 1] = p[rk2[i]] , li2[bak - i + 1] = li[rk2[i]];
register int L = p2[1], last = 2;
for (register int i = mincnt + (li2[1] - mincnt % p2[1] + p2[1]) % p2[1], j, flag; i <= LCM << 2; i += L) {
flag = true;
if (++fff > 20000000) break;
for (j = last; j <= bak; ++j)
if (i % p2[j] != li2[j]) {
flag = false;
break;
} else L = lcm(L, p2[last++]);
if (flag) {
printf("%lld\n", i);
goto finallive;
}
}
puts("-1");
}
finallive:;
}
return 0;
}