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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void Fix(vector<bool>& v)
{
	for (int i = v.size() - 1; i >= 1; i--)
	{
		if ((v[i] == 1) && v[i - 1] == 1)
		{
			v[i] = v[i - 1] = 0;
			if (i == v.size() - 1)
				v.push_back(1);
			else
				v[i + 1] = 1;
			if (i + 3 >= v.size())
				i = v.size();
			else
				i += 3;
		}
	}
	
	if (v.size() == 0)
		v.push_back(0);
}

vector<bool> Add(const vector<bool>& v1, const vector<bool>& v2)
{
	int i;
	bool b1, b2;
	vector<bool> sum, f2(2), f3(3), f4(4);
	
	f2[0] = 0; f3[0] = 1; f4[0] = 1;
	f2[1] = 1; f3[1] = 0; f4[1] = 0;
	f3[2] = 1; f4[2] = 0; 
	f4[3] = 1;
	
	for (i = 0; (i < v1.size() || (i < v2.size())); i++)
	{
		if (i < v1.size())
			b1 = v1[i];
		else
			b1 = 0;
		if (i < v2.size())
			b2 = v2[i];
		else
			b2 = 0;
		if (b1 ^ b2)
			sum.push_back(1);
		else
			sum.push_back(0);
	}

	Fix(sum);

	for (i = 0; (i < v1.size() && (i < v2.size())); i++)
	{
		if (v1[i] & v2[i])
		{
			if (i == 0) 
				sum = Add(sum, f2);
			else if (i == 1) 
				sum = Add(sum, f3);
			else 
				sum = Add(sum, f4);
		}
		
		if (i > 1) 
			f4.insert(f4.begin(), 1, 0);
	}
	return sum;
}

vector<bool> Multiply(const vector<bool>& v1, const vector<bool>& v2)
{
	vector<bool> f0, f1(v2), f2(v2), sum;
	for (int i = 0; i < v1.size(); i++)
	{
		if (v1[i] == 1) 
			sum = Add(sum, f2);
		f0 = f1; f1 = f2; f2 = Add(f0, f1);
	}
	return sum;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int t, i;
	cin >> t;
	vector<bool> mul_res;

	for (int j = 0; j < t; j++)
	{
		int x, y;
		bool temp;

		cin >> x;
		vector<bool>v1(x);

		for (i = 0; i < x; i++)
		{
			cin >> temp;
			v1[i] = temp;
		}

		cin >> y;
		vector<bool>v2(y);

		for (i = 0; i < y; i++)
		{
			cin >> temp;
			v2[i] = temp;
		}

		//reverse(str1.begin(), str1.end());
		//reverse(str2.begin(), str2.end());

		

		//for (int i = 0; i < x; i++)
		//	cout << v1[i] << " ";
		//cout << endl;

		//for (int i = 0; i < y; i++)
		//	cout << v2[i] << " ";
		//cout << endl;

		mul_res = Multiply(v1, v2);
		cout << mul_res.size() << " ";
		for (i = 0; i < mul_res.size(); i++)
			cout << mul_res[i] << " ";
		cout << endl;
	}
}