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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
Notice:
- This account submits solutions that result from the collaboration of two (non-Polish) individuals.
- We acknowledge that our participation is completely unofficial.
- We just want to be able to submit our solutions until the end of the week. Thank you :)!
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long int64;

namespace fft {

// General Convolution classes.

// DFT uses FFT over the field of complex numbers.
// NTT uses FFT over the field of numbers modulo 2013265921.

// How to use: 
// 1. Instantiate an object of class DFT or NTT.
// convolution::DFT dft;
// 2. Call the convolute method on two std::vectors a and b.
// auto c = dft.convolute(a, b);

// In the case of DFT, the types can be double, int or int64.
// In the case of NTT, the types can be int, int64 or unsigned int.

// DFT is faster and works on larger numbers, but may have precision issues.
// NTT should only be used when the inputs and outputs fit in the range
// [-10^9, 10^9] for int or int64, or [0, 2*10^9] for unsigned in.
// The array sizes for NTT should be at most 2^26.

typedef long long int64;

class Complex {
public:
    static constexpr double pi = acos(-1);
    double a, b;

    Complex(double a=0, double b=0) : a(a), b(b) {}

    Complex operator + (const Complex& other) const {
        return Complex(a + other.a, b + other.b);
    }

    Complex operator - (const Complex& other) const {
        return Complex(a - other.a, b - other.b);
    }

    Complex operator * (const Complex& other) const {
        return Complex(a * other.a - b * other.b, a * other.b + b * other.a);
    }

    Complex inverse() const {
        double len = a*a + b*b;
        return Complex(a / len, -b / len);
    }

    static Complex root(int lgsz, int kth=1) {
        return Complex(cos(2*pi*kth / (1<<lgsz)), sin(2*pi*kth / (1<<lgsz)));
    }
};

ostream& operator << (ostream& out, const Complex& c) {
    out << c.a << " + " << c.b << "i";
    return out;
}

void assign(double& d, const Complex& c) {
    d = c.a;
}

int64 myRound(const double& d) {
    return d + (d < 0 ? -0.5 : 0.5);
}

void assign(Complex& myC, const Complex& c) {
	myC = c;
}

void assign(int64& i, const Complex& c) {
    i = myRound(c.a);
}

void assign(int& i, const Complex& c) {
    i = myRound(c.a);
}

class Modular {
  public:
    static const int64 mod = 2013265921;
    static const int64 r = 137;
    static int64 lgpow(int64 x, int64 pw) {
        if (pw == 0)
            return 1;
        auto x2 = lgpow(x, pw>>1);
        if (pw&1)
            return x2*x2%mod*x%mod;
        return x2*x2%mod;
    }
    int64 x;

    Modular(int64 x=0) {
        if (x >= mod || x <= -mod)
            x %= mod;
        this->x = x;
    }

    Modular operator + (const Modular& other) const {
        int64 s = x + other.x;
        if (s >= mod)
            s -= mod;
        return Modular(s);
    }

    Modular operator - (const Modular& other) const {
        int64 d = x - other.x;
        if (d <= -mod)
            d += mod;
        return Modular(d);
    }

    Modular operator * (const Modular& other) const {
        return Modular(x * other.x % mod);
    }

    Modular inverse() {
        return Modular(lgpow(x, mod-2));
    }

    static Modular root(int lgsz, int kth=1) {
        return Modular(lgpow(lgpow(r, 1<<(27-lgsz)), kth));
    }
};

ostream& operator << (ostream& out, const Modular& m) {
    out << m.x;
    return out;
}

void assign(int64& i, const Modular& m) {
    auto t = m.x;
    if (t < 0)
        t += Modular::mod;
    i = t;
    if (t >= (Modular::mod>>1))
        i = -(Modular::mod - t);
}

void assign(int& i, const Modular& m) {
	auto t = m.x;
    if (t < 0)
        t += Modular::mod;
    i = t;
    if (t >= (Modular::mod>>1))
        i = -(Modular::mod - t);
}

void assign(unsigned int& i, const Modular& m) {
    auto t = m.x;
    if (t < 0)
        t += Modular::mod;
    i = t;
}

template<typename C>
class Convolution {
    int sz, lgsz;
    vector<C> roots, invRoots, lhs, rhs, res;
    vector<int> rev;

    static int nextPower2(int sz) {
        int i = 0;
        while ((1<<i) < sz)
            ++i;
        return i;
    }

