#include <bits/stdc++.h>
using namespace std;
int a[(int)1e5 + 10];
int b[(int)1e5 + 10];
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    long long sum = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      a[tmp] = i;
    }
    for (int i = 1; i <= m; i++) {
      cin >> b[i];
      b[i] = a[b[i]];
    }
    int bi = 1;
    while (bi <= m) {
      int indb = b[bi];
      sum += 2 * (b[bi] - bi) + 1;
      while ((++bi) <= m && b[bi] <= indb) sum++;
    }
    cout << sum << endl;
  }
  return 0;
}
