#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int ans;
char s[5005],v[5005];
void solve(char *x, char *y)
{
int l = 0,r = -1,cnt = 0;
int lx = strlen(x);
int ly = strlen(y);
while(l<lx)
{
while(cnt<=n&&r<ly)
{
r++;
if(r==ly)
break;
if(x[r]!=y[r])
cnt++;
}
ans = max(ans,r-l);
if(x[l]!=y[l]) cnt--;
l++;
}
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
cin>>s>>v;
ans = 0;
int len_s = strlen(s);
int len_v = strlen(v);
for(int i=0;i<len_s;i++)
solve(s+i,v);
for(int i=0;i<len_v;i++)
solve(s,v+i);
cout<<ans<<endl;
}
return 0;
}