#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
struct edge {
  long long to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int q;
  cin >> q;
  while (q-- != 0) {
    long long n, m;
    cin >> n >> m;
    vector<long long> da(n);
    for (long long i = 0; i < n; i++) cin >> da[i];
    long long p = 0;
    long long ans = 0;
    unordered_set<long long> us;
    for (long long i = 0; i < m; i++) {
      int t;
      cin >> t;
      if (us.count(t)) {
        ans += 1;
        continue;
      }
      while (da[p] != t) us.insert(da[p++]);
      ans += (p - i + 1) * 2 - 1;
    }
    cout << ans << endl;
  }
}
