#include<bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end()
#define ins(x) inserter(x,x.begin())
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1000010;
typedef set<int> Set;
map<Set,int>id;
vector<Set>c;
int idd(Set x){
if(id.count(x)) return id[x];
c.push_back(x);
return id[x]=c.size()-1;
}
int main(){
int t;
cin>>t;
while(t--){
c.clear();//为什么不能加这个?
stack<int>a;
int n;
cin>>n;
while(n--){
string s;
cin>>s;
if(s=="PUSH") a.push(idd(Set()));
else if(s=="DUP") a.push(a.top());
else {
Set x1=c[a.top()];a.pop();
Set x2=c[a.top()];a.pop();
Set x;
if(s=="UNION") set_union(all(x1),all(x2),ins(x));
else if(s=="INTERSECT") set_intersection(all(x1),all(x2),ins(x));
else if(s=="ADD"){
x=x2;
x.insert(idd(x1));
}
a.push(idd(x));
}
cout<<c[a.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}