//poj 3260 Common Subsequence
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1e6;
int tp,n,m;
int b[1005][2005],c[10005],d[10005];
char s1[1005],s2[1005];
inline int LIS()
{
int i,j,len=0;
fo(i,1,tp)
{
if(c[i]>d[len]) d[++len]=c[i];
else if(c[i]<d[1]) d[1]=c[i];
d[lower_bound(d+1,d+len+1,c[i])-d]=c[i];
}
return len;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
while(scanf("%s%s",s1+1,s2+1)==2)
{
M(b);M(d);tp=0;
int len1=strlen(s1+1),len2=strlen(s2+1);
fo(i,1,len2)
b[s2[i]][++b[s2[i]][0]]=i;
fo(i,1,len1)
for(j=b[s1[i]][0];j;j--)
c[++tp]=b[s1[i]][j];
printf("%d\n",LIS());
}
return 0;
}