qinzixu @ 2024-11-03 22:22:24
#include<bits/stdc++.h>
using namespace std;
int t,vis[310];
struct node{
char c;
int num;
};
stack<node>q;
void wk(){
for(int i=1;i<=300;i++)vis[i]=0;
q.push({0,0});
string a;
int l,ans=0,sum=0,f=0;
cin>>l;
cin>>a;
for(int i=1;i<=l;i++){
char op;
cin>>op;
if(op=='F')
{
string y,z;
char x;
cin>>x>>y>>z;
if(f==-1)continue;
sum++;
if(vis[x]){
puts("ERR");
f=-1;
continue;
}
vis[x]=1;
int cl=q.top().num;
if(y=="n"&&z=="n")q.push({x,cl});
//else if(z=="n"&&cl==-1)q.push({x,-1});
else if(z=="n")q.push({x,cl+1}),ans=max(ans,cl+1);
else if(y=="n")q.push({x,-99999});
else q.push({x,cl});
}
else{
if(f==-1)continue;
char c=q.top().c;
q.pop();
vis[c]=0;
sum--;
}
if(sum<0&&f==0)puts("ERR"),f=-1;
}
while(!q.empty())q.pop();
if(f==-1)return;
if(sum!=0)puts("ERR");
else if(a.size()==4){
if(ans==0)puts("Yes");
else puts("No");
}
else if(a[4]-'0'==ans&&a.size()==6)puts("Yes");
else if((a[4]-'0')*10+a[5]-'0'==ans&&a.size()==7)puts("Yes");
else puts("No");
return;
}
int main()
{
cin>>t;
for(int I=1;I<=t;I++)wk();
return 0;
}