Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.

Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.

To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).

What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.

Your program has to answer $t$ different test cases.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases.

Then the test cases follow, each represented by three lines.

The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) — the number of presents in the stack and the number of presents Santa wants to send, respectively.

The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) — the order of presents in the stack.

The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) — the ordered list of presents Santa has to send.

The sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.


-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1

Output
5
8

Starter code:

Solutions:
for tc in range(int(input())):
    n,m = list(map(int, input().split()))
    al = list(map(int, input().split()))
    bl = list(map(int, input().split()))
    aidx = {}
    for i,e in enumerate(al):
        aidx[e]=i
    midx = -1
    res = 0
    for i,e in enumerate(bl):
        idx = aidx[e]
        if idx <= midx:
            res += 1
        else:
            res += 2*(idx-i)+1
        midx = max(midx, idx)
    print(res)


for q11 in range(int(input())):
    n, m = list(map(int, input().split()))
    a = [int(q)-1 for q in input().split()]
    s = [int(q)-1 for q in input().split()]
    d = [0]*n
    for q in range(n):
        d[a[q]] = q
    max1, ans = -1, 0
    for q in range(m):
        if d[s[q]] > max1:
            ans += 2*(d[s[q]]-q)+1
            max1 = d[s[q]]
        else:
            ans += 1
    print(ans)


t = int(input())

for _ in range(t):
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    b = list(map(int, input().split(' ')))

    order = [0] * (n + 1)
    for i in range(n):
        order[a[i]] = i

    pref_max = 0
    ans = 0
    for i in range(m):
        if order[b[i]] < pref_max:
            ans += 1
        else:
            pref_max = order[b[i]]
            ans += 2 * (order[b[i]] - i) + 1

    print(ans)

T = int(input())
for t in range(T):
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    for i in range(n):
        a[i] -= 1
    for j in range(m):
        b[j] -= 1
    
    nummap = [0 for i in range(n)]
    for i in range(n):
        nummap[a[i]] = i
    b = [nummap[b[i]] for i in range(m)]
    
    largest = -1
    res = 0
    for i in range(m):
        if b[i] >= largest:
            res += 2*(b[i]-i) + 1
            largest = b[i]
        else:
            res += 1
    print(res)

from math import *
import os, sys
from bisect import *
from io import BytesIO

#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.setrecursionlimit(10 ** 9)
#sys.stdin = open("moobuzz.in", 'r')
#sys.stdout = open("moobuzz.out", 'w')

for i in range(int(input())):
	n, m = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		d[a[i]] = i
	
	ans = 0
	mx = 0
	for i in range(m):
		if mx < d[b[i]]:
			ans += 2 * (d[b[i]] - i) + 1
			mx = d[b[i]]
		else:
			ans += 1
	print(ans)


T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    A = [int(a)-1 for a in input().split()]
    B = [int(a)-1 for a in input().split()]
    
    X = [0] * N
    for i, a in enumerate(A):
        X[a] = i
    ans = 0
    ma = -1
    for i, b in enumerate(B):
        ans += (X[b] - i) * 2 + 1 if X[b] > ma else 1
        ma = max(ma, X[b])
    print(ans)

import sys
input = sys.stdin.readline

t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    memo = {}
    for i in range(n):
        memo[a[i]] = i
    
    max_num = -1
    cnt = 0
    ans = 0
    for i in range(m):
        if max_num < memo[b[i]]:
            ans += 2 * (memo[b[i]] - cnt) + 1
            max_num = memo[b[i]]
            cnt += 1
        else:
            ans += 1
            cnt += 1
    print(ans)

import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
    n,m=list(map(int,input().split()))
    aa=list(map(int,input().split()))
    bb=list(map(int,input().split()))
    ans=0
    ind=0
    vis=[0 for i in range(n)]
    co=0
    for i in range(m):
        if vis[bb[i]-1]==1:
            ans+=1
            co-=1
            continue
        while ind<n:
            co+=1
            if aa[ind]==bb[i]:
                vis[aa[ind]-1]=1
                ind+=1
                break
            else:
                
                vis[aa[ind]-1]=1
                ind+=1
        co-=1
        ans+=co*2+1
       # print(ans,ind)
        
    print(ans)
   # print()


q = int(input())
t = 1
vis = [0] * 300000
for i in range(q):
    n, m = [int(x) for x in input().split()]
    a = [ int(x) for x in input().split()]
    b =  [int(x) for x in input().split()]
    arr = 0
    i = 0
    k = 0
    for item in b:
        if vis[item] == t:
            arr += 1
            k -= 1
            continue
        while i < n:
            vis[a[i]] = t
            if a[i] == item:
                arr += (2 * k) + 1
                i += 1
                break
            i += 1
            k += 1
    print(arr)
    t += 1
        


for _ in range(int(input())):
    n, m = list(map(int, input().split()))
    topmost = set()
    a = iter(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    for bi in b:
        if bi in topmost:
            k = 0
            topmost.remove(bi)
        else:
            k = len(topmost)
            for ai in a:
                if ai == bi:
                    break
                topmost.add(ai)
                k += 1
            else:
                raise ValueError(f'No {bi} in a')
        ans += 2 * k + 1
    print(ans)


import sys
readline = sys.stdin.readline

T = int(readline())

Ans = [None]*T

for qu in range(T):
    N, M = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    A.reverse()
    
    res = 0
    seen = set()
    for b in B:
        res += 1
        if b in seen: 
            seen.remove(b)
            continue
        res += 2*len(seen)
        while A[-1] != b:
            seen.add(A.pop())
            res += 2
        A.pop()
    Ans[qu] = res
print('\n'.join(map(str, Ans)))

import sys

# inf = open('input.txt', 'r')
# reader = (map(int, line.split()) for line in inf)
reader = (list(map(int, line.split())) for line in sys.stdin)
input = reader.__next__

t, = input()
for _ in range(t):
    n, m = input()
    a = list(input())
    b = list(input())
    d = {el:i for i, el in enumerate(a)}
    maxPos = d[b[0]]
    ans = 2 * maxPos + 1
    Nremoved = 1
    for el in b[1:]:
        pos = d[el]
        if pos < maxPos:
            ans += 1
        else:
            ans += 2 * (pos - Nremoved) + 1
            maxPos = pos
        Nremoved += 1
    print(ans)
        
    
    
# inf.close()