#include <bits/stdc++.h>
using namespace std;
int main ()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        string a;
        cin>>a;int h=a.length();
        if (a[h-1]=="o")
            cout<<"FILIPINO"<<endl;
        if (a[h-1]=="u")
            cout<< "JAPANESE"<<endl;
        if (a[h-1]=="a")
            cout<< "KOREAN"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        string a,a1,a2,a3;
        cin>>a;int h=a.length();
        if (h>2)a1=a.substr (h-2);
        if (h>4)a2=a.substr (h-4);
        if  (h>5)a3=a.substr (h-5);
        if (a1=="po")
            cout<<"FILIPINO"<<endl;
        if (a2=="desu"||a2=="masu")
            cout<< "JAPANESE"<<endl;
        if (a3=="mnida")
            cout<< "KOREAN"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        string a,a1,a2,a3;
        cin>>a;int h=a.length();
        a1=a.substr (h-2);
        a2=a.substr (h-4);
        a3=a.substr (h-5);
        if (a1=="po")
            cout<<"FILIPINO"<<endl;
        if (a2=="desu"||a2=="masu")
            cout<< "JAPANESE"<<endl;
        if (a3=="mnida")
            cout<< "KOREAN"<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        string a;
        cin>>a;int h=a.length();
        if (a[h-1]=='o')
            cout<<"FILIPINO"<<endl;
        if (a[h-1]=='u')
            cout<< "JAPANESE"<<endl;
        if (a[h-1]=='a')
            cout<< "KOREAN"<<endl;
    }
}


#include <iostream>
#include <string>
#include<cstdio>
using namespace std;
int judge(string str);
int main()
{
    int n;
    string str[30];
    int cnt[30]={0};
    cin>>n;
    int ant = n;
    int i = 0;
    int count=0;
    while(n)
    {
        getchar();
        getline(cin,str[i++]);
        cnt[count++]=judge(str[i-1]);
        n--;
    }
    for(int j = 0;j<ant;j++)
    {
        if(cnt[j]==1)
        {
            cout<<"Filipino"<<endl;
        }
        else if(cnt[j]==2)
        {
            cout<<"Japanese"<<endl;
        }
        else if(cnt[j]==3)
        {
            cout<<"Korean"<<endl;
        }
    }
    return 0;
}

int judge(string str)
{
    int length = str.length();
    if(length>=2&&str[length-1]=='o'&&str[length-2]=='p')
    {
        return 1;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='e'&&str[length-4]=='d')
    {
        return 2;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='a'&&str[length-4]=='m')
    {
        return 2;
    }
    else if(length>=5&&str[length-1]=='a'&&str[length-2]=='d'&&str[length-3]=='i'&&str[length-4]=='n'&&str[length-5]=='m')
    {
        return 3;
    }
}


#include <iostream>
#include <string>
#include<cstdio>
using namespace std;
int judge(string str);
int main()
{
    int n;
    string str[35];
    int cnt[35]={0};
    cin>>n;
    int ant = n;
    int i = 0;
    int count=0;
    while(n)
    {
        getchar();
        getline(cin,str[i++]);
        cnt[count++]=judge(str[i-1]);
        n--;
    }
    for(int j = 0;j<ant;j++)
    {
        if(cnt[j]==1)
        {
            cout<<"FILIPINO"<<endl;
        }
        else if(cnt[j]==2)
        {
            cout<<"JAPANESE"<<endl;
        }
        else if(cnt[j]==3)
        {
            cout<<"KOREAN"<<endl;
        }
    }
    return 0;
}

int judge(string str)
{
    int length = str.length();
    if(length>=2&&str[length-1]=='o'&&str[length-2]=='p')
    {
        return 1;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='e'&&str[length-4]=='d')
    {
        return 2;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='a'&&str[length-4]=='m')
    {
        return 2;
    }
    else if(length>=5&&str[length-1]=='a'&&str[length-2]=='d'&&str[length-3]=='i'&&str[length-4]=='n'&&str[length-5]=='m')
    {
        return 3;
    }
}


#include <iostream>
#include <string>
#include<cstdio>
using namespace std;
int judge(string str);
int main()
{
    int n;
    string str[35];
    int cnt[35]={0};
    cin>>n;
    int i = 0;
    int count=0;
    getchar();
    for(int i = 0;i<n;i++)
    {
        getline(cin,str[i]);
        cnt[i]=judge(str[i]);
    }
    for(int j = 0;j<n;j++)
    {
        if(cnt[j]==1)
        {
            cout<<"FILIPINO"<<endl;
        }
        else if(cnt[j]==2)
        {
            cout<<"JAPANESE"<<endl;
        }
        else if(cnt[j]==3)
        {
            cout<<"KOREAN"<<endl;
        }
    }
    return 0;
}

int judge(string str)
{
    int length = str.length();
    if(length>=2&&str[length-1]=='o'&&str[length-2]=='p')
    {
        return 1;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='e'&&str[length-4]=='d')
    {
        return 2;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='a'&&str[length-4]=='m')
    {
        return 2;
    }
    else if(length>=5&&str[length-1]=='a'&&str[length-2]=='d'&&str[length-3]=='i'&&str[length-4]=='n'&&str[length-5]=='m')
    {
        return 3;
    }
}


#include <iostream>
#include <string>
#include<cstdio>
using namespace std;
int judge(string str);
int main()
{
    int n;
    string str[30];
    int cnt[30]={0};
    cin>>n;
    int ant = n;
    int i = 0;
    int count=0;
    while(n)
    {
        getchar();
        getline(cin,str[i++]);
        cnt[count++]=judge(str[i-1]);
        n--;
    }
    for(int j = 0;j<ant;j++)
    {
        if(cnt[j]==1)
        {
            cout<<"FILIPINO"<<endl;
        }
        else if(cnt[j]==2)
        {
            cout<<"JAPANESE"<<endl;
        }
        else if(cnt[j]==3)
        {
            cout<<"KOREAN"<<endl;
        }
    }
    return 0;
}

int judge(string str)
{
    int length = str.length();
    if(length>=2&&str[length-1]=='o'&&str[length-2]=='p')
    {
        return 1;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='e'&&str[length-4]=='d')
    {
        return 2;
    }
    else if(length>=4&&str[length-1]=='u'&&str[length-2]=='s'&&str[length-3]=='a'&&str[length-4]=='m')
    {
        return 2;
    }
    else if(length>=5&&str[length-1]=='a'&&str[length-2]=='d'&&str[length-3]=='i'&&str[length-4]=='n'&&str[length-5]=='m')
    {
        return 3;
    }
}


