/***************
Problem from :
Problem describe :
简单的并查集应用
data:
****************/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<stack>
#include<queue>
#include<ctime>
#include<cstring>
#include<vector>
#include<string>
#define ll __int64
#define inf 0x3f3f3f3f3f
using namespace std;
int fa[10005];
void init(int n)
{
for(int i=1; i<=n; i++) scanf("%d", &fa[i]);
return ;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int i, n, k, ans=0, x, y;
int cas = 0, T;
char c;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &k);
init(n);
printf("Case #%d:\n", ++cas);
while(k--)
{
scanf(" %c", &c);
if(c == 'C')
{
scanf("%d", &x);
fa[x] = 0;
}
else if(c == 'Q')
{
scanf("%d %d", &x, &y);
//找到x和y的集合的首元素
while(fa[x]) x = fa[x];
while(fa[y]) y = fa[y];
printf("%s\n", (x == y)?"YES":"NO");
}
}
}
return 0;
}