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
// Karol Kosinski 2019
#include <cstdio>
#include <algorithm>
#include <list>
#include <utility>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FOD(i,b,a) for(int i=(b-1);i>=(a);--i)
//#define DEBUG(x...) printf(x)
#define DEBUG(x...)
//#define FORDEBUG(e,L,x...) for(const auto &e : L) DEBUG(x)
#define FORDEBUG(e,L,x...)
//#define SUMDEBUG(L) {int s=0;for(const auto &e : L)s+=FF[e.first]*e.second;DEBUG("s=%d\n",s);}
#define SUMDEBUG(L)
using namespace std;

int FF[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377};

using Fib = list<pair<int, int>>;
const int NX = 1'200'000;
int FC[NX];

void multiply(const list<int> &LA, const list<int> &LB, Fib &LC)
{
    for (int ea : LA) for (int eb : LB)
    {
        int n = min(ea, eb);
        int m = max(ea, eb);
        int ind = m + n - 2;
        FOD(i,n/2,0)
        {
            int aux = ind - 4 * i;
            if (not FC[aux]) LC.emplace_back(aux, 0);
            ++FC[aux];
        }
        if (n % 2 == 1)
        {
            int aux = max(m - n + 1, 2);
            if (not FC[aux]) LC.emplace_back(aux, 0);
            ++FC[aux];
        }
    }
    LC.sort();
    DEBUG("multiply\n");
    for (auto &el : LC)
    {
        el.second = FC[el.first];
        DEBUG("(F_%d, %d) ", el.first, el.second);
        FC[el.first] = 0;
    }
    DEBUG("\n");
    SUMDEBUG(LC);
}

int push_to_right(Fib &LC)
{
    auto it1 = LC.begin(), it2 = LC.begin();
    advance(it2, 1);
    while (it2 != LC.end())
    {
        if (it2->first - it1->first == 1)
        {
            int a = min(it1->second, it2->second);
            it1->second -= a;
            it2->second -= a;
            auto x = it2;
            advance(x, 1);
            if (x == LC.end() or x->first != it2->first + 1)
                LC.insert(x, make_pair(it2->first + 1, a));
            else
                x->second += a;
        }
        if (it2->second == 0) it2 = LC.erase(it2);
        else ++it2;
        if (it1->second == 0) it1 = LC.erase(it1);
        else ++it1;
        if (it1 == it2 and it1 != LC.end()) ++it2;
    }
    int mx = 0;
    DEBUG("push_to_right\n");
    for (const auto &el : LC)
    {
        DEBUG("(F_%d, %d) ", el.first, el.second);
        mx = max(el.second, mx);
    }
    DEBUG("\n");
    SUMDEBUG(LC);
    return mx;
}

void print_result(const Fib &LC)
{
    int n = LC.rbegin()->first - 1;
    printf("%d ", n);
    for (const auto &el : LC) FC[el.first - 2] = 1;
    FOR(i,0,n) printf("%d ", FC[i]);
    printf("\n");
    for (const auto &el : LC) FC[el.first - 2] = 0;
}

