1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define make_pair mp
#define emplace_back pb
#include <bits/stdc++.h>
using namespace std;
mt19937 mt_rand(time(0));
const int N = 1e6 + 5;
int n;

typedef vector<bool> Fib;

void normalize(Fib &f) {
	f.pb(0);
	f.pb(0);

	for(int i=f.size()-1;i>=1;i--) {
		if(f[i] == 1 && f[i-1] == 1) {
			f[i] = 0;
			f[i-1] = 0;
			f[i+1] = 1;
		}
	}

	for(int i=1;i<f.size();i++) {
		if(f[i] == 1 && f[i-1] == 1) {
			f[i] = 0;
			f[i-1] = 0;
			f[i+1] = 1;
		}
	}

	while(f.size() > 1 && f.back() == 0) f.pop_back();
}

Fib operator +(Fib &f1, Fib &f2) {
	vector<int> res;
	for(int i=0;i<max(f1.size(),f2.size());i++) {
		if(f1.size() <= i) res.pb(f2[i]);
		else if(f2.size() <= i) res.pb(f1[i]);
		else res.pb(f2[i] + f1[i]);
	}

	res.pb(0);
	res.pb(0);

	for(int i=res.size()-1;i>=2;i--) {
		if(res[i] == 3 && res[i-1] == 0) {
			res[i+1] = 1;
			res[i] = 1;
			res[i-2]++;
		}
		else if(res[i] == 2 && res[i-1] == 1) {
			res[i+1] = 1;
			res[i] = 1;
			res[i-1] = 0;
		}
		else if(res[i] == 1 && res[i-1] == 2) {
			res[i+1] = 1;
			res[i] = 0;
			res[i-1] = 1;
		}
		else if(res[i] == 2 && res[i-1] == 0) {
			res[i+1] = 1;
			res[i] = 0;
			res[i-2]++;
		}
	}

	/*for(auto x : res) cout<<x<<" ";
	cout<<endl;*/

	if(res[1] == 3) {
		res[0] = 1;
		res[1] = 1;
		res[2] = 1;
	}
	if(res[1] == 2 && res[0] == 1) {
		res[2] = 1;
		res[1] = 1;
		res[0] = 0;
	}
	if(res[1] == 1 && res[0] == 2) {
		res[2] = 1;
		res[1] = 0;
		res[0] = 1;
	}
	if(res[1] == 2 && res[0] == 0) {
		res[2] = 1;
		res[0] = 1;
		res[1] = 0;
	}
	if(res[0] == 2) {
		res[1] = 1;
		res[0] = 0;
	}
	if(res[0] == 3) {
		res[1] = 1;
		res[0] = 1;
	}

	vector<bool> v;
	for(int i=0;i<res.size();i++) v.pb((bool)res[i]);

	normalize(v);
	return v;
}

Fib z[N];

Fib one_bit_mult(Fib &f1, Fib &f2) {
	int n = f1.size() + 1;
	int m = f2.size() + 1;
	if(n < m) return one_bit_mult(f2, f1);
	Fib res(n + m - 3);
	int siz = n + m - 4;
	if(m % 2 == 0) {
		for(int i=siz;i >= siz - 2 * m + 4;i -= 4) res[i] = 1;
	}
	else if(n == m) {
		for(int i=siz;i >= siz - 2 * m + 6;i -= 4)  res[i] = 1;
		res[siz - 2 * m + 4] = 1;
	}
	else {
		for(int i=siz;i >= siz - 2 * m + 6;i -= 4) res[i] = 1;
		res[siz - 2 * m + 3] = 1;
	}
	return res;
}

Fib operator *(Fib &f1, Fib &f2) {
	Fib ans;
	for(int i=0;i<f2.size();i++) {
		if(i == 0) z[0] = f1;
		else if(i == 1) z[1] = f1 + f1;
		else z[i] = z[i-1] + z[i-2];
		if(f2[i]) ans = ans + z[i];

	}

	return ans;
}

int t;

int main()
{
	scanf("%d", &t);
	while(t--) {
		int n, m, x;
		int cnt11 = 0, cnt12 = 0;
		scanf("%d", &n);
		Fib v, u;
		while(n--) {
			scanf("%d", &x);
			v.pb(x);
			cnt11 += x;
		}
		scanf("%d", &m);
		while(m--) {
			scanf("%d", &x);
			u.pb(x);
			cnt12 += x;
		}

		Fib res;
		if(cnt11 == 1 && cnt12 == 1) res = one_bit_mult(v, u);
		else res = v * u;
		printf("%d ", (int)res.size());
		for(auto x : res) printf("%d ", (int)x);
		printf("\n");
	}
	return 0;
}