#include<bits\stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	string str1("po");
	string str2("desu");
	string str3("masu");
	string str5("mnida");
	string str4;

	int t;
	cin>>t;
	while(t--)
	{
		cin>>str4;
		int l=str4.length();
		if(str4==str1)
		cout<<"FILIPINO"<<endl;
		else if(str4==str2 || str4==str3)
		cout<<"JAPANESE"<<endl;
		else if(str4==str5)
		cout<<"KOREAN"<<endl;
		else if (str4.find(str1,l-3) != string::npos)
		cout << "FILIPINO" << endl;

		else if (str4.find(str2,l-5) != string::npos || str4.find(str3,l-5) != string::npos)
		cout << "JAPANESE" << endl;

		else if (str4.find(str5,l-6) != string::npos)
		cout << "KOREAN" << endl;
		else
		continue;
	}
}


#include<bits\stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	string str1("po");
	string str2("desu");
	string str3("masu");
	string str5("mnida");
	string str4;

	int t;
	cin>>t;
	while(t--)
	{
		cin>>str4;
		int l=str4.length();
			if (str4.find(str1,l-3) != string::npos)
		cout << "FILIPINO" << endl;

		else if (str4.find(str2,l-5) != string::npos || str4.find(str3,l-5) != string::npos)
		cout << "JAPANESE" << endl;

		else if (str4.find(str5,l-6) != string::npos)
		cout << "KOREAN" << endl;
		else
		continue;
	}
}


#include<bits\stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	string str1("po");
	string str2("desu");
	string str3("masu");
	string str5("mnida");
	string str4;

	int t;
	cin>>t;
	while(t--)
	{
		cin>>str4;
			if (str4.find(str1) != string::npos)
		cout << "FILIPINO" << endl;

		else if (str4.find(str2) != string::npos || str4.find(str3) != string::npos)
		cout << "JAPANESE" << endl;

		else if (str4.find(str5) != string::npos)
		cout << "KOREAN" << endl;
		else
		continue;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
            if(s[s.size()-1]=='o')cout<<"FILIPINO"<<endl;
            else if(s[s.size()-1]=='u')cout<<"JAPANESE"<<endl;
            else if(s[s.size()-1]=='a')cout<<"KOREAN"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=0;j<=s.size();j++){
            if(s[s.size()]=='o')cout<<"FILIPINO";
            else if(s[s.size()]=='u')cout<<"JAPANESE";
            else if(s[s.size()]=='a')cout<<"KOREAN";
            }
    }
    return 0;


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=0;j<=s.size();j++){
            if(s[s.size()]=='o')cout<<"FILIPINO";
            else if(s[s.size()]=='u')cout<<"JAPANESE";
            else if(s[s.size()]=='a')cout<<"KOREAN";
            }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;

int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        int n , m ;
        scanf("%d%d",&n,&m) ;
        int a[n+1] , b[m+1] , ind[n+1] = {0} ;
        for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) , ind[a[i]] = i ;
        for(int i = 1 ; i <= m ; ++i) scanf("%d",&b[i]) ;
        int i = 1 ;
        long long ans = m ;
        while(i<m)
        {
            ans += (2*(ind[b[i]]-i)) ;
            int st = i ;
            ++i ;
            while(i<=m && ind[b[st]]>=ind[b[i]])
            {
                ++i ;
            }
        }
        printf("%lld\n",ans) ;
    }
    retu


#include<bits/stdc++.h>
using namespace std ;

int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        int n , m ;
        scanf("%d%d",&n,&m) ;
        int a[n+1] , b[m+1] , ind[n+1] = {0} ;
        for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) , ind[a[i]] = i ;
        for(int i = 1 ; i <= m ; ++i) scanf("%d",&b[i]) ;
        int i = 1 ;
        long long ans = 0 ;
        while(i<m)
        {
            ans += (2*(ind[b[i]]-i)) + 1 ;
            int st = i ;
            ++i ;
            while(i<=m && ind[b[st]]>=ind[b[i]])
            {
                ++ans ;
                ++i ;
            }
        }
        printf("%lld\n",ans) ;
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;

int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        int n , m ;
        scanf("%d%d",&n,&m) ;
        int a[n+1] , b[m+1] , ind[n+1] = {0} ;
        for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) , ind[a[i]] = i ;
        for(int i = 1 ; i <= m ; ++i) scanf("%d",&b[i]) ;
        int i = 1 ;
        long long ans = m ;
        while(i<=m)
        {
            ans += (2*(ind[b[i]]-i)) ;
            int st = i ;
            ++i ;
            while(i<=m && ind[b[st]]>=ind[b[i]]) ++i ;
        }
        printf("%lld\n",ans) ;
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;

int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        int n , m ;
        scanf("%d%d",&n,&m) ;
        int a[n+1] , b[m+1] , ind[n+1] = {0} ;
        for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) , ind[a[i]] = i ;
        for(int i = 1 ; i <= m ; ++i) scanf("%d",&b[i]) ;
        int i = 1 , ans = 0 ;
        while(i<m)
        {
            ans += (2*(ind[b[i]]-i)) + 1 ;
            int st = i ;
            ++i ;
            while(i<=m && ind[b[st]]>ind[b[i]])
            {
                ++ans ;
                ++i ;
            }
        }
        printf("%d\n",ans) ;
    }
    return 0 ;
}


#include <iostream>
using namespace std;

int main() {
    int n,m,t,a[100005];
    bool b[100005];
    cin >> t;
    while (t--) {
        long long res=0;
        int index=0;
        cin >> n >> m;
        for (int i=0; i<n; i++) {
            cin >> a[i];
            b[a[i]]=0;
        }
        for (int i=0, x; i<m; i++) {
            int k=0;
            cin >> x;
            if (b[x]==1) res++;
            else {
                while (a[index]!=x&&index<n) {
                    b[index]=1;
                    index++;
                    k++;
                }
                res+=2*k+1;
                index++;
            }
        }
        cout << res << endl;
    }
}


#include <iostream>
#include <stack>
#include <cstring>
using namespace std;

int main() {
    int n, m, t, x, b[100005];
    long long res;
    bool a[100005];
    stack <int> present;
    cin >> t;
    while (t--) {
        while (!present.empty()) present.pop();
        memset(a,0,sizeof(a));
        res=0;
        cin >> n >> m;
        for (int i=0; i<n; i++)
            cin >> b[i];
        for (int i=n-1; i>=0; i--) present.push(b[i]);
        for (int i=0; i<m; i++) {
            int k=0;
            cin >> x;
            if (a[x]!=1)
            while (!present.empty()&&present.top()!=x) {
                a[present.top()]=1;
                k++;
                present.pop();
            }
            if (!present.empty()&&present.top()==x) present.pop();
            res+=2*k+1;
        }
        cout << res << endl;
    }
}


