#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
using namespace std;
static char buf[1 << 22];
static int bp = 0, bl = 0;
static inline int gc() {
if (bp == bl) { bl = (int)fread(buf, 1, sizeof(buf), stdin); bp = 0; if (bl == 0) return -1; }
return buf[bp++];
}
static inline ll rd() {
int c = gc();
while (c != -1 && (c < '0' || c > '9')) c = gc();
ll x = 0;
while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = gc(); }
return x;
}
static char obuf[1 << 22];
static int op = 0;
static inline void flush() { fwrite(obuf, 1, op, stdout); op = 0; }
static inline void pc(char c) { if (op == (int)sizeof(obuf)) flush(); obuf[op++] = c; }
static inline void wr(int x) {
if (x == 0) { pc('0'); return; }
char tmp[12]; int c = 0;
while (x > 0) { tmp[c++] = '0' + x % 10; x /= 10; }
while (c > 0) pc(tmp[--c]);
}
int main() {
int t = (int)rd();
while (t--) {
int n = (int)rd(), q = (int)rd();
vector<int> par(n + 1, 0);
vector<ll> edg(n + 1, 0);
for (int i = 2; i <= n; ++i) par[i] = (int)rd();
for (int i = 2; i <= n; ++i) edg[i] = rd();
vector<int> deg(n + 1, 0);
for (int i = 2; i <= n; ++i) ++deg[par[i]];
vector<int> chOff(n + 2, 0);
for (int v = 1; v <= n; ++v) chOff[v + 1] = chOff[v] + deg[v];
vector<int> chArr(max(1, n));
{
vector<int> cur(n + 2, 0);
for (int v = 1; v <= n; ++v) cur[v] = chOff[v];
for (int v = 2; v <= n; ++v) chArr[cur[par[v]]++] = v;
}
vector<int> topo(n);
{
int h = 0, tl = 0;
topo[tl++] = 1;
while (h < tl) {
int u = topo[h++];
for (int i = chOff[u]; i < chOff[u + 1]; ++i) topo[tl++] = chArr[i];
}
}
const ll MEM_LIMIT = 20000000;
vector<ll> lcmSub(n + 1, 1);
vector<ll> totalTime(n + 1, 0);
bool canBuild = true;
ll totalMem = 0;
for (int i = n - 1; i >= 0 && canBuild; --i) {
int u = topo[i];
if (deg[u] == 0) {
lcmSub[u] = 1;
totalTime[u] = 0;
} else {
ll L = deg[u];
for (int j = chOff[u]; j < chOff[u + 1]; ++j) {
int c = chArr[j];
ll g = __gcd(L, lcmSub[c]);
ll nL = L / g * lcmSub[c];
if (nL > MEM_LIMIT || nL < L) { canBuild = false; break; }
L = nL;
}
if (!canBuild) break;
totalMem += L;
if (totalMem > MEM_LIMIT) { canBuild = false; break; }
lcmSub[u] = L;
}
}
if (canBuild) {
vector<vector<int>> tab(n + 1);
for (int i = n - 1; i >= 0; --i) {
int u = topo[i];
if (deg[u] == 0) {
tab[u].assign(1, u);
} else {
ll L = lcmSub[u];
tab[u].resize((size_t)L);
int d = deg[u];
for (ll r = 0; r < L; ++r) {
int idx = (int)(r % (ll)d);
int c = chArr[chOff[u] + idx];
ll nr = (r + (ll)edg[c]) % lcmSub[c];
tab[u][(size_t)r] = tab[c][(size_t)nr];
}
}
for (int j = chOff[u]; j < chOff[u + 1]; ++j) {
vector<int>().swap(tab[chArr[j]]);
}
}
ll L1 = lcmSub[1];
vector<int>& root = tab[1];
for (int i = 0; i < q; ++i) {
ll m = rd();
wr(root[(size_t)(m % L1)]);
pc((i + 1 == q) ? '\n' : ' ');
}
} else {
vector<int> jmp(n + 1);
vector<ll> addT(n + 1, 0);
for (int i = n - 1; i >= 0; --i) {
int u = topo[i];
if (deg[u] == 1) {
int c = chArr[chOff[u]];
jmp[u] = jmp[c];
addT[u] = edg[c] + addT[c];
} else {
jmp[u] = u;
addT[u] = 0;
}
}
int startU = jmp[1];
ll startAdd = addT[1];
for (int i = 0; i < q; ++i) {
ll m = rd();
int u = startU;
ll T = m + startAdd;
while (deg[u] != 0) {
int d = deg[u];
int idx = (int)(T % (ll)d);
int c = chArr[chOff[u] + idx];
T += edg[c] + addT[c];
u = jmp[c];
}
wr(u);
pc((i + 1 == q) ? '\n' : ' ');
}
}
}
flush();
return 0;
}