    void precalc() {
        if (2*sz != roots.size()) {
            roots.resize(2*sz);
            invRoots.resize(2*sz);
            for (int j = (1<<lgsz); j < (1<<(lgsz+1)); ++j) {
                roots[j] = C::root(lgsz, j - (1<<lgsz));
                invRoots[j] = roots[j].inverse();
            }
            for (int j = (1<<lgsz) - 1; j >= 1; --j) {
                roots[j] = roots[j<<1];
                invRoots[j] = invRoots[j<<1];
            }        
        }

        if (sz != rev.size()) {
            rev.resize(sz);
            rev[0] = 0;
            for (int i = 1; i < sz; ++i) {
                rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lgsz - 1));
            }
        }
    }

    void prepInverse() {
        res.resize(sz);
        for (int i = 0; i < sz; ++i) {
            res[i] = lhs[i] * rhs[i];
        }
    }

    void fft(vector<C>& c, bool invert) {
        for (int i = 0; i < sz; ++i) {
            if (i < rev[i])
                swap(c[i], c[rev[i]]);
        }

        auto& r = (invert ? invRoots : roots);

        for (int halfStep = 1; halfStep < sz; halfStep <<= 1) {
            for (int start = 0; start < sz; start += (halfStep<<1)) {
                for (int i = 0; i < halfStep; ++i) {
                    C w = r[i + (halfStep<<1)] * c[start + i + halfStep];
                    c[start + i + halfStep] = c[start + i] - w;
                    c[start + i] = c[start + i] + w;
                }
            }
        }

        if (invert) {
            C mult = C(sz).inverse();
            for (int i = 0; i < sz; ++i) {
                c[i] = c[i] * mult;
            }
        }
    }

    template<typename T>
    void toField(vector<C>& c, const vector<T>& a) {
        c.resize(sz);
        for (int i = 0; i < a.size(); ++i)
            c[i] = a[i];
        for (int i = a.size(); i < sz; ++i)
            c[i] = 0;
    }

    template<typename T>
    void fromField(vector<T>& a, const vector<C>& c) {
        a.resize(sz);
        for (int i = 0; i < sz; ++i) {
            assign(a[i], c[i]);
        }
    }

  public:
    template<typename T>
    vector<T> convolute(const vector<T>& lhs, const vector<T>& rhs) {
        lgsz = 1 + nextPower2(max(lhs.size(), rhs.size()));
        sz = (1<<lgsz);
        precalc();
        toField(this->lhs, lhs);
        toField(this->rhs, rhs);
        fft(this->lhs, false);
        fft(this->rhs, false);
        prepInverse();
        fft(this->res, true);
        vector<T> res;
        fromField(res, this->res);
        return res;
    }
};

typedef Convolution<Complex> DFT;
typedef Convolution<Modular> NTT;
};

void Read(vector<int64>& a) {
    int n;
    cin >> n;
    a.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
}

void ShiftLeft(vector<int64>& a, int cnt) {
    if (a.size() == 0)
        return;

    for (int i = cnt; i < a.size(); ++i) {
        a[i - cnt] = a[i];
    }

    a.resize(a.size() - cnt);
}

void Diff(vector<int64>& a, vector<int64>& b) {
    for (int i = 0; i < a.size() && i < b.size(); ++i) {
        a[i] -= b[i];
    }
}

void Add(vector<int64>& a, vector<int64>& b) {
    a.resize(max(a.size(), b.size()));
    for (int i = 0; i < a.size() && i < b.size(); ++i) {
        a[i] += b[i];
    }
}

void Print(vector<int64>& a) {
    cout << a.size() << "\n";
    for (int i = 0; i < a.size(); ++i) {
        cout << a[i] << " ";
    }
    cout << endl;
}

void Propagate(vector<int64>& a) {
    for (int i = (int)a.size() - 3; i >= 0; --i) {
        a[i] -= a[i + 2];
    }
}

void BlindParity(vector<int64>& a, int parity) {
    for (int i = parity; i < a.size(); i += 2) {
        a[i] = 0;
    }
}

void Adjust(fft::DFT& dft, vector<int64>& ans, vector<int64>& a, vector<int64>& b, int parity) {
    auto ainv = a;
    BlindParity(ainv, 1 - parity);
    reverse(ainv.begin(), ainv.end());
    // k = j - i - 2/4.
    auto aux = dft.convolute(ainv, b);
    ShiftLeft(aux, a.size() - 1 + 2);
    // Print(aux);
    if (parity == 0) {
        Add(ans, aux);
        // ShiftLeft(aux, 2);
        // Diff(ans, aux);
    } else {
        Diff(ans, aux);
        // ShiftLeft(aux, 2);
        // Add(ans, aux);
    }
}

inline void Safety(vector<int64>& ans, int p) {
    if (p >= ans.size())
        ans.resize(p + 1);
}

void ClearNegatives(vector<int64>& ans) {
    if (ans[0] < 0) {
        ans[1] += ans[0];
        ans[0] = 0;
    }

    for (int i = 1; i < ans.size(); ++i) {
        if (ans[i] < 0) {
            Safety(ans, i + 1);
            ans[i - 1] -= ans[i];
            ans[i + 1] += ans[i];
            ans[i] = 0;
        }
    }
}

const int64 kInf = (1LL<<60);

