#include <bits/stdc++.h>
using namespace std;
int t;
const int maxn = 1e5 + 5;
int n, m, b[maxn], a[maxn];
stack<int> stk;
bool use[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  while (!stk.empty()) stk.pop();
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), use[a[i]] = false;
  for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
  for (int i = n; i >= 1; i--) stk.push(a[i]);
  long long ans = 0;
  long long pre = 0;
  for (int i = 1; i <= m; i++) {
    if (use[b[i]])
      ans++, pre--;
    else {
      while (stk.top() != b[i]) {
        pre++;
        use[stk.top()] = true;
        stk.pop();
      }
      stk.pop();
      ans += (pre)*2ll + 1ll;
    }
  }
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
