#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}
int main() {
  int t;
  cin >> t;
  vi n(t), m(t);
  vvi a(t), b(t);
  for (int i = 0; i < t; ++i) {
    cin >> n[i] >> m[i];
    a[i].resize(n[i], 0);
    b[i].resize(m[i], 0);
    for (int j = 0; j < n[i]; ++j) {
      cin >> a[i][j];
    }
    for (int j = 0; j < m[i]; ++j) {
      cin >> b[i][j];
    }
  }
  for (int i = 0; i < t; ++i) {
    vi pos(n[i] + 1, -1);
    for (int j = 0; j < n[i]; ++j) {
      pos[a[i][j]] = j;
    }
    int r = -1;
    ll ret = 0;
    for (int j = 0; j < m[i]; ++j) {
      if (r > pos[b[i][j]]) {
        ret++;
      } else {
        chmax(r, pos[b[i][j]]);
        ret += 2 * (pos[b[i][j]] - j) + 1;
      }
    }
    cout << ret << "\n";
  }
  return 0;
}
