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
#include <bits/stdc++.h>
using namespace std;
//#define int LL // TODO
#define VL vector<LL>
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

int R(int a, int b){return rand()%(b-a+1)+a;}
// ******************************************************************************

VI normalizuj(VL &res);
template<typename T>vector<T> multiply(const vector<T> & a, const vector<T> & b,
    bool split = false, bool normalize = false);
// do FFT wrzucamy wektor z wartościami {0, 1}, więc wynik w jednej komórce <= n

namespace
  {
  VI fft_plus(VI a, VI b) // res[i+j] += a[i] * b[j]
    {
    return multiply(a, b);
    }
  VI fft_minus(VI a, VI b) // res[i-j] += a[i] * b[j]
    {
    reverse(ALL(b));
    VI res = multiply(a, b);
    res.erase(res.begin(), res.begin() + b.size() - 1);
    return res;
    }

  VI mnoz0(VI A, VI B) // B parzyste
    {
    VI res(A.size()+B.size());

    auto AminusB = fft_minus(A, B);
    REP(i, AminusB.size())
        if(i >= 4)res[i-4] -= AminusB[i];

    return res;  
    }

  VI mnoz2(VI A, VI B) // B nieparzyste
    {
    VI res(A.size()+B.size());

    auto AminusB = fft_minus(A, B);
    REP(i, AminusB.size())
        {
        if(i >= 2)res[i-2] -= AminusB[i];
        if(i >= 1)res[i-1] += AminusB[i];
        if(i >= 5)res[i-5] -= AminusB[i];
        }

    return res;  
    }
  }

VL add(VL a, VI b)
  {
  while(a.size() < b.size())a.PB(0);
  REP(i, b.size())
    a[i] += b[i];

  return a;
  }

VI mnoz_pom(VI A, VI B)
  {
  VI pa[2], pb[2];
  pa[0].resize(A.size());
  pa[1].resize(A.size());

  pb[0].resize(B.size());
  pb[1].resize(B.size());

  REP(i, A.size())
    pa[i%2][i] = A[i];

  REP(i, B.size())
    pb[i%2][i] = B[i];
  
  vector<VI> V;
  V.PB(fft_plus(A, B));

  V.PB(mnoz0(B, pa[0]));
  V.PB(mnoz2(B, pa[1]));

  V.PB(mnoz0(A, pb[0]));
  V.PB(mnoz2(A, pb[1]));

  VL res;
  for(auto i : V)res = add(res, i);
  FORD(i, (int)res.size()-5, 0)res[i] += res[i+4];
  REP(i, min(A.size(), B.size()))res[0] += i % 2 * A[i] * B[i];
 
  VI res2 = normalizuj(res);
  return res2;
  }

VI read()
  {
  int n;
  scanf("%d", &n);

  VI res(n);
  REP(i, n)
    scanf("%d", &res[i]);
  return res;
  }

void solve()
  {
  auto a = read();
  auto b = read();

  auto c = mnoz_pom(a, b);

  printf("%d ", (int)c.size()); 
  REP(i, c.size())printf("%d ", (int)c[i]);puts("");
  }

int32_t main()
  {
  int z;
  scanf("%d", &z);
  while(z--)solve();
  }

bool correct(VL &res)
  {
  REP(i, res.size())if(res[i] > 1)return 0;
  REP(i, res.size()-1)if(res[i] == 1 && res[i+1] == 1)return 0;
  return 1;
  }
VI normalizuj(VL &res)
    {
    REP(i, 100)res.PB(0);
    int iters = 0; // fib[iter] <=~ sum(res[i])
    do
      {
      iters++;
      FOR(i, 2, res.size()-2)
        {
        res[i-2] += res[i]/2;
        res[i+1] += res[i]/2;
        res[i] %= 2; 
        }
      
      FOR(i, 0, res.size()-3)
        {
        LL dalej = min(res[i], res[i+1]);
        res[i+2] += dalej;
        res[i] -= dalej;
        res[i+1] -= dalej;
        }
      
      res[1] += res[0]/2;
      res[0] %= 2;

      res[2] += res[1] / 2;
      res[0] += res[1] / 2;
      res[1] %= 2;
      }
    while(!correct(res));
    while(res.size() && res.back() == 0)res.pop_back();

    VI res2;
    for(auto i : res)res2.PB(i);
    return res2;
    }

// ************************************** FFT ********************************************

