//ti表示给定的复杂度最多有几层O(n)的循环
//now是现在的层数,maxn是记录的最大层数,check[]记录变量是否使用和对应复杂度(1是O(1),2是O(n))
//firstc记录第一个无法进入的循环的变量
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#define ll long long
using namespace std;
int T,n,ti=0,now,maxn,sum=0;
int check[26];
stack<int>s;
char firstc='0';
bool flag=0;
void krual()//初始化
{
sum=0;
firstc='0';
flag=0;
ti=0;
maxn=0;
now=0;
memset(check,0,sizeof(check));
return ;
}
int main()
{
//freopen("temp.out","w",stdout);
scanf("%d",&T);
while(T--)
{
krual();
scanf("%lld",&n);
string s1,s2,s3,s4;
char c;
cin>>s1;
if(s1[2] == 'n')
{
int len=s1.size();
for(int i=4;i<len-1;++i) ti=ti*10+(s1[i]-'0');
}
while(!s.empty()) s.pop();
while(n--)
{
//cout<<firstc<<" "<<now<<endl;
cin>>c;
if(c == 'F')
{
++sum;
cin>>c>>s3>>s4;
if(flag) continue;
if(check[c-'a'] > 0)
{
flag=1;
continue;
}
if(s4 == "n" && s3 !="n")//合法O(n)
{
++now;
if(firstc == '0')
maxn=max(maxn,now);
s.push(c-'a');
check[c-'a']=2;
}
else if((s3 == "n" && s4 == "n" )|| (s3 != "n" && s4 != "n" && s3 <= s4))//合法O(1)
{
s.push(c-'a');
check[c-'a']=1;
}
else//非法
{
if(firstc == '0') firstc=c;
s.push(c-'a');
check[c-'a']=1;
}
}
else
{
--sum;
if(s.empty()) flag=1;
if(flag) continue;
if(check[s.top()] == 2) --now;
if(s.top() == (int)(firstc-'a')) firstc='0';
check[s.top()]=0;
s.pop();
}
}
if(sum != 0) flag=1;
if(flag) printf("ERR\n");
else if(maxn != ti) printf("No\n");
else printf("Yes\n");
}
return 0;
}