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
#include <stdio.h>
#include <stdlib.h>

#define xint64 long long
#define MAXL 2024000

int l1 = 0;
int f1[MAXL];
int l2 = 0;
int f2[MAXL];

int ls = 0;
int s[MAXL];
int mw = 0;

int xmin(int a, int b) {
  return (a>b) ? b : a;
}

void show(int* t, int l) {
  printf("%d", l);
  for (int i=0;i<l;i++)
    printf(" %d", t[i]);
  printf("\n");
}

xint64 val(int* t, int l) {
  xint64 x1 = 1;
  xint64 x2 = 2;
  xint64 s = 0;
  if (l==0)
    return 0;
  else if (l==1)
    return t[0] * x1;
  else {
    s = t[0] * x1;
    for (int i=1;i<l;i++) {
      s += t[i] * x2;
      xint64 x = x1 + x2;
      x1 = x2;
      x2 = x;
    }
    return s;
  }
}

void loop_2(int ix, int f) { // indeksy w Fib
  //printf("MUL F(%d)xF(%d)\n", ix, f);
  while (ls<=ix+f-2) {
    s[ls] = 0;
    ls++;
  }
  while (ix>=3 && f>=3) {
    s[ix+f-4] += 1;
    //s[ix+f-5] -= 1;
    ix -= 2;
    f -= 2;
  }
  if (f>=3)
    s[f-2] += 1;
  else if (ix>=3)
    s[ix-2] += 1;
  else
    s[0] += 1;
}

void join_up() {
  for (int i=1;i<ls;i++) {
    if (s[i]!=0 && s[i-1]!=0) {
      int mm = xmin(s[i], s[i-1]);
      s[i] -= mm;
      s[i-1] -= mm;
      while (ls<=i+1) {
        s[ls] = 0;
        ls++;
      }
      s[i+1] += mm;
    }
  }
}

void split() {
  for (int i=ls-1;i>0;i--) {
    if (s[i]>1) {
      int r = s[i] % 2;
      int r2 = s[i] / 2;
      while (ls<=i+1) {
        s[ls] = 0;
        ls++;
      }
      s[i+1] += r2;
      s[i-(i==1 ? 1 : 2)] += r2;
      s[i] = r;
    }
  }
}

void cut_head() {
  while (ls>0 && s[ls-1]==0)
    ls--;
}

int ok() {
  if (ls==0)
    return 1;
  if (s[0]>1)
    return 0;
  for (int i=0;i<ls;i++)
    if (s[i]>1)
      return 0;
    else if (s[i]*s[i-1]!=0)
      return 0;
  return 1;
}

xint64 go_up(xint64 v, int ix, xint64 x1, xint64 x2) {
  //printf("UP(%lld, %d, %lld, %lld)\n", v, ix, x1, x2);
  if (x1+x2<=v) {
    v = go_up(v, ix+1, x2, x1+x2);
    if (v>=x2) {
      s[ix] += 1;
      return v-x2;
    } else
      return v;
  } else {
    s[ix] += 1;
    return v-x2;
  }
}

void up() {
  if (ls==0 || s[0]<=1)
    return ;
  xint64 s0 = s[0];
  s[0] = 0;
  go_up(s0, 0, 1, 1);
}

void norm() {
  int nw = 0;
  while (!ok()) {
    join_up();
    split();
    //show(s, ls);
    up();
    nw++;
//    show(s, ls);
  }
  if (nw>mw)
    mw = nw;
  cut_head();
}

void mul(int f) {  // f = prawdziwy indeks F ( 1=1, 2=1, 3=2, 4=5, itd. )
  if (f==1 || f==2) {
    for (int i=0;i<l1;i++) {
      while (ls<=i) {
        ls++;
        s[i] = 0;
      }
      s[i] += f1[i];
    }
  } else {
    for (int i=l1-1;i>=1;i--) {
      if (f1[i]==0)
        continue ;
      int ix = i+2;
      // f1[i] => F(i+2) x f2[f] => F(f+1) = F(i+f+2)-F(i+f)+F(i)xF(f-1) = s[i+f]-s[i+f-2]+f[i-2]xf[f-2]
      loop_2(ix, f);
    }
    if (f1[0]==1)
      s[f-2] += 1;
  }
}

int main() {
  int k;
  scanf("%d", &k);
  for (int i=0;i<k;i++) {
    ls = 0;
    scanf("%d", &l1);
    for (int j=0;j<l1;j++)
      scanf("%d", &f1[j]);
    scanf("%d", &l2);
    for (int j=0;j<l2;j++)
      scanf("%d", &f2[j]);
    //show(f1, l1);
    //show(f2, l2);
    //printf("F1 = %lld\n", val(f1, l1));
    //printf("F2 = %lld\n", val(f2, l2));
    for (int j=0;j<l2;j++)
      if (f2[j]==1)
        mul(j+2);
    norm();
    show(s, ls);
    //printf("MW=%d\n", mw);
    //if (val(f1, l1)*val(f2, l2)!=val(s, ls)) {
     // printf("SHIT %lld %lld = %lld vs %lld => \n", val(f1, l1), val(f2, l2), val(f1, l1)*val(f2, l2), val(s, ls));
     // show(f1, l1);
     // show(f2, l2);
     // show(s, ls);
    //}
    //printf("FS = %lld\n", val(s, ls));
  }
}