void ClearFoursAndGreater(vector<int64>& ans) {
    int64 maxv = kInf;
    Safety(ans, 2);

    while (maxv > 3) {
        ans[1] += ans[0];
        ans[0] = 0;
        ans[2] += ans[1] / 2;
        ans[1] %= 2;

        maxv = 0;
        for (int i = 2; i < ans.size(); ++i) {
            maxv = max(maxv, ans[i]);
            if (ans[i] >= 3) {
                Safety(ans, i + 2);
                ans[i - 2] += ans[i] / 3;
                ans[i + 2] += ans[i] / 3;
                ans[i] %= 3;
            }
        }
    }
}

void Split(vector<int64>& ans, vector<vector<int64>>& desc) {
    desc.resize(6);
    for (int j = 0; j < 6; ++j) {
        desc[j].resize(ans.size());
    }
    for (int i = 0; i < ans.size(); ++i) {
        for (int j = 0; j < ans[i]; ++j) {
            desc[(i&1) * 3 + j][i] += 1;
        }
    }
}

void ClearThrees(vector<int64>& ans) {
    for (int i = ans.size() - 1; i >= 2; --i) {
        if (ans[i] >= 3) {
            Safety(ans, i + 2);
            ans[i - 2] += ans[i] / 3;
            ans[i + 2] += ans[i] / 3;
            ans[i] -= 3;

            for (int j = i + 2; j < ans.size(); j += 2) {
                if (ans[j] >= 3) {
                    Safety(ans, j + 2);
                    ans[j - 2] += ans[j] / 3;
                    ans[j + 2] += ans[j] / 3;
                    ans[j] -= 3;
                } else break;
            }
        }
    }
}

void ClearTwos(vector<int64>& ans) {
    for (int i = ans.size() - 1; i >= 0; --i) {
        if (ans[i] == 2) {
            Safety(ans, i + 1);
            if (ans[i + 1] == 1) {
                ans[i + 2] += 1;
                ans[i + 1] = 0;
                ans[i] = 1;
            } else {
                if (i == 0) {
                    ans[i + 1] += 1;
                    ans[i] = 0;
                } else if (i == 1) {
                    if (ans[i - 1] == 0) {
                        ans[i + 1] += 1;
                        ans[i - 1] += 1;
                        ans[i] = 0;
                    }
                } else if (ans[i - 1] == 0) {
                    if (ans[i - 1] == 1)
                        continue;
                    if (ans[i - 2] == 0) {
                        ans[i + 1] = 1;
                        ans[i] = 0;
                        ans[i - 2] = 1;
                    } else if (ans[i - 2] == 1) {
                        ans[i + 1] = 1;
                        ans[i] = 0;
                        ans[i - 2] = 2;
                    }
                }
            }
        }
    }

    for (int i = ans.size() - 1; i >= 1; --i) {
        if (ans[i] == 2) {
            Safety(ans, i + 1);
            ans[i + 1] += 1;
            ans[i] = 1;
            ans[i - 1] = 0;
        }
    }
}

void ClearOnes(vector<int64>& ans) {
    for (int i = ans.size() - 1; i >= 1; --i) {
        if (ans[i] == 1 && ans[i - 1] == 1) {
            Safety(ans, i + 1);
            ans[i + 1] = 1;
            ans[i] = 0;
            ans[i - 1] = 0;
            for (int j = i + 1; j + 1 < ans.size(); j += 2) {
                if (ans[j] == 1 && ans[j + 1] == 1) {
                    ans[j] = 0;
                    ans[j + 1] = 0;
                    Safety(ans, j + 2);
                    ans[j + 2] += 1;
                }
            }
        }
    }
}

void PrintFinal(vector<int64>& ans) {
    while (!ans.empty() && ans.back() == 0)
        ans.pop_back();

    cout << ans.size() - 1 << " ";
    for (int i = 1; i < ans.size(); ++i) {
        cout << ans[i] << " ";
    }
    cout << "\n";
}

void Solve() {
    vector<int64> a, b;
    Read(a);
    Read(b);

    fft::DFT dft;


    // k = i + j.
    auto ans = dft.convolute(a, b);

    Adjust(dft, ans, a, b, 0);
    Adjust(dft, ans, a, b, 1);
    Adjust(dft, ans, b, a, 0);
    Adjust(dft, ans, b, a, 1);

    Propagate(ans);

    Safety(ans, 5);
    ClearNegatives(ans);
    ClearFoursAndGreater(ans);

    vector<vector<int64>> desc;
    Split(ans, desc);
    ans.clear();
    for (int i = 0; i < 6; ++i) {
        Add(ans, desc[i]);
        ClearTwos(ans);
        ClearOnes(ans);
    }

    PrintFinal(ans);
}

int main() {
    int test;
    cin >> test;

    for (;test; --test) {
        Solve();
    }
}