#include<bits/stdc++.h>
#include<cstring>
using namespace std;
stack<char> st;
bool ERR=0;
char flag=0;
int l,w,w1;
string O,x,y;
char c,i1;
bool xu[30];
void solve(){
cin>>c;
if(c=='F'){
cin>>i1>>x>>y;
st.push(i1);
if(xu[i1-'a']==1)ERR=1;
else xu[i1-'a']=1;
if(!ERR&&(x=="n"&&x!=y||x!="n"&&y!="n"&&stoi(x)>stoi(y)))flag=i1;
else if(flag==0&&ERR==0&&x!=y&&y=="n"){
w1++;
}
}else if(c=='E'){
if(st.empty())ERR=1;
if(!ERR)w=max(w1,w);
if(!flag&&!ERR&&w1>0){
w1--;
}
if(!ERR)xu[st.top()-'a']=0;
if(!ERR)if(flag==st.top())flag=0;
if(!ERR)st.pop();
}
}
int main(){
int t;
cin>>t;
while (t--){
cin>>l>>O;
ERR=0;
w=0,w1=0;
for(int i=0;i<30;i++)xu[i]=0;
for(int i=0;i<l;i++){
solve();
}
if(ERR)cout<<"ERR\n";
else if(!st.empty())cout<<"ERR\n";
else if(w==0&&O=="O(1)")cout<<"Yes\n";
else if(w==0&&O!="O(1)")cout<<"No\n";
else{
string temp="O(n^"+to_string(w)+")";
if(temp==O)cout<<"Yes\n";
else cout<<"No\n";
}
while(!st.empty())st.pop();
}
return 0;
}