#include <bits/stdc++.h>
#include "message.h"
using namespace std;
#ifdef lisie_bimbi
#else
#define endl '\n'
#endif
typedef long long ll;
const ll inf = 1'000'000'000;
#ifdef lisie_bimbi
const int PACKET_SIZE = 31;
const int CALLS_CNT_LIMIT = 100;
int calls_cnt;
std::vector<bool> C(PACKET_SIZE);
std::vector<std::vector<bool>> R;
void quit(const char* message) {
printf("%s\n", message);
exit(0);
}
std::vector<bool> taint(const std::vector<bool>& A) {
std::vector<bool> B = A;
bool bit = 0;
for (int i = 0; i < PACKET_SIZE; i++)
if (C[i] == 1) {
B[i] = bit;
bit = !bit;
}
return B;
}
std::vector<bool> send_packet(std::vector<bool> A) {
calls_cnt++;
if (calls_cnt > CALLS_CNT_LIMIT)
quit("Too many calls");
if ((int)A.size() != PACKET_SIZE)
quit("Invalid argument");
std::vector<bool> B = taint(A);
R.push_back(B);
return B;
}
void run_scenario() {
R.clear();
calls_cnt = 0;
int S;
assert(1 == scanf("%d", &S));
std::vector<bool> M(S);
for (int i = 0; i < S; i++) {
int bit;
assert(1 == scanf("%d", &bit));
assert((bit == 0) || (bit == 1));
M[i] = bit;
}
for (int i = 0; i < PACKET_SIZE; i++) {
int bit;
assert(1 == scanf("%d", &bit));
assert((bit == 0) || (bit == 1));
C[i] = bit;
}
send_message(M, C);
std::vector<bool> D = receive_message(R);
int K = (int)R.size();
int L = (int)D.size();
//printf("%d %d\n", K, L);
for (int i = 0; i < L; i++)
printf("%s%d", (i == 0 ? "" : " "), (D[i] ? 1 : 0));
printf("\n");
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T;
assert(1 == scanf("%d", &T));
for (int i = 1; i <= T; i++)
run_scenario();
}
#endif
void send_message(vector<bool> m, vector<bool> c){
mt19937 rnd(239);
for(int i = 0; i <= 30; i++){
c[i] = !c[i];
}
int n = m.size();
int cnt = 31;
vector<bool> t(31, 1);
vector<int> z;
for(int i = 0; i <= 30; i++){
if(c[i]){
z.push_back(i);
}
}
while(cnt > 16){
vector<bool> s(31);
ll x = rnd();
for(int i = 0; i <= 30; i++){
if(c[i] && ((x >> i) & 1)){
s[i] = 1;
}
}
vector<bool> s1 = send_packet(s);
for(int i = 0; i <= 30; i++){
if((x >> i) & 1){
s[i] = 1;
}
}
for(int i = 0; i <= 30; i++){
if(s[i] != s1[i]){
cnt -= t[i];
t[i] = 0;
}
}
}
vector<bool> m1;
for(int i = 0; i <= 10; i++){
m1.push_back((n >> i) & 1);
}
for(auto i : m){
m1.push_back(i);
}
while(m1.size() % 16 != 0){
m1.push_back(0);
}
for(int i = 0; i < m1.size(); i += 16){
vector<bool> s(31);
for(int j = 0; j < 16; j++){
s[z[j]] = m1[i + j];
}
send_packet(s);
}
}
vector<bool> receive_message(vector<vector<bool>> r){
mt19937 rnd(239);
vector<int> c(31, 1);
reverse(r.begin(), r.end());
int cnt = 31;
while(cnt > 16){
vector<bool> s(31);
ll x = rnd();
for(int i = 0; i <= 30; i++){
if(((x >> i) & 1)){
s[i] = 1;
}
}
vector<bool> s1 = r.back();
r.pop_back();
for(int i = 0; i <= 30; i++){
if(s[i] != s1[i]){
cnt -= c[i];
c[i] = 0;
}
}
}
vector<int> z;
for(int i = 0; i <= 30; i++){
if(c[i]){
z.push_back(i);
}
}
vector<bool> m1;
while(!r.empty()){
for(int i = 0; i <= 15; i++){
m1.push_back(r.back()[z[i]]);
}
r.pop_back();
}
int n = 0;
for(int i = 0; i <= 10; i++){
n += (m1[i] << i);
}
vector<bool> m;
for(int i = 11; i < 11 + n; i++){
m.push_back(m1[i]);
}
return m;
}