#include <cstdio>
#include <set>
#define RG register
#define R RG LL
#define GC c = getchar()
using namespace std;
typedef long long LL;
const int N = 1e5 + 9;
LL a[N], p[N], b[N], X, Y, G;
multiset<LL> s;
inline LL in()
{
RG char GC;
while (c < '-')
GC;
R x = c & 15;
GC;
while (c > '-')
x *= 10, x += c & 15, GC;
return x;
}
void exgcd(R a, R b)
{
if (!b)
{
X = 1;
Y = 0;
G = a;
return;
}
exgcd(b, a % b);
(Y ^= X ^= Y ^= X) -= a / b * X;
}
inline LL mul(R b, R k, R m)
{ //快速乘
R a = 0;
for (; k; k >>= 1, b = (b << 1) % m)
if (k & 1)
a = (a + b) % m;
return a;
}
int main()
{
R T = in(), n, m, i, c, k, mx;
RG multiset<LL>::iterator it;
E:
while (T--)
{
n = in();
m = in();
for (i = 1; i <= n; ++i)
a[i] = in();
for (i = 1; i <= n; ++i)
p[i] = in();
for (i = 1; i <= n; ++i)
b[i] = in();
s.clear(); //注意清空
for (i = 1; i <= m; ++i)
s.insert(in());
mx = c = 0;
m = 1; //初始总方程
for (i = 1; i <= n; ++i)
{
it = s.upper_bound(a[i]); //谨慎选择lower_bound和upper_bound
if (it != s.begin())
--it;
k = *it;
s.erase(it);
s.insert(b[i]); //小心手一滑erase(*it)(居然还有90分)
mx = max(mx, (a[i] - 1) / k + 1); //更新限制
k %= p[i];
a[i] %= p[i]; //开始解方程,去掉系数
if (!k && a[i])
{
puts("-1");
goto E;
}
if (!k && !a[i])
continue; //这两个要特判
exgcd(k, p[i]);
if (a[i] % G)
{
puts("-1");
goto E;
}
p[i] /= G;
a[i] = mul(a[i] / G, (X % p[i] + p[i]) % p[i], p[i]);
exgcd(m, p[i]); //开始合并,X和a-c都可能是负数
if ((a[i] - c) % G)
{
puts("-1");
goto E;
}
m = m / G * p[i];
c = (c + mul(mul(m / p[i], ((a[i] - c) % m + m) % m, m), (X % m + m) % m, m)) % m;
}
printf("%lld\n", c >= mx ? c : c + m * ((mx - c - 1) / m + 1)); //满足限制
}
return 0;
}