#include<stdio.h>
int isInVisibleArr(int k,int a[], int size)
{ int i=0,ans=0;
for(i=0;i<=size;i++)
{
if(k==a[i])
{
ans=1;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
//INPUT
int N,M;
scanf("%d %d",&N,&M);
int Harr[N];//height array
int Carr[N];//Color array
int i=0;
while(i<N)
{ scanf("%d",&Harr[i]);
i++;
}
i=0;
while(i<N)
{ scanf("%d",&Carr[i]);
i++;
}
//Processing:
int vi=0;
int vColor[N]; //stores visible array;
i=N-1-1;
int count=1;
vColor[vi++]=Carr[N-1];
int currVisHt=N-1;
while(i>=0)
{ if(Harr[i]>Harr[currVisHt])
{
if(isInVisibleArr(Carr[i],vColor,vi)==0)
{
count++;
vColor[vi++]=Carr[i];
currVisHt=i;
}
}
i--;
}
printf("\n%d",count);
}
}