#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline void INP();
const long long N = 3e6 + 7;
const long long INF = LLONG_MAX;
const long long inf = INT_MAX;
long long n, Q;
long long arr[N], vis[N], brr[N];
void solve() {
  long long m;
  cin >> n >> m;
  for (long long i = 0; i < (long long)n; i++) cin >> arr[i], vis[arr[i]] = 0;
  for (long long i = 0; i < (long long)m; i++) cin >> brr[i];
  long long ans = 0, s = 0, above = 0;
  for (long long i = 0; i < (long long)m; i++) {
    if (vis[brr[i]]) {
      ans++;
      above--;
    } else {
      long long cnt = above;
      for (long long j = s; j < (long long)n; j++) {
        vis[arr[j]] = 1;
        if (arr[j] != brr[i]) {
          cnt++;
          above++;
        } else {
          ans += 2 * cnt + 1;
          s = j + 1;
          break;
        }
      }
    }
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  INP();
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
inline void INP() {}
