#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int a[n], b[m];
    for (int i = 0; i < n; i++) cin >> a[i];
    int cur[n + 1];
    memset(cur, -1, sizeof(cur));
    int idx = 0;
    long long int ans = 0, prev = 0;
    for (int i = 0; i < m; i++) {
      int k;
      cin >> k;
      if (cur[k]) {
        ans += (prev * 2);
        while (!(a[idx] == k)) cur[a[idx]] = 0, idx++, ans += 2, prev++;
        idx++;
        ans++;
      } else {
        ans++;
        prev--;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
