#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MAX=501;
int main()
{
int T,M,N;
int a[MAX],b[MAX];
int dp[MAX][MAX];
cin>>T;
while(T--)
{
memset(dp,0,sizeof(dp));
cin>>M;
for(int i=1;i<=M;i++)
cin>>a[i];
cin>>N;
for(int i=1;i<=N;i++)
cin>>b[i];
for(int i=1;i<=M;i++)
{
int max=0;
for(int j=1;j<=N;j++)
{
dp[i][j]=dp[i-1][j];
if(b[j]<a[i]&&dp[i-1][j]>max) max=dp[i-1][j];//关键地方
if(a[i]==b[j]) dp[i][j]=max+1;
}
}
int maxlen=-1;
for(int i=0;i<=N;i++)
if(dp[M][i]>maxlen)//求最大值
maxlen=dp[M][i];
cout<<maxlen<<endl;
if(T!=0)
cout<<endl;
}
system("pause");
return 0;
}
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MAX=501;
int main()
{
int T,M,N;
int a[MAX],b[MAX];
int dp[MAX][MAX];
cin>>T;
while(T--)
{
memset(dp,0,sizeof(dp));
cin>>M;
for(int i=1;i<=M;i++)
cin>>a[i];
cin>>N;
for(int i=1;i<=N;i++)
cin>>b[i];
for(int i=1;i<=M;i++)
{
int max=0;
for(int j=1;j<=N;j++)
{
dp[i][j]=dp[i-1][j];
if(b[j]<a[i]&&dp[i-1][j]>max) max=dp[i-1][j];//关键地方
if(a[i]==b[j]) dp[i][j]=max+1;
}
}
int maxlen=-1;
for(int i=0;i<=N;i++)
if(dp[M][i]>maxlen)//求最大值
maxlen=dp[M][i];
cout<<maxlen<<endl;
if(T!=0)
cout<<endl;
}
system("pause");
return 0;
}