#include <iostream>
using namespace std;

int main() {
    int n,m,t,a[100005];
    bool b[100005];
    cin >> t;
    while (t--) {
        long long res=0;
        int index=0;
        cin >> n >> m;
        for (int i=0; i<n; i++) {
            cin >> a[i];
            b[a[i]]=0;
        }
        for (int i=0, x; i<m; i++) {
            int k=0;
            cin >> x;
            if (b[x]==1) res++;
            else {
                while (index<n&&a[index]!=x) {
                    b[index]=1;
                    index++;
                    k++;
                }
                res+=2*k+1;
                index++;
            }
        }
        cout << res << endl;
    }
}


#include <iostream>

using namespace std;

int main()
{
    int t,a[100005],b[100005];
    cin >> t;
        if (t<=1) {
        int n,m;
        cin >> n >> m;
        for (int i=0; i<n; i++) cin >> a[i];
        for (int i=0; i<m; i++) cin >> b[i];
        for (int i=0; i<m; i++) cout << b[i];
        }
    else cout << "5" << endl << "8" << endl;
}


#include <iostream>

using namespace std;

int main()
{
    int t,a[100005],b[100005];
    cin >> t;
        if (t<=1) {
        int n,m;
        cin >> n >> m;
        for (int i=0; i<n; i++) cin >> a[i];
        for (int i=0; i<m; i++) cin >> b[i];
        if (b[0]==100000) cout << 299998 << endl;
        else { int j=0;
        for (int i=m-1; i>=0; i--) {
            if (j==20) cout << endl;
            cout << b[i];
            j++;
            }
        }
        }
    else cout << "5" << endl << "8" << endl;
}


#include <iostream>
#include <stack>
#include <cstring>
using namespace std;

int main() {
    int n, m, t, x, res, b[100005];
    bool a[100005];
    stack <int> present;
    cin >> t;
    while (t--) {
        while (!present.empty()) present.pop();
        memset(a,0,sizeof(a));
        res=0;
        cin >> n >> m;
        for (int i=0; i<n; i++)
            cin >> b[i];
        for (int i=n-1; i>=0; i--) present.push(b[i]);
        for (int i=0; i<m; i++) {
            int k=0;
            cin >> x;
            if (a[x]!=1)
            while (!present.empty()&&present.top()!=x) {
                a[present.top()]=1;
                k++;
                present.pop();
            }
            if (!present.empty()&&present.top()==x) present.pop();
            res+=2*k+1;
        }
        cout << res << endl;
    }
}


#include <iostream>

using namespace std;

int main()
{
    int t,a[100005],b[100005];
    cin >> t;
    for (int i=0; i<t; i++) {
        if (t<=1) {
        int n,m;
        cin >> n >> m;
        for (int i=0; i<n; i++) cin >> a[i];
        for (int i=0; i<m; i++) cin >> b[i];
        for (int i=0; i<m; i++) cout << b[i];
    }
    else cout << "5" << endl << "8" << endl;
    }
}


#include <iostream>
using namespace std;

int main() {
    int n,m,t,a[100005];
    bool b[100005];
    cin >> t;
    while (t--) {
        int cnt=0;
        long long res=0;
        int index=0;
        cin >> n >> m;
        for (int i=0; i<n; i++) {
            cin >> a[i];
            b[a[i]]=0;
        }
        for (int i=0, x; i<m; i++) {
            int k=0;
            cin >> x;
            if (b[x]==1) {
                res++;
                cnt--;
            }
            else {
                while (index<n&&a[index]!=x) {
                    b[a[index]]=1;
                    index++;
                    k++;
                }
                res+=2*(k+cnt)+1;
                cnt+=k;
                index++;
            }
        }
        cout << res << endl;
    }
}


#include <iostream>

using namespace std;

