#include<bits/stdc++.h>
using namespace std;
int t,tt,f;
string s[105];
int is_ok(string st){
int nt = st.size();
for(int i = 0;i < nt-1;i++){
if(st[i] > st[i+1]){
return 0;
}
}
return 1;
}
int main(){
cin >> t;
while(t--){
cin >> tt;
f = 1;
if(tt == 1){
cin >> s[1];
if(is_ok(s[1])){
f = 1;
}else{
f = 0;
}
}else{
for(int i = 1;i <= tt;i++){
cin >> s[i];
}
sort(s+1,s+1+tt);
for(int i = 1;i < tt;i++){
if(is_ok(s[i])==0 || s[i][s[i].size()-1] > s[i+1][0]){
f = 0;
break;
}
}
}
cout << f << '\n';
}
return 0;
}