void push_twos_to_left(Fib &LC)
{
    auto it = LC.rbegin();
    while (it != LC.rend())
    {
        if (it->second == 2)
        {
            auto x = it;
            advance(x, 1);
            if (x != LC.rend())
            {
                if (it->first - x->first == 2 and x->second == 1)
                {
                    it->second = 1;
                    x->second = 2;
                    LC.insert(x.base(), make_pair(it->first - 1, 1));
                }
                else if (it->first - x->first > 2)
                {
                    it->second = 1;
                    LC.insert(x.base(), make_pair(it->first - 2, 1));
                    LC.insert(x.base(), make_pair(it->first - 1, 1));
                }
            }
            else if (it->first >= 4)
            {
                it->second = 1;
                LC.insert(x.base(), make_pair(it->first - 2, 1));
                LC.insert(x.base(), make_pair(it->first - 1, 1));
            }
        }
        ++it;
    }
    DEBUG("push_twos_to_left\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
}

void erase_twos_from_left(Fib &LC)
{
    auto it = LC.begin(), x = LC.begin();
    advance(x, 1);
    if (it->first == 2 and it->second == 2 and (x == LC.end() or x->first > 3))
    {
        LC.insert(x, make_pair(3, 1));
        it = LC.erase(it);
    }
    else if (it->first == 3 and it->second == 2)
    {
        it->second = 1;
        it = LC.insert(it, make_pair(2, 2));
    }
    if (it->first != 2 or it->second != 2) return;
    
    x = it;
    advance(x, 1);
    while (x != LC.end() and x->first - it->first == 1)
    {
        ++x, ++it;
    }
    LC.insert(x, make_pair(it->first + 1, 1));
    advance(x, -2);
    int aux = 0;
    while (x != LC.begin())
    {
        x->second = aux;
        aux = 1 - aux;
        --x;
    }
    x->second = aux;
    
    it = LC.begin();
    while (it != LC.end())
    {
        if (it->second == 0) it = LC.erase(it);
        else ++it;
    }
    DEBUG("erase_twos_from_left\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
}

void eliminate_twos(Fib &LC)
{
    //LC.pop_front();
    //LC.pop_front();
    //LC.begin()->second = 2;
    //LC.emplace_front(2, 1);
    //LC.emplace_front(3, 1);
    //LC.emplace_front(6, 1);
    //LC.emplace_front(7, 1);
    //LC.sort();
    //FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    //DEBUG("\n");
    
    auto it1 = LC.begin(), it2 = LC.begin();
    advance(it2, 1);
    while (it2 != LC.end())
    {
        if (it2->first - it1->first == 1 and it1->second == 1)
        {
            auto x = it2;
            advance(x, 1);
            if (it2->second == 1)
            {
                if (x != LC.end() and x->first - it2->first == 2 and x->second == 2)
                {
                    it1->second = 0;
                    it2->second = 0;
                    LC.insert(x, make_pair(it2->first + 1, 1));
                }
            }
            else // it2->second == 2
            {
                if (x == LC.end() or x->first - it2->first > 1)
                {
                    it1->second = 0;
                    it2->second = 1;
                    LC.insert(x, make_pair(it2->first + 1, 1));
                }
                else // x->first - it2->first == 1 and x->second == 1
                {
                    it1->second = 0;
                    it2->second = 1;
                    x->second = 2;
                }
            }
        }
        if (it2->second == 0) it2 = LC.erase(it2);
        else ++it2;
        if (it1->second == 0) it1 = LC.erase(it1);
        else ++it1;
        if (it1 == it2 and it1 != LC.end()) ++it2;
    }
    DEBUG("eliminate_twos\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
}

void erase_close_ones(Fib &LC)
{
    auto it = LC.begin();
    while (it != LC.end())
    {
        // it->second == 1
        auto x = it;
        advance(x, 1);
        int aux = 1;
        while (x != LC.end() and x->first - it->first == aux) ++x, ++aux;
        DEBUG("it = %d    x = %d     aux = %d\n",
                it->first, ((x == LC.end()) ? -1 : x->first), aux);
        if (aux > 1)
        {
            x = LC.insert(x, make_pair(it->first + aux, 1));
            if (aux % 2 == 1) ++it;
            it->second = 0;
            aux = 0;
            ++it;
            while (it != x)
            {
                it->second = aux;
                aux = 1 - aux;
                ++it;
            }
        }
        else ++it;
    }
    
    it = LC.begin();
    while (it != LC.end())
    {
        if (it->second == 0) it = LC.erase(it);
        else ++it;
    }
    DEBUG("erase_close_ones\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
}

void get_number_to_normalize(Fib &LC, Fib &LD)
{
    auto it = LC.begin();
    while (it != LC.end())
    {
        int aux = min(it->second, 2);
        it->second -= aux;
        LD.emplace_back(it->first, aux);
        ++it;
    }
    
    it = LC.begin();
    while (it != LC.end())
    {
        if (it->second == 0) it = LC.erase(it);
        else ++it;
    }
    DEBUG("get_number_to_normalize - LC\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
    DEBUG("get_number_to_normalize - LD\n");
    FORDEBUG(el, LD, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LD);
}

void add_numbers(Fib &LC, Fib &LD)
{
    LC.merge(LD);
    int aux = -1;
    auto it = LC.begin();
    while (it != LC.end())
    {
        auto x = it;
        advance(x, 1);
        if (x != LC.end() and x->first == it->first)
        {
            it->second += x->second;
            LC.erase(x);
        }
        ++it;
    }
    
    DEBUG("add_numbers - LC\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
    DEBUG("add_numbers - LD\n");
    FORDEBUG(el, LD, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
}

void erase_close_ones_and_twos(Fib &LC)
{
    //LC.pop_front();
    //LC.pop_front();
    //LC.pop_front();
    //LC.emplace_front(8, 2);
    //LC.emplace_front(6, 2);
    //LC.emplace_front(4, 1);
    //LC.emplace_front(3, 1);
    //LC.emplace_front(2, 1);
    //FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    //DEBUG("\n");

    auto it = LC.begin();
    while (it != LC.end())
    {
        auto x = it;
        advance(x, 1);
        if (x != LC.end() and x->first - it->first == 1 and it->second == 1)
        {
            auto y = x;
            advance(y, 1);
            if (x->second == 2)
            {
                it->second = 0;
                x->second = 1;
                LC.insert(y, make_pair(x->first + 1, 1));
                ++it;
                continue;
            }
            if (x->second == 1 and y != LC.end()
                and y->second == 2 and y->first - x->first == 1)
            {
                x->second = 0;
                y->second = 1;
                advance(y, 1);
                LC.insert(y, make_pair(x->first + 2, 1));
                ++it;
                continue;
            }
        }
        if (it->second == 1)
        {
            int aux = 1;
            while (x != LC.end() and x->first - it->first == aux) ++x, ++aux;
            DEBUG("it = %d    x = %d     aux = %d\n",
                    it->first, ((x == LC.end()) ? -1 : x->first), aux);
            if (aux > 1)
            {
                x = LC.insert(x, make_pair(it->first + aux, 1));
                if (aux % 2 == 1) ++it;
                it->second = 0;
                aux = 0;
                ++it;
                while (it != x)
                {
                    it->second = aux;
                    aux = 1 - aux;
                    ++it;
                }
            }
            else ++it;
        }
        else ++it;
    }
    
    it = LC.begin();
    while (it != LC.end())
    {
        if (it->second == 0) it = LC.erase(it);
        else ++it;
    }
    DEBUG("erase_close_ones_and_twos\n");
    FORDEBUG(el, LC, "(F_%d, %d) ", el.first, el.second);
    DEBUG("\n");
    SUMDEBUG(LC);
}

void testcase()
{
    Fib LC;
    {
        int nfa, nfb;
        list<int> LA, LB;
        scanf("%d", &nfa);
        FOR(i,0,nfa)
        {
            int a;
            scanf("%d", &a);
            if (a) LA.emplace_back(i + 2);
        }
        scanf("%d", &nfb);
        FOR(i,0,nfb)
        {
            int a;
            scanf("%d", &a);
            if (a) LB.emplace_back(i + 2);
        }
        multiply(LA, LB, LC);
    }
    int mx = push_to_right(LC);
    if (mx == 1)
    {
        print_result(LC);
        DEBUG("-------------------------------------\n");
        return;
    }
    if (mx == 2)
    {
        //LC.emplace_front(2, 2);
        push_twos_to_left(LC);
        erase_twos_from_left(LC);
        eliminate_twos(LC);
        erase_close_ones(LC);
        print_result(LC);
        DEBUG("-------------------------------------\n");
        return;
    }
    Fib LD, LE;
    get_number_to_normalize(LC, LD);
    push_twos_to_left(LD);
    erase_twos_from_left(LD);
    eliminate_twos(LD);
    erase_close_ones(LD);
    while (not LC.empty())
    {
        get_number_to_normalize(LC, LE);
        push_twos_to_left(LE);
        erase_twos_from_left(LE);
        eliminate_twos(LE);
        erase_close_ones(LE);
        
        add_numbers(LD, LE);
        erase_close_ones_and_twos(LD);
        push_twos_to_left(LD);
        erase_twos_from_left(LD);
        eliminate_twos(LD);
        erase_close_ones(LD);
    }
    print_result(LD);
    DEBUG("-------------------------------------\n");
}

int main()
{
    int z;
    scanf("%d", &z);
    while (z--) testcase();
    return 0;
}