import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void solve() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(x => x.to!int-1).array;
    auto B = readln.split.map!(x => x.to!int-1).array;
    auto C = new int[](N);
    foreach (i; 0..N) C[A[i]] = i;
    long ans = 0;
    int mx = -1;
    long cnt = 0;

    foreach (b; B) {
        if (C[b] < mx) {
            ans += 1;
        } else {
            mx = C[b];
            ans += (C[b] - cnt) * 2 + 1;
        }
        cnt += 1;
    }

    ans.writeln;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) solve;
}