#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define int long long
#define trace1(x) cerr<<#x<<": "<<x<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
const int N=1e5+5;
const int LG=19;
int n, m, q, ct=0;
int a[N], root[N], level[N], parent[LG][N], st[20*N], lc[20*N], rc[20*N];
vector<int> g[N];
int build(int L, int R)
{
int node=++ct;
if(L==R)
{
st[node]=-1;
return node;
}
int M=(L+R)>>1;
lc[node]=build(L, M);
rc[node]=build(M+1, R);
return node;
}
int update(int onode, int L, int R, int pos, int val)
{
int node=++ct;
if(L==R)
{
st[node]=val;
return node;
}
int M=(L+R)>>1;
lc[node]=lc[onode];
rc[node]=rc[onode];
if(pos<=M)
{
lc[node]=update(lc[onode], L, M, pos, val);
}
else
{
rc[node]=update(rc[onode], M+1, R, pos, val);
}
return node;
}
void dfs(int k, int par, int lvl)
{
int onode=root[par];
root[k]=update(onode, 1, m, a[k], k);
parent[0][k]=par;
level[k]=lvl;
for(auto it:g[k])
{
if(it==par)
continue;
dfs(it, k, lvl+1);
}
}
void precompute()
{
for(int i=1;i<LG;i++)
{
for(int j=1;j<=n;j++)
{
if(parent[i-1][j])
parent[i][j]=parent[i-1][parent[i-1][j]];
}
}
}
int walk(int u, int k)
{
int diff=k;
for(int i=LG-1;i>=0;i--)
{
if(diff&(1<<i))
{
u=parent[i][u];
}
}
return u;
}
int LCA(int u, int v)
{
if(level[u]<level[v])
swap(u,v);
int diff=level[u]-level[v];
for(int i=LG-1;i>=0;i--)
{
if(diff&(1<<i))
{
u=parent[i][u];
}
}
if(u==v)
return u;
for(int i=LG-1;i>=0;i--)
{
if(parent[i][u] && parent[i][u]!=parent[i][v])
{
u=parent[i][u];
v=parent[i][v];
}
}
return parent[0][u];
}
int query(int node, int L, int R, int pos)
{
if(L==R)
{
return st[node];
}
int M=(L+R)>>1;
if(pos<=M)
return query(lc[node], L, M, pos);
else
return query(rc[node], M+1, R, pos);
}
int32_t main()
{
int t;
cin>>t;
while(t--)
{
memset(root
, 0, sizeof(root
)); memset(level
, 0, sizeof(level
)); memset(parent
, 0, sizeof(parent
)); ct=0;
cin>>n>>m>>q;
m++;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=0;i<=N;i++)
{
g[i].clear();
}
for(int i=1;i<=n-1;i++)
{
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
root[0]=build(1, m);
int ans=0;
dfs(1, 0, 0);
precompute();
for(int i=1;i<=q;i++)
{
int p, v;
cin>>p>>v;
int u=1 + ((p^ans)%n + n)%n;
int storeu=u;
int storecol=a[u];
int kth_ancestor=walk(u, v);
if(kth_ancestor==0)
{
ans=-1;
}
else
{
int node=query(root[kth_ancestor], 1, m, storecol);
if(node==-1)
ans=-1;
else
ans=level[storeu]-level[node];
}
cout<<ans<<endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIGludCBsb25nIGxvbmcKIA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