#include<bits/stdc++.h>
#define Maxsize 60
using namespace std;
struct node{
char date[Maxsize];
struct node *lchild;
struct node *rchild;
};
node *CreateBT(char *pre,char*in,int n){
node *b;
char *p;
int k;
if(n<=0)return NULL;
b=(node*)malloc(sizeof(node));
b->date = * pre;
for(p=in;p<in+n;p++)
if(*p==*pre)
break;
k=p-in;
b->lchild =CreateBT(pre+1,in,k);
b->rchild =CreateBT(pre+k+1,p+1,n-k-1);
return b;
}
int BTheight(node * b){
int lchildh,rchildh;
if(b==NULL) return (0);
else{
lchildh=BTheight(b->lchild );
rchildh=BTheight(b->rchild );
return (lchildh>rchildh)?(lchildh+1):(rchildh+1);
}
}
int main () {
node b;
int m;
cin>>m;
getchar();
for(int i=0;i<m;i++){
int j;
cin>>j;
char a[Maxsize];
char c[Maxsize];
cin.getline(a,Maxsize);
cin.getline(c,Maxsize);
CreateBT(a,c,j);
cout<<BTheight(&b)<<endl;
}
}