/* Prec. error max_ans/1e15 (2.5e18) for (long) doubles, so int rounding works
for doubles with answers 0.5e15, e.g. for sizes 2^20 and RANDOM ints in [0,45k],
assuming DBL_MANT_DIG=53 and LDBL_MANT_DIG=64. Consider normalizing and brute.*/
typedef double ld; // 'long double' is 2.2 times slower
struct C { ld re, im;
  C operator * (const C & he) const {
    return C{re * he.re - im * he.im,
        re * he.im + im * he.re};
  }
  void operator += (const C & he) { re += he.re; im += he.im; }
};
void dft(vector<C> & a, bool rev) {
  const int n = a.size();
  for(int i = 1, k = 0; i < n; ++i) {
    for(int bit = n / 2; (k ^= bit) < bit; bit /= 2);
    if(i < k) swap(a[i], a[k]);
  }
  for(int len = 1, who = 0; len < n; len *= 2, ++who) {
    static vector<C> t[30];
    vector<C> & om = t[who];
    if(om.empty()) {
      om.resize(len);
      const ld ang = 2 * acosl(0) / len;
      REP(i, len) om[i] = i%2 || !who ?
          C{cos(i*ang), sin(i*ang)} : t[who-1][i/2];
    }
    for(int i = 0; i < n; i += 2 * len)
      REP(k, len) {
         const C x = a[i+k], y = a[i+k+len]
            * C{om[k].re, om[k].im * (rev ? -1 : 1)};
        a[i+k] += y;
        a[i+k+len] = C{x.re - y.re, x.im - y.im};
      }
  }
  if(rev) REP(i, n) a[i].re /= n;
}
template<typename T>vector<T> multiply(const vector<T> & a, const vector<T> & b,
    bool split, bool normalize) {
  if(a.empty() || b.empty()) return {};
  T big = 0; if(normalize) { // [0,B] into [-B/2, B/2]
    assert(a.size() == b.size()); // equal size!!!
    for(T x : a) big = max(big, x);
    for(T x : b) big = max(big, x);
    big /= 2;
  }
  int n = a.size() + b.size();
  vector<T> ans(n - 1);
  /* if(min(a.size(),b.size()) < 190) { // BRUTE FORCE
    REP(i, a.size()) REP(j, b.size()) ans[i+j] += a[i]*b[j];
    return ans; } */
  while(n&(n-1)) ++n;
  auto foo = [&](const vector<C> & w, int i, int k) {
    int j = i ? n - i : 0, r = k ? -1 : 1;
    return C{w[i].re + w[j].re * r, w[i].im
        - w[j].im * r} * (k ? C{0, -0.5} : C{0.5, 0});
  };
  if(!split) { // standard fast version
    vector<C> in(n), done(n);
    REP(i, a.size()) in[i].re = a[i] - big;
    REP(i, b.size()) in[i].im = b[i] - big;
    dft(in, false);
    REP(i, n) done[i] = foo(in, i, 0) * foo(in, i, 1);
    dft(done, true);
    REP(i, ans.size()) ans[i] = is_integral<T>::value ?
        llround(done[i].re) : done[i].re;
  //REP(i,ans.size())err=max(err,abs(done[i].re-ans[i]));
  }
  else { // Split big INTEGERS into pairs a1*M+a2,
    const T M = 1<<15; // where M = sqrt(max_absvalue).
    vector<C> t[2]; // This version is 2.2-2.5 times slower.
    REP(x, 2) {
      t[x].resize(n);
      auto & in = x ? b : a; // below use (in[i]-big) if normalized
      REP(i, in.size()) t[x][i]=C{ld(in[i]%M), ld(in[i]/M)};
      dft(t[x], false);
    }
    T mul = 1;
    for(int s = 0; s < 3; ++s, mul *= M) {
      vector<C> prod(n);
      REP(x, 2) REP(y, 2) if(x + y == s) REP(i, n)
        prod[i] += foo(t[0], i, x) * foo(t[1], i, y);
      dft(prod, true); // remember: llround(prod[i].re)%MOD*mul !!!
      REP(i, ans.size()) ans[i]+= llround(prod[i].re)*mul;
    }
  }
  if(normalize) {
    T so_far = 0;
    REP(i, ans.size()) {
      if(i < (int) a.size()) so_far += a[i] + b[i];
      else so_far -= a[i-a.size()] + b[i-a.size()];
      ans[i] += big * so_far - big * big * min(i + 1, (int) ans.size() - i);
    }
  }
  return ans;
}