#include<iostream>
#include<fstream>
#include<sstream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int maxn=500001;
bool judge(char* cs, int num)
{
stack<char> st;
while( !st.empty() ) st.pop();
int i=0, len=strlen(cs);
cout<<cs<<endl;
for(char c='1'; c<='1'+num;)
{
if( !st.empty() && i<len && st.top()==cs[i] ){ st.pop(); i++; }
else { st.push(c); c++; }
}
st.pop();
if( st.empty() ) return true;
return false;
}
int main()
{
fstream file;
file.open("E:\\rail.txt", ios::in);
int num;
file>>num;
char* cs=new char[num];
while( file>>cs )
{
if( judge(cs, num) ) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}