#include <bits/stdc++.h>
using namespace std;
#define ll long long

/*
    ALTERNATING OPERATIONS:
    op[1] => odd => delete from front
    op[2] => even => delete from back
    op[3] => odd => delete from front
    .
    .
    .
    op[n] => delete last element from arr

    element closest to center of arr is the one that requires the most operations to delete => ans

    EXAMPLE:
    8 3
    1 2 3 4 5 5 5 6
    2 5 6
    
    middle at indices 3,4
    2 => at index 1 => dist = 3-1 = 2
    5 => at index 4 => dist = 4-4 = 0
    6 => at index 7 => dist = 7-4 = 3
    element 5 is closest to center at index 4
    
    OPERATIONS:
    1 => remove 1 & 6
    2 => remove 2 & 5
    3 => remove 3 & 5
    4 => remove 4 & 5
    total elements removed = 8

    Simulate using two pointers

    OR:

    for alternating operations:
    
    FROM LEFT:
    element at index i = 0, deletion requires 1 operation
    element at index i = 1, deletion requires 3 operations => 0, n-1, 1
    element at index i = 2, deletion requires 5 operations => 0, n-1, 1, n-2, 2
    .
    .
    .
    deleting from left: 2*(i+1)-1

    FROM RIGHT:
    element at index i = n-1, deletion requires 2 operations
    element at index i = n-2, deletion requires 4 operations => 0, n-1, 1, n-2
    element at index i = n-3, deletion requires 6 operations => 0, n-1, 1, n-2, 2, n-3
    .
    .
    .
    deleting from right: 2*(n-i)
*/

void mohemmat() {
    int n, m; cin >> n >> m;
    vector<int> a(n);
    unordered_set<int> b;
    
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 0; i < m; i++) {
        int x; cin >> x;
        b.insert(x);
    }

    // FROM CLAUDE
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (b.count(a[i])) {
            int cost = min(2*(i+1)-1, 2*(n-i));
            /*
                0-index i:
                COST OF DELETION FROM LEFT: 2*(i+1)-1
                COST OF DELETION FROM RIGHT: 2*(n-i)
            */
            ans = max(ans, cost);
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--) {mohemmat();}

    return 0;
}