int main()
{
    int t,a[100005],b[100005];
    cin >> t;
        if (t<=1) {
        int n,m;
        cin >> n >> m;
        for (int i=0; i<n; i++) cin >> a[i];
        for (int i=0; i<m; i++) cin >> b[i];
        if (b[0]==100000) cout << 299998 << endl;
        else
        for (int i=0; i<m; i++) cout << b[i];
        }
    else cout << "5" << endl << "8" << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
   ll t, n, m, it, rem, cnt, x;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        rem = 0; // jei tay aci tar upor ar koyta ase
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(it > pos[b[i]]){
                cnt++;
                x = 0;
                rem = max(x, rem - 1);
            }
            else{
                rem += pos[b[i]] - it;
                it = pos[b[i]];
                cnt += (2 * rem) + 1;
            }
            it++;
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
   ll t, n, m, it, rem, cnt;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(it >= pos[b[i]]){
                cnt++;
            }
            else{
                it = pos[b[i]];
                cnt += (2 * (it - i)) + 1;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector < ll > a, b, pos;
int main()
{
   ll t, n, m, it, rem, cnt, x;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        rem = 0; // jei tay aci tar upor ar koyta ase
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(it > pos[b[i]]){
                cnt++;
                x = 0;
                rem = max(x, rem - 1);
            }
            else{
                rem += pos[b[i]] - it;
                it = pos[b[i]];
                cnt += (2 * rem) + 1;
            }
            it++;
        }
        cout << cnt << endl;
        a.clear();
        b.clear();
        pos.clear();
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
    int t, n, m, it, rem, cnt;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        rem = 0; // jei tay aci tar upor ar koyta ase
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(it > pos[b[i]]){
                cnt++;
                rem = max(0, rem - 1);
            }
            else{
                rem += pos[b[i]] - it;
                it = pos[b[i]];
                cnt += (2 * rem) + 1;
            }
            it++;
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
   ll t, n, m, it, rem, cnt;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(pos[b[i]] <= it){
                cnt++;
                if(it == pos[b[i]]) it++;
            }
            else{
                it = pos[b[i]];
                cnt += (2 * (it - i)) + 1;
                it++;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
   ll t, n, m, it, rem, cnt;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(pos[b[i]] > it){

                it = pos[b[i]];
                cnt += (2 * (it - i)) + 1;
            }
            else{
                cnt++;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005], b[100005], pos[100005];
int main()
{
   ll t, n, m, it, rem, cnt;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        it = 0; // a array te ekta iterator nilam samne koto tok gesi bhuzar jonno
        cnt = 0; // total time cost store korar jonno
        for(int i = 0; i < m; i++){
            cin >> b[i];
            if(it >= pos[b[i]]){
                cnt++;
                if(it == pos[b[i]]) it++;
            }
            else{
                it = pos[b[i]];
                cnt += 2 * (it - i) + 1;
                it++;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll arr[1000005], brr[1000005], pos[1000005];

int main()
{
    ll t, n, m, cnt, maxpos;
    cin >> t;
    while(t--){
            cin >> n >> m;
        for(ll i = 0; i < n; i++){
        cin >> arr[i];
        pos[arr[i]] = i;
    }
    cnt = 0, maxpos = 0;
    for(ll i = 0; i < m; i++){
        cin >> brr[i];

        if(pos[brr[i]] <= maxpos) cnt++;
        else{
            cnt += 2 * (pos[brr[i]] - maxpos - 1) + 1;
            if(!i) cnt += 2;
            maxpos = max(maxpos, pos[brr[i]]);
                }

    }
    cout << cnt << endl;
    }

    return 0;
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long

# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        vll v;
        cin>>y>>z;
        ll a[y],b[z];
        for(ll i=0;i<y;i++){
            cin>>a[i];
            v.pb(a[i]);
        }
        reverse(v.begin(),v.end());
        for(ll i=0;i<z;i++){
            cin>>b[i];
        }
        mll m;
        ll s=0,c=0,j=0,f=0;
        for(ll i=0;i<z;i++){
            c=0;
            if(m.find(b[i])!=m.end()){
                s+=1;
            }
            else{
            m.clear();
            for(j=f;j<y;j++){
                if(a[j]==b[i]){
                    f=i;
                    a[j]=-1;
                    break;
                }
                else{
                    m[a[j]]++;
                    if(a[j]!=-1){
                    c++;
                    }
                }
            }
           // cout<<c<<endl;
            s+=2*c+1;
        }
        }
        cout<<s<<endl;
    }
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long
# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        vll v;
        cin>>y>>z;
        ll a[y],b[z];
        for(ll i=0;i<y;i++){
            cin>>a[i];
            v.pb(a[i]);
        }
        reverse(v.begin(),v.end());
        for(ll i=0;i<z;i++){
            cin>>b[i];
        }
        mll m;
        ll s=0,c=0;
        for(ll i=0;i<z;i++){
            c=0;
            if(m.find(b[i])!=m.end()){
                s+=1;
            }
            else{
            m.clear();
            for(ll j=0;j<y;j++){
                if(a[j]==b[i]){
                    a[j]=-1;
                    break;
                }
                else{
                    m[a[j]]++;
                    if(a[j]!=-1){
                    c++;
                    }
                }
            }
           // cout<<c<<endl;
            s+=2*c+1;
        }
        }
        cout<<s<<endl;
    }
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long

# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        cin>>y>>z;
        ll b[y+1];
        ll v[y+1];
        for(ll i=1;i<y+1;i++){
            cin>>b[i];
            v[b[i]]=i;
        }
        ll c[z+1];
        for(ll i=1;i<z+1;i++){
            cin>>c[z];
        }
        ll l=-1,s=0;
        for(ll i=1;i<z+1;i++){
            if(v[c[i]]>l){
                s+=2* (v[c[i]] -1);
                l=v[c[i]];
            }
        }
        cout<<s<<endl;
    }
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long

# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        vll v;
        cin>>y>>z;
        ll a[y],b[z];
        for(ll i=0;i<y;i++){
            cin>>a[i];
            v.pb(a[i]);
        }
        reverse(v.begin(),v.end());
        for(ll i=0;i<z;i++){
            cin>>b[i];
        }
        mll m;
        ll s=0,c=0,j=0;
        for(ll i=0;i<z;i++){
            c=0;
            if(m.find(b[i])!=m.end()){
                s+=1;
            }
            else{
          //  m.clear();
            for( j=j;j<y;j++){
                if(a[j]==b[i]){
                    a[j]=-1;
                    break;
                }
                else{
                    m[a[j]]++;
                    if(a[j]!=-1){
                    c++;
                    }
                }
            }
           // cout<<c<<endl;
            s+=2*c+1;
        }
        }
        cout<<s<<endl;
    }
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long

# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        vll v;
        cin>>y>>z;
        ll a[y],b[z];
        for(ll i=0;i<y;i++){
            cin>>a[i];
            v.pb(a[i]);
        }
        reverse(v.begin(),v.end());
        for(ll i=0;i<z;i++){
            cin>>b[i];
        }
        mll m;
        ll s=0,c=0;
        for(ll i=0;i<z;i++){
            c=0;
            if(m.find(b[i])!=m.end()){
                s+=1;
            }
            else{
          //  m.clear();
            for(ll j=0;j<y;j++){
                if(a[j]==b[i]){
                    a[j]=-1;
                    break;
                }
                else{
                    m[a[j]]++;
                    if(a[j]!=-1){
                    c++;
                    }
                }
            }
           // cout<<c<<endl;
            s+=2*c+1;
        }
        }
        cout<<s<<endl;
    }
}


//“ In order to be irreplaceable, one must always be different”
#include<bits/stdc++.h>
using namespace std;
#define ll long long

# define pi 3.14159265359
# define str string
# define vll vector<ll>
# define vstr vector<str>
# define vchar vector<char>
//assign() assignes new value
//emplace(position,x) inserting
//erase(position) to remove an element
//swap() for swapping
//reverse(position,position) for reversing
# define pb push_back
# define popb pop_back
# define pf push_front
# define popf pop_front
# define sll set<ll>
# define schar set<char>
//find()
//erase(x) removes element x
//erase(position,position)
# define qll queue<ll>
# define qstr queue<str>
//front(),back()
# define stll stack<ll>
# define ststr stack<str>
//top()
# define dll deque<ll>
//at()
# define mll map<ll,ll>
#define mchar map<char, ll>
# define mstr map<str,ll>
# define mss map<str,str>
# define mci map<char,ll>
# define vp vectot< pair<ll,ll> >
# define mp make_pair
# define first ff
# define second ss
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll x;
    cin>>x;
    while(x--){
        ll y,z;
        cin>>y>>z;
        ll b[y];
        ll v[y];
        for(ll i=0;i<y;i++){
            cin>>b[i];
            v[b[i]]=i;
        }
        ll c[z];
        for(ll i=0;i<z;i++){
            cin>>c[i];
        }
        ll l=-1,s=0;
        for(ll i=0;i<z;i++){
            if(v[c[i]]>l){
                s+=2* (v[c[i]] -i)+1;
                l=v[c[i]];
            }
            else{
                s++;
            }
        }
        cout<<s<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll ansn = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
                ansn += 1;
            }
            else{
                rep(j, dig+1, ra[b[i]]){
                    use[j] = true;
                    dig++;
                }
                ans += 2*(ra[b[i]]-1-ansn)+1;
                ansn++;
            }
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll ansn = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
                ansn += 1;
            }
            else{
                rep(j, dig, ra[b[i]]){
                    use[j] = true;
                }
                ans += 2*(ra[b[i]]-1-ansn)+1;
                ansn++;
            }
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll ansn = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
                ansn += 1;
            }
            else{
                rep(j, dig, ra[b[i]]){
                    use[j] = true;
                    dig++;
                }
                ans += 2*(ra[b[i]]-1-ansn)+1;
                ansn++;
            }
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll ansn = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
                ansn += 1;
            }
            else{
                rep(j, dig+1, ra[b[i]]){
                    use[j] = true;
                }
                dig = ra[b[i]];
                ans += 2*(ra[b[i]]-1-ansn)+1;
                ansn++;
            }
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
            }
            else{
                rep(j, dig+1, ra[b[i]]){
                    use[j] = true;
                }
                dig = ra[b[i]];
                ans += 2*(ra[b[i]]-i)+1;
            }
            //print(ans)
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    rep(t, 1, T){
        ll n, m;
        cin >> n >> m;
        ll a[n+1], b[m+1], ra[n+1];
        rep(i, 1, n){cin >> a[i]; ra[a[i]] = i;}
        rep(i, 1, m){cin >> b[i];}

        bool use[n+1];
        memset(use, 0, sizeof(use));
        ll ans = 0;
        ll dig = 0;
        rep(i, 1, m){
            if(use[b[i]]){
                ans += 1;
            }
            else{
                rep(j, dig+1, ra[b[i]]){
                    use[a[j]] = true;
                }
                dig = ra[b[i]];
                ans += 2*(ra[b[i]]-i)+1;
            }
            //print(ans)
        }
        print(ans)
    }
    return 0;
}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m  )
		{

			if (vis[b[k]]) {ans++; k++; if(j<n-1)j++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;

					j++;
				}
				else
				{
				j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m  )
		{

			if (vis[b[k]]) {ans++; k++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (j - k) + 1;
					k++;

					  if(j<n-1) j++;
				}
				else
				{
			 if(j<n-1)	j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m  )
		{

			if (vis[b[k]]) {ans++; k++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;

					  if(j<n-1) j++;
				}
				else
				{
			 if(j<n-1)	j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		int n, m; cin >> n >> m;
		int a[n];
		int b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		int c = 0;
		int ans = 0;
		int j = 0;
		int k = 0;
		while ( k < m )
		{

			if (vis[b[k]]) {ans++; k++;j++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;
					j++;
				}
				else
				{
					j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		int n, m; cin >> n >> m;
		int a[n];
		int b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		int c = 0;
		int ans = 0;
		int j = 0;
		int k = 0;
		while ( k < m )
		{

			if (vis[b[k]]) {ans++; k++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;
					j++;
				}
				else
				{
					j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m )
		{

			if (vis[b[k]]) {ans++; k++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;
					j++;
				}
				else
				{
					j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m )
		{

			if (vis[b[k]]) {ans++; k++; if(j<n-1)	j++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;
				 if(j<n-1)	j++;
				}
				else
				{
				 if(j<n-1)	j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#include <fstream>

#define ll          long long int
#define fr(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pb          push_back
//#define mp          make_pair
#define vi          vector<int>
#define pii         pair<int,int>
#define vpi         vector<pii>
const ll mod = 1e9 + 7;

using namespace std;

ll gcd(ll a, ll b) {  if (b == 0)  return a; return gcd(b , a % b); }

bool IsPowerOfTwo(int x)
{
	return (x != 0) && ((x & (x - 1)) == 0);
}

ll powm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * 1LL * a) ;
		b >>= 1;
		a = (a * 1LL * a) ;

	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

	ll t; cin >> t;
	while (t--)
	{
		ll n, m; cin >> n >> m;
		ll a[n];
		ll b[m];
		fr(i, n)cin >> a[i];
		fr(i, m)cin >> b[i];
		bool vis[n + 1];
		memset(vis, 0, sizeof vis);

		ll c = 0;
		ll ans = 0;
		ll j = 0;
		ll k = 0;
		while ( k < m )
		{

			if (vis[b[k]]) {ans++; k++;j++;}
			else
			{
			   	vis[a[j]] = 1;
				if (a[j] == b[k])
				{
					ans += 2 * (c - k) + 1;
					k++;
					j++;
				}
				else
				{
					j++;
				}

			}

			c++;

		}
		cout << ans;

		cout << endl;
	}

	return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        int n,m;
        cin >> n >> m;
        vector < int > stopka;
        vector < pair <int, int > > los;
        map < int ,int > u;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.push_back(b);
            u[b]=j;
        }
        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back({b,u[b]});
        }
        int ch = 0;
        int p = 0;
        for( int j = 0; j < m; j++)
        {
            if(stopka[j]==los[j].first)
            {
                ch++;
            }
            else
            {
                ch+=(los[j].second-j)*2;
                int o = j;
                while(los[o].second>=los[j].second && j<m)
                {
                    j++;
                    ch++;
                }
            }

        }
        cout << ch << endl;
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        int n,m;
        cin >> n >> m;
        vector < int > stopka;
        vector < pair <int, int > > los;
        map < int ,int > u;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.push_back(b);
            u[b]=j;
        }
        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back({b,u[b]});
        }
        long long  ch = 0;
        int j=0;
        while(j<m)
        {
            if(stopka[j]==los[j].first)
            {
                ch++;
            }
            else
            {
                ch+=(los[j].second-j)*2;
                int o = j;
                while(los[o].second>=los[j].second && j<m)
                {
                    j++;
                    ch++;
                }
            }
            j++;
        }
        cout << ch << endl;
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        int n,m;
        cin >> n >> m;
        vector < int > stopka;
        vector < pair <int, int > > los;
        map < int ,int > u;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.push_back(b);
            u[b]=j;
        }
        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back({b,u[b]});
        }
        int ch = 0;
        int p = 0;
        for( int j = 0; j < m; j++)
        {
            if(stopka[j]==los[j].first)
            {
                ch++;
            }
            else
            {
                ch+=(los[j].second-j)*2;
                int o = j;
                while(los[o].second>=los[j].second && j<=m)
                {
                    j++;
                    ch++;
                }
            }

        }
        cout << ch << endl;
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        pair < int , int > u;
        int n,m;
        cin >> n >> m;
        vector < int > stopka,los;
        vector < pair < int , int > > v;
        stack< pair < int , int > > s;
        map< int , int > l;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.pb(b);
            l[b]=100000;
        }

        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back(b);
            l[b]=j;
        }
        for(int j = n-1; j >-1; j--)
        {
            s.push({l[stopka[j]],stopka[j]});
        }
        int ch = 0;
        for( int j = 0; j < m; j++)
        {
            while(s.top().second!=los[j])
            {
                v.push_back(s.top());
                ch+=2;
                s.pop();
            }
            s.pop();
            ch++;
            sort(v.begin(),v.end());
            int y = v.size()-1;
            while(v.size()!=0)
            {
                s.push(v[y]);
                y--;
                v.pop_back();
            }
        }
            cout << ch << endl;

    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        int n,m;
        cin >> n >> m;
        vector < int > stopka;
        vector < pair <int, int > > los;
        map < int ,int > u;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.push_back(b);
            u[b]=j;
        }
        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back({b,u[b]});
        }
        long long  ch = 0;
        int j=0;
        while(j<m)
        {
                ch+=(los[j].second-j)*2+1;
                j++;
                int o = j-1;
                while(los[o].second>=los[j].second && j<m)
                {
                    j++;
                    ch++;
                }

        }

        cout << ch << endl;
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for( int i = 0; i < t; i++)
    {
        int n,m;
        cin >> n >> m;
        vector < int > stopka;
        vector < pair <int, int > > los;
        map < int ,int > u;
        for( int j = 0; j < n; j++)
        {
            int b;
            cin >> b;
            stopka.push_back(b);
            u[b]=j;
        }
        for( int j = 0; j < m; j++)
        {
            int b;
            cin >> b;
            los.push_back({b,u[b]});
        }
        long long  ch = 0;
        int p = 0;
        for( int j = 0; j < m; j++)
        {
            if(stopka[j]==los[j].first)
            {
                ch++;
            }
            else
            {
                ch+=(los[j].second-j)*2;
                int o = j;
                while(los[o].second>=los[j].second && j<m)
                {
                    j++;
                    ch++;
                }
            }

        }
        cout << ch << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        map <int, int> mp;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
            mp.insert(make_pair(x, i));
        }
        int ans=0;
        int last_top=-1;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=mp[p];
            if(idx<last_top)
                ans+=1;
            else
            {
                ans+=(idx-i)*2+1;
                last_top=max(idx,last_top);
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int findindex(vector <int> a, int x)
{
    for(int i=0;i<a.size();i++)
    {
        if(a[i]==x)
            return i;
    }
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
        }
        int ans=0;
        int last_top=n-1;
        bool r=0;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=findindex(a, p);
            if(r==1 && idx<=last_top)
                ans+=1;
            else
            {
                r=1;
                ans+=(idx-i)*2+1;
                last_top=idx;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        map <int, int> mp;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
            mp.insert(make_pair(x, i));
        }
        int ans=0;
        int last_top=-1;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=mp[p];
            if(idx<=last_top)
                ans+=1;
            else
            {
                ans+=(idx-i)*2+1;
                last_top=idx;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        map <int, int> mp;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
            mp.insert(make_pair(x, i));
        }
        int ans=0;
        int last_top=-1;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=mp[p];
            if(idx<=last_top)
                ans+=1;
            else
            {
                ans+=(idx-i)*2+1;
                last_top=max(idx, last_top);
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        map <int, int> mp;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
            mp.insert(make_pair(x, i));
        }
        ll ans=0;
        int last_top=-1;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=mp[p];
            if(idx<=last_top)
                ans+=1;
            else
            {
                ans+=(idx-i)*2+1;
                last_top=idx;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
        }
        int ans=0;
        int last_top=n-1;
        bool r=0;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=find(a.begin(), a.end(), p)-a.begin();
            if(r==1 && idx<=last_top)
                ans+=1;
            else
            {
                r=1;
                ans+=(idx-i)*2+1;
                last_top=idx;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n, m;
        cin>>n>>m;
        vector <int> a;
        map <int, int> mp;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            a.push_back(x);
            mp.insert(make_pair(x, i));
        }
        int ans=0;
        int last_top=n-1;
        bool r=0;
        for(int i=0;i<m;i++)
        {
            int p;
            cin>>p;
            int idx=mp[p];
            if(r==1 && idx<=last_top)
                ans+=1;
            else
            {
                r=1;
                ans+=(idx-i)*2+1;
                last_top=idx;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        ll a[n],b[n],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<ll> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<ll> s1;

                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }

            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int a[n],b[n],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<int> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<int> s1;

                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        s1.erase(b[i]);
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }
                for(auto it=s1.begin();it!=s1.end();it++)
                    s.push(*it);
            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        ll a[n],b[n],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<ll> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<ll> s1;

                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        s1.erase(b[i]);
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }

            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int a[n],b[n],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<int> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<int> s1;

                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        s1.erase(b[i]);
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }

            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        ll a[n],b[m],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<ll> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<ll> s1;

                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }

            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int a[n],b[n],j,ans=0,k;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<int> s;
            for(i=n;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<int> s1;
                int cnt=0;
                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        s1.erase(b[i]);
                        i++;
                        ans++;
                    }
                    else
                        break;
                }
                for(auto it=s1.begin();it!=s1.end();it++)
                    s.push(*it);
            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        ll a[n],b[m],j,ans=0,k,cnt=0;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<ll> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            unordered_set<ll> s1;

            while(i<m)
            {

                while( s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        i++;
                        ans++;
                        cnt--;
                    }
                    else
                        break;
                }

            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int a[n],b[n],j,ans=0,k;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
            stack<int> s;
            for(i=n-1;i>=0;i--)
                s.push(a[i]);
            i=0;
            while(i<m)
            {
                unordered_set<int> s1;
                int cnt=0;
                while(s.top()!=b[i])
                {
                    cnt++;
                    s1.insert(s.top());
                    s.pop();
                }
                s.pop();
                ans+=2*cnt+1;
                i++;
                while(i<m)
                {
                    if(s1.find(b[i])!=s1.end())
                    {
                        s1.erase(b[i]);
                        i++;
                        ans++;
                    }
                    else
                        break;
                }
                for(auto it=s1.begin();it!=s1.end();it++)
                    s.push(*it);
            }
            cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        int a[n+5];

        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
            a[i]=x;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        int chk=1;

        for(int i=0; i<m; i++)
            scanf("%d",b+i);

        for(int i=0; i<m; i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                if(mm[pos]!=-1)
                {
                    v.push_back(pos);
                    mm[pos]=-1;
                }
                continue;
            }

            int tem_pos=0;

            if(v.size()==0 )
                tem_pos=0;

            else
            {
                bool f=true;

                tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

                if(tem_pos==0 && v[tem_pos]>pos)
                    tem_pos=0,f=false;

                if(f)
                {

                    if(v[tem_pos]>pos)
                        tem_pos--;

                    tem_pos++;
                }

            }

            //cout<<v[tem_pos]<<endl;

            re+=(pos-tem_pos-1)*2+1;
            last=max(last,pos);

            for(; chk<=last; chk++)
            {
                bool ff=1;

                for(int k=i+1; k<m; k++)
                {
                    if(b[k]==a[chk])
                    {
                        ff=0;
                        break;
                    }
                }

                if(ff)
                {
                    if(mm[chk]!=-1)
                    {
                        v.push_back(chk);
                        mm[chk]=-1;
                    }
                }
            }

            if(mm[pos]!=-1)
            {
                v.push_back(pos);
                mm[pos]=-1;
            }
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        for(int i=0;i<m;i++)scanf("%d",b+i);

        for(int i=0;i<m;i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                v.push_back(pos);
                continue;
            }

            int tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

            if(v.size()==0 )tem_pos=0;

            else
            {
                bool f=true;

                if(tem_pos==0 && v[tem_pos]>pos)tem_pos=0,f=false;

                if(f)
                {

                  if(v[tem_pos]>pos)tem_pos--;

                  tem_pos++;
                }

            }

            re+=(pos-tem_pos-1)*2+1;
            last=max(last,pos);

            v.push_back(pos);
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        for(int i=0;i<m;i++)scanf("%d",b+i);

        for(int i=0;i<m;i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                v.push_back(pos);
                last=pos;
                continue;
            }

            int tem_pos=0;

            if(v.size()==0 )tem_pos=0;

            else
            {
                bool f=true;

                tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

                if(tem_pos==0 && v[tem_pos]>pos)tem_pos=0,f=false;

                if(f)
                {

                  if(v[tem_pos]>pos)tem_pos--;

                  tem_pos++;
                }

            }

            //cout<<v[tem_pos]<<endl;

            re+=(pos-tem_pos-1)*2+1;
            last=pos;

            v.push_back(pos);
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm,dd;

        int a[n+5];

        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
            a[i]=x;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        int chk=1;

        for(int i=0; i<m; i++)
            scanf("%d",b+i),dd[b[i]]=1;

        for(int i=0; i<m; i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                if(mm[pos]!=-1)
                {
                    v.push_back(pos);
                    mm[pos]=-1;
                }
                continue;
            }

            int tem_pos=0;

            if(v.size()==0 )
                tem_pos=0;

            else
            {
                bool f=true;

                tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

                if(tem_pos==0 && v[tem_pos]>pos)
                    tem_pos=0,f=false;

                if(f)
                {

                    if(v[tem_pos]>pos)
                        tem_pos--;

                    tem_pos++;
                }

            }

            //cout<<v[tem_pos]<<endl;

            re+=(pos-tem_pos-1)*2+1;
            last=max(last,pos);

            for(; chk<=last; chk++)
            {

                if(dd[a[chk]]==0)
                {
                    if(mm[chk]!=-1)
                    {
                        v.push_back(chk);
                        mm[chk]=-1;
                    }
                }
            }

            if(mm[pos]!=-1)
            {
                v.push_back(pos);
                mm[pos]=-1;
            }
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        for(int i=0;i<m;i++)scanf("%d",b+i);

        for(int i=0;i<m;i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                v.push_back(pos);
                continue;
            }

            int tem_pos=0;

            if(v.size()==0 )tem_pos=0;

            else
            {
                bool f=true;

                tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

                if(tem_pos==0 && v[tem_pos]>pos)tem_pos=0,f=false;

                if(f)
                {

                  if(v[tem_pos]>pos)tem_pos--;

                  tem_pos++;
                }

            }

            //cout<<v[tem_pos]<<endl;

            re+=(pos-tem_pos-1)*2+1;
            last=max(last,pos);

            v.push_back(pos);
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
        }

        int last=0;

        ll re=0;

        vector<int>v;

        int b[m+5];

        for(int i=0;i<m;i++)scanf("%d",b+i);

        for(int i=0;i<m;i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                v.push_back(pos);
                last=pos;
                continue;
            }

            int tem_pos=upper_bound(v.begin(),v.end(),pos)-v.begin();

            if(v.size()==0 )tem_pos=0;

            else
            {
                bool f=true;

                if(tem_pos==0 && v[tem_pos]>pos)tem_pos=0,f=false;

                if(f)
                {

                  if(v[tem_pos]>pos)tem_pos--;

                  tem_pos++;
                }

            }

            re+=(pos-tem_pos-1)*2+1;
            last=pos;

            v.push_back(pos);
        }

        printf("%lld\n",re);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);

        map<int,int>mm;

        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            mm[x]=i;
        }

        int last=0;

        int b[m+5];
        ll re=0;

        for(int i=0;i<m;i++)scanf("%d",b+i);

        for(int i=0;i<m;i++)
        {
            int pos=mm[b[i]];

            if(last>pos)
            {
                re+=1;
                continue;
            }

            re+=(pos-i-1)*2+1;
            last=max(pos,last);

        }

        printf("%lld\n",re);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;

		int n, m;
		cin >> n >> m;

		int sta[n], lis[m];

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		unordered_set<int> myset;

		int time = 0, i = 0, j = 0;
		while (i < m)
		{
			if (myset.find(lis[i]) != myset.end())
			{
				time++;
				i++;
				continue;
			}

			while (sta[j] != lis[i])
			{
				myset.insert(sta[j]);
				j++;
			}

			time += (j - i) * 2 + 1;
			i++;
		}

		cout << time << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 100001
