#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t, n, m;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    queue<long long> q;
    map<long long, long long> s;
    long long b[m], x;
    for (long long i = 0; i < n; i++) cin >> x, q.push(x);
    long long ans = 0;
    for (long long i = 0; i < m; i++) {
      cin >> b[i];
      auto it = s.lower_bound(b[i]);
      if (it != s.end() && it->first == b[i])
        ans += 1, s.erase(b[i]);
      else {
        long long k = 0;
        k += s.size();
        while (q.front() != b[i] && !q.empty()) s[q.front()] = 1, q.pop(), k++;
        ans += (2 * k + 1);
        q.pop();
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
