#include <bits/stdc++.h>
using namespace std;
#define int              long long int
#define double           long double
#define print(a)         for(auto x : a) cout << x << " "; cout << endl


const int M = 1000000007;
const int N = 3e5+9;
const int INF = 2e9+1;
const int LINF = 2000000000000000001;

inline int power(int a, int b, int mod=M) {
    int x = 1;
    a %= mod;
    while (b) {
        if (b & 1) x = (x * a) % mod; 
        a = (a * a) % mod;
        b >>= 1;
    }
    return x;
}


//_ ***************************** START Below *******************************




vector<int> a;
vector<int> b;

int consistency(int n, int m){
	
	if(m==1) return 1;
	
	vector<int> prefix(m, n);
	int i = 0, j = 0;
	while(i<n && j < m){
		while(i<n && a[i] != b[j]) i++;
		if(i==n) break;
		
		prefix[j] = i;
		i++;
		j++;
	}
	
	vector<int> sufix(m, -1);
	i = n-1, j = m-1;
	while(i>=0 && j>=0){
		while(i>=0 && a[i] != b[j]) i--;
		if(i==-1) break;
		
		sufix[j] = i;
		i--;
		j--;
	}
	
	int ans = 0;
	
	for(int i=0; i<m; i++){
		if(i==0){
			if(sufix[i+1] != -1)	ans++;
		}
		else if(i==m-1){
			if(prefix[i-1] != n)	ans++;
		}
		else{
			int left = prefix[i-1];
			int right = sufix[i+1];
			
			if(left < right){
				ans++;
			}
		}
	}
	
	return ans;
}








// vector<int> a;
// vector<int> b;

int practice(int n, int m){
	

}





void solve() {
    
    int n, m;
    cin >> n >> m;
    
    a.resize(n);
    b.resize(m);
    
    for(int i=0; i<n; i++) cin >> a[i];
    for(int i=0; i<m; i++) cin >> b[i];
    
    cout << consistency(n, m) << endl;


}





int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}