import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
public static int MAX = 100001;
public static int c_index(int[] A,int l,int r,int k){
int m;
while(r-l>1){
m = (l+r)/2;
if(A[m]>=k){
l = m;
}
else{
r = m;
}
}
return r;
}
public static int find_Lcs(int[] A,int size) {
// TODO Auto-generated method stub
int len = 1;
int[] Table = new int[size];
Table[0] = A[0];
for(int i = 0;i<size;i++){
if(A[i] < Table[0]){
Table[0] = A[i];
}
else if(A[i] > Table[len-1]){
Table[len++] = A[i];
}
else{
Table[c_index(Table,-1,len-1,A[i])]=A[i];
}
}
return len;
}
Scanner sc
= new Scanner
(System.
in); int[] X = new int[100001];
int[] Y = new int[100001];
int[] C = new int[100001];
int t
= Integer.
parseInt(br.
readLine()); while(t-->0){
int n
= Integer.
parseInt(br.
readLine()); for(int i =0;i<n;i++){
X
[i
] = Integer.
parseInt(st.
nextToken()); C[X[i]] = i;
}
for(int i =0;i<n;i++){
Y
[i
] = Integer.
parseInt(st.
nextToken()); X[i] = C[Y[i]];
}
out.println(find_Lcs(X,n));
}
//print_Lcs(X,X.length(),Y.length());
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5tYXRoLio7CmltcG9ydCBqYXZhLmlvLio7CnB1YmxpYyBjbGFzcyBNYWluewoJcHVibGljIHN0YXRpYyBpbnQgTUFYID0gMTAwMDAxOwoJcHVibGljIHN0YXRpYyBpbnQgY19pbmRleChpbnRbXSBBLGludCBsLGludCByLGludCBrKXsKCQlpbnQgbTsKCQl3aGlsZShyLWw+MSl7CgkJCW0gPSAobCtyKS8yOwoJCQlpZihBW21dPj1rKXsKCQkJCWwgPSBtOwoJCQl9CgkJCWVsc2V7CgkJCQlyID0gbTsKCQkJfQoJCX0KCQlyZXR1cm4gcjsKCX0KCXB1YmxpYyBzdGF0aWMgaW50IGZpbmRfTGNzKGludFtdIEEsaW50IHNpemUpIHsKCQkvLyBUT0RPIEF1dG8tZ2VuZXJhdGVkIG1ldGhvZCBzdHViCgk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