#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n, m, x;
  cin >> t;
  while (t--) {
    cin >> m >> n;
    long long a[100005] = {0};
    long long st[m];
    for (int i = 0; i < m; i++) cin >> st[i];
    int k = 0, p = 0;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> x;
      if (a[x]) {
        k--;
        ans++;
      } else {
        while (st[p] != x) {
          k++;
          a[st[p]] = 1;
          p++;
        }
        a[x] = 1;
        ans += 2 * k + 1;
        k--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
