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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll pair <ll, ll>
#define ppi pair <pii, pii>
#define ppii pair <pii, pii>
#define ppli pair <pli, pli>
#define ppll pair <pli, pll>
using namespace std;

const ll MIN_INF = -(1ll<<61);

template <typename T>
ostream & operator << (ostream &out, const vector <T> &V) {
	if (!V.empty()) {
		out << "{";
		for (auto v : V)
			out << v << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <class T1, class T2>
ostream & operator << (ostream &out, const pair <T1, T2> &P) {
	out << "{" << P.first << ", " << P.second << "}";
	return out;
}

struct big {
    int s;
    int *a;
    big(int s, vector <int> &A) : s(s) {
        a = (int*) calloc(s, sizeof(int));
        for (int i = 0; i < s; i++) {
            a[i] = A[i];
        }
    }
    big(int s) : s(s) {
        a = (int*) calloc(s, sizeof(int));
    }
};


string my_add(string &a, string &b) {
    string c;
    if(a.size()<b.size())
        swap(a,b);
    //reverse(a.begin(),a.end());
    //reverse(b.begin(),b.end());
    int more=0; int b_size=b.size();                                //more-carrying
    for(int i=0; i<b.size(); i++)
        {
            c.push_back(((a[i]-'0')+(b[i]-'0')+more)%10+'0');
            more=((a[i]-'0')+(b[i]-'0')+more)/10;
        }
    for(int i=b_size; i<a.size(); i++)
        {
            c.push_back(((a[i]-'0')+more)%10+'0');
            more=((a[i]-'0')+more)/10;
        }
    if(more!=0)
        c.push_back('1');
    //reverse(c.begin(),c.end());
    //cout << "add " << a << " " << b << " | " << c <<endl;
    return c;
}

// a >= b
string my_sub(string &a, string &b) {
    if (a == b) {
        return "0";
    }
    string c;
    //reverse(a.begin(),a.end());
    //reverse(b.begin(),b.end());
    int more=0; int b_size=b.size();                                //more-carrying
    for(int i=0; i<b.size(); i++)
        {
            c.push_back(((a[i]-'0')-(b[i]-'0')-more+10)%10+'0');
            more=((a[i]-'0')-(b[i]-'0')-more) < 0 ? 1 : 0;
        }
    for(int i=b_size; i<a.size(); i++)
        {
            c.push_back(((a[i]-'0')-more+10)%10+'0');
            more=((a[i]-'0')-more) < 0 ? 1 : 0;
        }
    int s = c.size();
    while (s > 0 && c[s-1] == '0') {
        c.pop_back();
        s--;
    }
    //reverse(c.begin(),c.end());
    //cout << "sub " << a << " " << b << " | " << c <<endl;
    return c;
}


string my_multiplication(string &a, string &b)
{
    if(a=="0" || b=="0")
        return "0";
    else
        {
            vector <int> a1; vector <int> b1;
            for(int i=a.size()-1; i>=0; )
                {
                    int j=i; int c=0; int f=1;
                    while(j>=0 && a[j]!='-' && i-j<=5)
                        {
                            c=c+(a[j]-'0')*f;
                            f=f*10;
                            j--;
                        }
                    if(i!=0 || a[i]!='-')
                        a1.push_back(c);
                    i=i-6;
                }
            for(int i=b.size()-1; i>=0; )
                {
                    int j=i; int c=0; int f=1;
                    while(j>=0 && b[j]!='-' && i-j<=5)
                        {
                            c=c+(b[j]-'0')*f;
                            f=f*10;
                            j--;
                        }
                    if(i!=0 || b[i]!='-')
                        b1.push_back(c);
                    i=i-6;
                }

            /*cout << positive <<endl;
            for(int i=0; i<a1.size(); i++)
                cout << a1[i] << " ";
            cout <<endl;
            for(int i=0; i<b1.size(); i++)
                cout << b1[i] << " ";
            cout <<endl;*/

            vector <long long> Answer; long long carrying=0;
            for(int i=0; i<a1.size()+b1.size()-1; i++)
               {
                   long long c=0; int q=i+1-b1.size(); int r=a1.size();
                   for(int j=max(0,q); j<min(i+1,r); j++)
                       c=c+1ll*a1[j]*b1[i-j];
                   Answer.push_back((c+carrying)%1000000);
                   carrying=(c+carrying)/1000000;
               }

            if(carrying>0)
                Answer.push_back(carrying);
                
            string c;

            c += to_string(Answer[Answer.size()-1]);

            for(int i=Answer.size()-2; i>=0; i--)
                {
                    if(Answer[i]<10)
                        c += "00000" + to_string(Answer[i]);
                    else
                    if(Answer[i]>=10 && Answer[i]<100)
                        c += "0000" + to_string(Answer[i]);
                    else
                    if(Answer[i]>=100 && Answer[i]<1000)
                        c += "000" + to_string(Answer[i]);
                    else
                    if(Answer[i]>=1000 && Answer[i]<10000)
                        c += "00" + to_string(Answer[i]);
                    else
                    if(Answer[i]>=10000 && Answer[i]<100000)
                        c += "0" + to_string(Answer[i]);
                    else
                    if(Answer[i]>=100000)
                        c += to_string(Answer[i]);
                }
            reverse(c.begin(), c.end());
            return c;
        }
}

// checks if a >= b
bool my_comp(string &a, string &b) {
    int n = a.size(), m = b.size();
    if (n != m) {
        return n > m;
    }
    for (int i = n-1; i >= 0; i--) {
        if (a[i] != b[i]) {
            return a[i] > b[i];
        }
    }
    return true;
}

void testcase_ilo() {
    int n, m;
    cin >> n;
    int s = n, s_a = 0, s_b = 0; vector <int> A(n, 0);
    vector <string> F;
    F.push_back("1");
    F.push_back("2");
    for (int i = 2; i <= n; i++) {
        F.push_back(my_add(F[i-1], F[i-2]));
    }
    //cout << F <<endl;
    string a = "0", b = "0";
    for (int i = 0; i < n; i++) {
        cin >> A[i];
        s_a += A[i];
        if (A[i] == 1) {
            a = my_add(a, F[i]);
        }
    }
    cin >> m;
    s = max(max(n, m) + 1, 3);
    vector <int> B(m, 0);
    for (int i = n+1; i < s; i++) {
         F.push_back(my_add(F[i-1], F[i-2]));
    }
    for (int j = 0; j < m; j++) {
        cin >> B[j];
        s_b += B[j];
        if (B[j] == 1) {
            b = my_add(b, F[j]);
        }
    }
    //cout << a << "  " << b <<endl;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    string c = my_multiplication(a, b);
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for (int i = s; F[i-1].size() <= c.size() + 1; i++) {
        F.push_back(my_add(F[i-1], F[i-2]));
    }
    //cout << F <<endl;
    vector <int> Ans;
    for (int i = F.size()-1; i >= 0; i--) {
        if (my_comp(c, F[i])) {
            Ans.push_back(1);
            c = my_sub(c, F[i]);
            //cout << i << endl;
        }
        else {
            Ans.push_back(0);
        }
    }
    
    reverse(Ans.begin(), Ans.end());
    s = Ans.size();
    while (s > 0 && Ans[s-1] == 0) {
        Ans.pop_back();
        s--;
    }
    cout << Ans.size() << " ";
    for (int i = 0; i < s; i++) {
        cout << Ans[i] << " ";
    }
    cout <<endl;
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    for (int t = 0; t < T; t++) {
        testcase_ilo();
    }
    return 0;
}