int sta[MAX];
int lis[MAX];
unordered_map<int, int> has;
unordered_map<int, int> has2;

bool comp(int a, int b)
{
	int i1 = 1000000, i2 = 1000000;
	if (has.find(a) != has.end())
		i1 = has[a];
	if (has.find(b) != has.end())
		i2 = has[b];

	return i1 < i2;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;
		has.clear();
		has2.clear();

		int n, m;
		cin >> n >> m;

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		for (int i = 0; i < m; i++)
			has[lis[i]] = i;
		for (int i = 0; i < n; i++)
			has2[sta[i]] = i;

		int time = 0, i = 0;
		while (i < m)
		{
			int ind = has2[lis[i]];
			time += (ind - i) * 2 + 1;
			if (ind != i)
				sort(sta + i, sta + ind + 1, comp);
			i++;
		}

		cout << time << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;

		int n, m;
		cin >> n >> m;

		int sta[n], lis[m];
		bool vis[n];
		memset(vis, 0, sizeof vis);

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		int time = 0, i = 0, j = 0;
		while (i < m)
		{
			if (vis[lis[i]])
			{
				time++;
				i++;
				continue;
			}

			while (sta[j] != lis[i])
			{
				vis[sta[j]] = 1;
				j++;
			}

			time += ((j - i) * 2) + 1;
			i++;
		}

		cout << time << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;

		int n, m;
		cin >> n >> m;

		int sta[n], lis[m];
		bool vis[1000001];
		memset(vis, 0, sizeof vis);

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		int time = 0, i = 0, j = 0;
		while (i < m)
		{
			if (vis[lis[i]])
			{
				time++;
				i++;
				continue;
			}

			while (sta[j] != lis[i])
			{
				vis[sta[j]] = 1;
				j++;
			}

			time += ((j - i) * 2) + 1;
			i++;
		}

		cout << time << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;

		int n, m;
		cin >> n >> m;

		int sta[n], lis[m];
		bool vis[1000001];
		memset(vis, 0, sizeof vis);

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		int time = 0, i = 0, j = 0;
		while (i < m)
		{
			if (vis[lis[i]])
			{
				time++;
				i++;
				continue;
			}

			while (sta[j] != lis[i])
			{
				vis[sta[j]] = 1;
				j++;
			}

			time += ((j - i + 1) * 2) + 1;
			i++;
		}

		cout << time << endl;
	}

	return 0;
}

