#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
const int maxn=600001;
int n,m,size;
struct student
{
long long key,x;
}a[maxn];
struct node
{
long long key,rev,x;
node *child[2];
}bst[maxn],*root;
node *pos=bst;
queue<node*>mem;
void rotate(node* &r,bool t)
{
node *y=r->child[!t];
r->child[!t]=y->child[t];
y->child[t]=r;
r=y;
}
void newnode(node* &r,int key)
{
if(mem.empty())r=pos++;
else r=mem.front(),mem.pop();
r->key=key;
r->x=1;
r->rev=rand();
r->child[1]=r->child[0]=NULL;
}
void insert(node* &r,int key)
{
if(!r)newnode(r,key);
else
{
bool t=r->key<key;
insert(r->child[t],key);
if(r->child[t]->rev<r->rev)rotate(r,!t);
}
}
void delet(node* &r,int key)
{
if(!r)return;
if(r->key==key)
{
if(r->child[0]&&r->child[1])
{
bool t=r->child[0]->rev<r->child[1]->rev;
rotate(r,t);
delet(r->child[t],key);
}
else
{
mem.push(r);
if(r->child[0])r=r->child[0];
else r=r->child[1];
}
}
else delet(r->child[r->key<key],key);
}
node* find(node* &r,int key)
{
if(!r)return NULL;
if(r->key==key)return r;
else return find(r->child[r->key<key],key);
}
void dfs(node* &r)
{
if(!r)return;
a[++size].x=r->x;
a[size].key=r->key;
dfs(r->child[0]);dfs(r->child[1]);
}
bool cmp(const student a,const student b)
{
return a.x>b.x;
}
int main()
{
char s[2];
int i;
long long j,k;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]==‘C‘)
{
scanf("%lld",&j);
node *y=find(root,j);
if(y)y->x++;
else insert(root,j);
}
if(s[0]==‘D‘)
{
scanf("%lld",&j);
node *y=find(root,j);
if(y)
{
y->x--;
if(y->x<=0)delet(root,j);
}
}
if(s[0]==‘M‘)
{
scanf("%lld",&j);getchar();
scanf("%lld",&k);
node *x=find(root,j),*y=find(root,k);
if(x!=NULL&&y!=NULL&&x!=y)
{
x->x+=y->x;
delet(root,k);
}
}
}
dfs(root);
sort(a+1,a+size+1,cmp);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%lld",&j);
if(j>size)printf("NO\n");
else printf("%lld\n",a[j].x);
}
return 0;
}