import java.util.*;
public class D478{
public static void main(String[] args){
Scanner keyin=new Scanner(System.in);
while(keyin.hasNext()){
int input=keyin.nextInt();
int input2=keyin.nextInt();
int[] a=new int[input2];
int[] b=new int[input2];
for(int i=1;i<=input;i++){
int count=0;
for(int index=0;index<a.length;index++){
a[index]=keyin.nextInt();
}
for(int index2=0;index2<a.length;index2++){
b[index2]=keyin.nextInt();
}
for(int index=0,index3=0;index3<a.length && index<b.length;){
if(a[index3]==b[index]){
count++;
index++;
index3++;
}else if(a[index3]>b[index]){
index++;
}else{
index3++;
}
}
System.out.println(count);
}
}
}
}