// int32_t main()
// {
// 	ios_base::sync_with_stdio(false);
// 	cin.tie(NULL);

// 	int T;
// 	cin >> T;
// 	while (T)
// 	{
// 		T--;
// 		int n, x;
// 		cin >> n >> x;

// 		int arr[n];
// 		for (int i = 0; i < n; i++)
// 			cin >> arr[i];

// 		int sum = 0;
// 		for (int i = 0; i < n; i++)
// 			sum += arr[i];

// 		if (sum <= x)
// 		{
// 			cout << 0 << endl;
// 			continue;
// 		}

// 		int i = 0, ma = 0;
// 		sum = 0;

// 		while (i < n)
// 		{
// 			if (sum + arr[i] > x + max(arr[ma], arr[i]))
// 				break;
// 			sum += arr[i];
// 			if (arr[ma] < arr[i])
// 				ma = i;
// 			i++;
// 		}

// 		cout << ma + 1 << endl;
// 	}

// 	return 0;
// }


#include <bits/stdc++.h>
using namespace std;

#define int long long

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;

		int n, m;
		cin >> n >> m;

		int sta[n], lis[m];
		bool vis[1000001];
		memset(vis, 0, sizeof vis);

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		int time = 0, i = 0, j = 0;
		while (i < m)
		{
			if (vis[lis[i]])
			{
				time++;
				i++;
				continue;
			}

			while (sta[j] != lis[i])
			{
				vis[sta[j]] = 1;
				j++;
			}

			time += ((j - i) * 2) + 1;
			i++;
		}

		cout << time << endl;
	}

	return 0;
}

