这道题目是一道阅读理解题目,题意略繁琐。因为只能交换相邻的数字,所以问题简化了很多。先判定整个数列是不是 good。然后依次求出每个 $a_i, a_{i+1}$ 变动之后使得 $a_i > a_{i+1}$ 或者 $a_i + d < a_{i+1}$ 的代价,这都可以常数时间内计算出来,第二种情况需要判断可行性,移动 $a_i$ 或者 $a_{i+1}$ 是等价的,求出上界即可,并且这种情况也不需要考虑交叉的情况,$a_i$ 只能往左移动,$a_{i+1}$ 只能往右移动。

//  2023/11/21

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <functional>

using namespace std;

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1},
{1, 0}, {-1, 0},
{1, 1}, {1, -1},
{-1, 1}, {-1, -1},
};

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;

const ull Pr = 131;

#define LN ListNode
#define LNP ListNode*
#define TN TreeNode
#define TNP TreeNode*

#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])

#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nonempty(a) (!a.empty())
#define all(a) (a).begin(),(a).end()
#define SZ(a) int((a).size())

#ifdef DEBUG
#define debug1(x) cout << #x" = " << x << endl;
#define debug2(x,y) cout << #x" = " << x << " "#y" = " << y << endl;
#define debug3(x,y,z) cout << #x" = " << x << " "#y" = " << y << " "#z" = " << z << endl;
#else
#define debug1
#define debug2
#define debug3
#endif

#ifdef DEBUG

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int val) : val(val), next(nullptr) {}
    ListNode(int val, ListNode *next) : val(val), next(next) {}
};

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

#endif

const int N = 100010;
int t, n, a[N], p[N], m, d, pos[N];

int main(int argc, const char * argv[]) {
    #ifdef USE_INPUT_FILE
    freopen("input.txt", "r", stdin);
    #endif

    std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    cin >> t;
    while (t--) {
        cin >> n >> m >> d;
        
        For1(i, 1, n) {
            cin >> p[i];
            pos[p[i]] = i;
        }
        
        For1(i, 1, m) {
            cin >> a[i];
        }
        
        bool flag = true;
        For1(i, 1, m - 1) {
            int x = pos[a[i]], y = pos[a[i + 1]];
            if (x > y || x + d < y) {
                flag = false;
                break;
            }
        }
        
        if (!flag) {
            cout << 0 << '\n';
            continue;
        }
        
        int res = INF;
        
        For1(i, 1, m - 1) {
            int x = pos[a[i]], y = pos[a[i + 1]];
            int tmp1 = y - x, tmp2 = abs(x + d + 1 - y);
            res = min(res, tmp1);
            int top = x - 1 + n - y;
            if (top >= tmp2) {
                res = min(res, tmp2);
            }
        }
        
        cout << res << '\n';
    }

    return 0;
}