#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 1000000
int next[maxn + 2];
int numa[maxn + 10];
int numb[maxn + 10];
int n,m;
void fan( int numb[])
{
int len = n;
next[1] = 0;
for( int i = 2 ,j = 0;i <= len; i++)
{
while( j > 0 && numb[j+1] != numb[i])
j = next[j];
if( numb[j+1] == numb[i])
j ++;
next[i] = j;
}
}
int kmp(int numa[], int numb[])
{
int len1 = n;
int len2 = m;
fan(numb);
for( int i = 0, j = 1; j<= len1; j++)
{
while( i > 0 && numb[ i + 1] != numa[j])
i = next[i];
if( numb[ i +1] == numa[j])
i++;
if( i == len2)
{
return j-len2+1;
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n, &m);
for( int k = 1; k <= n; k++)
cin>>numa[k];
for( int k = 1; k<= m; k++)
cin>>numb[k];
cout<<kmp(numa,numb)<<endl;
}
}