#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#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;
int a[N], level[N], storedistance[N], parent[LG][N], parentcol[LG][N];
vector<int> col[N];
vector< pair<int, int> > g[N];
void dfs(int k, int par, int lvl, int dist)
{
if(col[a[k]].size()!=0)
{
parentcol[0][k]=col[a[k]].back();
}
parent[0][k]=par;
level[k]=lvl;
storedistance[k]=dist;
col[a[k]].pb(k);
for(auto it:g[k])
{
if(it.ff==par)
continue;
dfs(it.ff, k, lvl+1, it.ss+dist);
}
col[a[k]].pop_back();
}
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]];
if(parentcol[i-1][j])
parentcol[i][j]=parentcol[i-1][parentcol[i-1][j]];
}
}
}
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 dist(int u, int v)
{
return level[u] + level[v] - 2*level[LCA(u,v)];
}
int check(int u, int k, int v)
{
int diff=k;
int par=u;
for(int i=LG-1;i>=0;i--)
{
if(diff&(1<<i))
{
par=parentcol[i][par];
}
}
if(par==0)
return 2;
int leveldiff=level[u]-level[par];
if(leveldiff>=v)
return 1;
return 0;
}
int findnode(int u, int k)
{
int lo=0, hi=n, store;
while(lo<hi)
{
int mid=(lo+hi)>>1;
store=check(u, mid, k);
if(store==1)
{
hi=mid;
}
else if(store==2)
{
hi=mid-1;
}
else
{
lo=mid+1;
}
}
if(check(u, lo, k)!=1)
{
return 0;
}
int diff=lo;
int ans=u;
for(int i=LG-1;i>=0;i--)
{
if(diff&(1<<i))
{
ans=parentcol[i][ans];
}
}
return ans;
}
int32_t main()
{
IOS;
int t;
cin>>t;
while(t--)
{
memset(parent
, 0, sizeof(parent
)); memset(parentcol
, 0, sizeof(parentcol
)); cin>>n>>m>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
g[i].clear();
}
for(int i=0;i<=m;i++)
{
col[i].clear();
}
for(int i=1;i<=n-1;i++)
{
int u,v,w;
cin>>u>>v;
w=1;
g[u].pb(mp(v,w));
g[v].pb(mp(u,w));
}
int ans=0;
dfs(1, 0, 1, 0);
precompute();
for(int i=1;i<=q;i++)
{
int p,k;
cin>>p>>k;
int u=1 + (((p^ans)%n) + n)%n;
int node=findnode(u,k);
if(node==0)
ans=-1;
else
ans=storedistance[u]-storedistance[node];
cout<<ans<<endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKI2RlZmluZSBJT1MgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50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