import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
public class Main
{
static HashMap
<Integer,Integer
> index
;static int g
=0; static int l
=0;
public static int lower_bound(int start,int end,int a[],int key)
{
if(start>end) return start;
int mid=(start+end)>>1;
if(a[mid]==key) return mid;
if(key<a[mid]) return lower_bound(start,mid-1,a,key);
else return lower_bound(mid+1,end,a,key);
}
{
fast s = new fast();
StringBuilder final_ans = new StringBuilder();
int t=s.nextInt();
while(t!=0)
{
int n=s.nextInt();
int st=s.nextInt();
index
=new HashMap
<Integer,Integer
>();
int a[]=new int[n];
int b[]=new int[st];
int temp[]=new int[n];
for(int i=0;i<n;i++) {a[i]=s.nextInt();temp[i]=a[i];index.put(a[i], i);}
for(int i=0;i<st;i++) b[i]=s.nextInt();
for(int i=0;i<st;i++)
{
l=0; g=0;
int pos=index.get(b[i]);
int low=0; int high=n-1;
while(low<=high)
{
int mid=(low+high)>>1;
if(mid==pos) break;
if(mid<pos)
{
if(a[mid]>b[i]) g++;
low=mid+1;
}
else if(mid>pos)
{
if(a[mid]<b[i]) l++;
high=mid-1;
}
}
int lb=lower_bound(0,n-1,temp,b[i]);
int gb=n-1-lb;
if(l
==g
) System.
out.
println(l
); else if(l>g)
{
if(gb
<l
) System.
out.
println("-1");
}
else
{
if(lb
<g
) System.
out.
println("-1"); }
}
t--;
}
}
static class fast {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int gcd(int a,int b)
{
if(a==0) return b;
return gcd(b%a,b);
}
public fast() {
}
i = is;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = i.read(buf);
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEubWF0aC5CaWdJbnRlZ2VyOwppbXBvcnQgamF2YS51dGlsLio7CmltcG9ydCBqYXZhLnV0aWwucmVnZXguTWF0Y2hlcjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC5QYXR0ZXJuOwoKaW1wb3J0IGphdmF4Lm5ldC5zc2wuU1NMQ29udGV4dDsKCnB1YmxpYyBjbGFzcyBNYWluCnsJCiAgIHN0YXRpYyBIYXNoTWFwPEludGVnZXIsSW50ZWdlcj4gaW5kZXg7c3RhdGljIGludCBnPTA7IHN0YXRpYyBpbnQgbD0wOwogICAKICAgcHVibGljIHN0YXRpYyBpbnQgbG93ZXJfYm91bmQoaW50IHN0YXJ0LGludCBlbmQsaW50IGFbXSxpbnQga2V5KQogICB7CgkgICBpZihzdGFydD5lbmQpIHJldHVybiBzdGFydDsKCSAgIGludCBtaWQ9KHN0YXJ0K2VuZCk+PjE7CgkgICAKCSAgIGlmKGFbbWlkXT09a2V5KSByZXR1cm4gbWlkOwoJICAgCgkgICBpZihrZXk8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