#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<math.h>
#include<map>
#include <unordered_map>
#include<vector>
#include<queue>
#include<set>
#include <bits/stdc++.h>
#include<deque>
#include<string>
#define N 40
#define K 100005
#define MOD 1e9+7
#define ll long long int
using namespace std;
ll max(ll a, ll b){
if(a>b)
return a;
return b;
}
ll min(ll a, ll b){
if(a<b)
return a;
return b;
}
const int ALPHABET_SIZE = 2;
struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
// isEndOfWord is true if the node represents
// end of a word
int index_of_no;
};
struct TrieNode *getNode(void)
{
struct TrieNode *pNode = new TrieNode;
pNode->index_of_no = 0;
for (int i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
return pNode;
}
void insert(struct TrieNode *root, string key, int pos)
{
struct TrieNode *pCrawl = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - '0';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
if(pCrawl->index_of_no!= 0){
pCrawl->index_of_no = min(pCrawl->index_of_no, pos);
}
else
pCrawl->index_of_no = pos;
}
string get_32_bit(int val){
int lim = 32;
char bin[lim+1];
bin[lim] = '\0';
for(int i=0;i<lim;i++){
bin[i] = '0';
}
int i = 0;
while(val >0){
int rem = val%2;
bin[lim-1-i] = rem + '0';
i++;
val/=2;
}
return bin;
}
int search(struct TrieNode *root, string key)
{
struct TrieNode *pCrawl = root;
//cout<<key.length()<<endl;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - '0';
if(!pCrawl->children[index]){
if(pCrawl->children[0] != NULL){
pCrawl = pCrawl->children[0];
//cout<<i<<" 00"<<endl;
}
else if(pCrawl->children[1] != NULL){
pCrawl = pCrawl->children[1];
//cout<<i<<" 11"<<endl;
}
/*
else{
cout<<i<<" return from here"<<endl;
return pCrawl->index_of_no;
}*/
}
else{
pCrawl = pCrawl->children[index];
//cout<<i<<" match"<<endl;
}
}
return pCrawl->index_of_no;
}
string comple(string s){
char inv[33];
for(int i=0;i<s.length();i++){
if(s[i] == '0')
inv[i] = '1';
else
inv[i] = '0';
}
inv[32] = '\0';
return inv;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n, q;
cin>>n>>q;
int x[q];
int nos[n];
for(int i=0;i<n;i++){
cin>>nos[i];
}
for(int i=0;i<q;i++){
cin>>x[i];
}
struct TrieNode *root = getNode();
for(int i=0;i<n;i++){
int val = nos[i];
string s = get_32_bit(val);
//cout<<i<<" "<<s<<endl;
insert(root, s, i+1);
}
string req;
for(int i=0;i<q;i++){
string pres = get_32_bit(x[i]);
string req = comple(pres);
//cout<<"$ "<<req<<endl;
int matching_pos = search(root, req);
cout<<matching_pos<<endl;
}
}
}