#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define i128 __int128
#define lid (id << 1)
#define rid (id << 1 | 1)
#define ll __int128
const int N = 1e5+5;
namespace io {
#define fastio ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
#define gc() getchar()
inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; }
inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); }
template <class I> inline void read(I &x){ for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; }
template <class I> inline void write(I x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); }
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::read;
using io::write;
using io::putc;
namespace tianyu{
mt19937 rd(time(0));
int n, m;
ll a[N], p[N], t[N], mx, b[N];
multiset<ll> qwq;
i128 gcd(i128 a, ll b){
return b == 0 ? a : gcd(b, a % b);
}
i128 lcm(i128 a, ll b){
return (a / gcd(a, b)) * (b / gcd(a, b)) * gcd(a, b);
}
ll qpow(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res % p;
}
ll mul(ll a, ll b, ll p){
ll res = 0;
while (b){
if (b & 1) res = (res + a) % p;
a = (a + a) % p;
b >>= 1;
}
return res % p;
}
void exgcd(ll a, ll b, ll &x, ll &y){
if (b == 0){
x = 1, y = 0;
return;
}
exgcd(b, a % b, x, y);
ll k = x;
x = y;
y = k - a / b * y;
}
ll excrt(){
ll M = 1, ans = 0;
for (int i = 1;i <= n;i++){
ll a1 = (i128)M * b[i] % p[i], b1 = p[i], x, y;
ll tmp = (a[i] - b[i] * ans % b1 + b1) % b1;
exgcd(a1, b1, x, y);
ll gcd_ = gcd(a1, b1), bg = b1 / gcd_;
if (tmp % gcd_ != 0) return -1;
x = mul(x, tmp / gcd_, bg);
ans = (i128)(ans + (i128)x * M % b1) % (M * bg);
M *= bg;
}
if (ans < mx) ans += ((mx - ans - 1) / M + 1) * M;
return ans;
}
void awa(){
qwq.clear();
mx = 0;
read(n), read(m);
for (int i = 1;i <= n;i++) read(a[i]);
for (int j = 1;j <= n;j++) read(p[j]);
for (int i = 1;i <= n;i++) read(t[i]);
for (int i = 1;i <= m;i++){
ll awa;
read(awa);
qwq.insert(awa);
}
for (int i = 1;i <= n;i++){
auto res = qwq.upper_bound(a[i]);
if (res != qwq.begin()) --res;
b[i] = *res;
qwq.erase(res);
qwq.insert(t[i]);
mx = max(mx, (a[i] - 1) / b[i] + 1);
}
write(excrt());
putc('\n');
}
}
signed main(){
int T = 1;
cin >> T;
while (T--){
tianyu::awa();
}
return 0;
}