#include <cstdio>
#include <cstring>
const int N = 1000050;
int a[N], b[N], nxt[N];
int main()
{
    nxt[0] = -1;
    int T, n, m;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
            scanf("%d", a + i);
        for (int i = 0; i < m; i++)
            scanf("%d", b + i);
        for (int i = 1, j = -1; i < m; i++)
        {
            while (~j && b[j + 1] != b[i]) j = nxt[j];
            if (b[j + 1] == b[i]) j++;
            nxt[i] = j;
        }
        int ans = -1;
        for (int i = 0, j = -1; i < n && ans == -1; i++)
        {
            while (~j && b[j + 1] != a[i]) j = nxt[j];
            if (b[j + 1] == a[i]) j++;
            if (j == m - 1) ans = i - m + 2, j = nxt[j];
        }
        printf("%d\n", ans);
    }
    return 0;
}