#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define TLE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
using namespace std;
stack<int> s;
map<char, int> mp;
int main()
{
int T;
TLE;
cin >> T;
while(T--){
int n;
int m = 0;
cin >> n;
string standard;
cin >> standard;
for (int i = 1; i <= n;i++){
char kind, var, start, end;
cin >> kind;
int size = s.size();
m = max(m, size);
if(kind=='F'){
cin >> var >> start >> end;
if(mp[var]==1){
cout << "ERR" << '\n';
break;
}
else{
mp[var]=1;
s.push(var);
}
if(start=='n'){
break;
}
if(end=='n'){
m++;
}
if(start>end){
break;
}
}else{
if(s.empty()){
cout << "ERR"<<'\n';
break;
}
mp[s.top()] = 0;
}
}
if(!s.empty()){
cout << "ERR" << '\n';
break;
}
if(m==0){
if(standard=="O(1)"){
cout << "Yes" << '\n';
}
else{
cout << "No" << '\n';
}
}
else{
string ans = "O(n^m)";
ans[4] = (m + '0');
if(ans==standard){
cout << "Yes" << '\n';
}
else{
cout << "No" << '\n';
}
}
}
return 0;
}