// int32_t main()
// {
// 	ios_base::sync_with_stdio(false);
// 	cin.tie(NULL);

// 	int T;
// 	cin >> T;
// 	while (T)
// 	{
// 		T--;
// 		int n, x;
// 		cin >> n >> x;

// 		int arr[n];
// 		for (int i = 0; i < n; i++)
// 			cin >> arr[i];

// 		int sum = 0;
// 		for (int i = 0; i < n; i++)
// 			sum += arr[i];

// 		if (sum <= x)
// 		{
// 			cout << 0 << endl;
// 			continue;
// 		}

// 		int i = 0, ma = 0;
// 		sum = 0;

// 		while (i < n)
// 		{
// 			if (sum + arr[i] > x + max(arr[ma], arr[i]))
// 				break;
// 			sum += arr[i];
// 			if (arr[ma] < arr[i])
// 				ma = i;
// 			i++;
// 		}

// 		cout << ma + 1 << endl;
// 	}

// 	return 0;
// }


#include <bits/stdc++.h>
using namespace std;

#define MAX 100001
int sta[MAX];
int lis[MAX];
unordered_map<int, int> has;

int find(int el)
{
	int i = 0;
	while (sta[i] != el)
		i++;
	return i;
}

bool comp(int a, int b)
{
	int i1 = 1000000, i2 = 1000000;
	if (has.find(a) != has.end())
		i1 = has[a];
	if (has.find(b) != has.end())
		i2 = has[b];

	return i1 < i2;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;
	while (T)
	{
		T--;
		has.clear();
		int n, m;
		cin >> n >> m;

		for (int i = 0; i < n; i++)
			cin >> sta[i];
		for (int i = 0; i < m; i++)
			cin >> lis[i];

		for (int i = 0; i < m; i++)
			has[lis[i]] = i;

		int time = 0, i = 0;
		while (i < m)
		{
			int ind = find(lis[i]);
			time += (ind - i) * 2 + 1;
			sort(sta + i, sta + ind + 1, comp);
			i++;
		}

		cout << time << endl;
	}

	return 0;
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += (((pos[b[i]]- count) * 2) + 1);
                cur_pos = pos[b[i]]-count;
                count++;
            }
            else{
                ans+=1;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                if(cur_pos != -1){
                    ans += ((max((pos[b[i]]- cur_pos-1), 0) * 2) + 1);
                    cur_pos = pos[b[i]];
                }
                else
                {
                    ans += ((max((pos[b[i]]), 0) * 2) + 1);
                }

            }
            else{
                ans+=1;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += (pos[b[i]] - count)*2 + 1;
                cur_pos = pos[b[i]];
                count++;

            }
            else{
                ans+=1;
                count++;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += (((pos[b[i]]- count) * 2) + 1);
                cur_pos = pos[b[i]]-count;
                count++;
            }
            else{
                ans+=1;
                count++;
            }
        }
        cout << ans << endl;
    }
}