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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
  __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                   __gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1);    // Out: 0.
// *s.find_by_order(1);  // Out: 2.

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

int losuj(int a, int b) {
  return rand() % (b - a + 1) + a;
}

string ToString(int n, int len) {
  string s = to_string(n);
  reverse(s.begin(), s.end());
  while ((int) s.size() < len) s.push_back(' ');
  reverse(s.begin(), s.end());
  return s;
}

struct Farmer;
struct Tank;

struct Unit {
  virtual ~Unit() {}

  Farmer* ToFarmer();
  Tank* ToTank();

  virtual int Gold() const = 0;
};

struct Farmer : Unit {
  int gold;

  Farmer() : gold(0) {}

  int Gold() const override {
    return gold;
  }

  void ZeroGold() {
    gold = 0;
  }

  void AddGold(int cnt) {
    gold += cnt;
  }

  static string Name() {
    return "FARMER";
  }
};

struct Tank : Unit {
  int Gold() const override {
    return 0;
  }

  static string Name() {
    return "TANK";
  }
};

Farmer* Unit::ToFarmer() { return dynamic_cast<Farmer*>(this); }
Tank* Unit::ToTank() { return dynamic_cast<Tank*>(this); }

struct Gold;
struct Stone;
struct Base;

struct Plansza;

struct Pole {
  Plansza* plansza;
  Unit* unit;
  bool ruszony = false;

  Pole(Plansza* plansza_) : plansza(plansza_), unit(nullptr) {}
  virtual ~Pole() {}

  Farmer* GetFarmer() {
    if (unit == nullptr) return nullptr;
    return unit->ToFarmer();
  }

  Tank* GetTank() {
    if (unit == nullptr) return nullptr;
    return unit->ToTank();
  }

  Gold* ToGold();
  Stone* ToStone();
  Base* ToBase();

  virtual int IleZlota() const = 0;
  virtual bool JestSkala() const = 0;
  virtual void EndTurnInner() = 0;

  void EndTurn() {
    ruszony = false;
    EndTurnInner();
  }

  virtual void PrintInner(debug& deb) const = 0;

  void Print(debug& deb) const {
    if (unit) {
      deb << "\033[7m";
    }
    PrintInner(deb);
    deb << "\033[0m";
  }
};

struct Plansza {
  int n;
  int zloto = 200;
  vector<vector<unique_ptr<Pole>>> pola;
  vector<unique_ptr<Unit>> units;

  bool Valid(int wsp) const { return 0 <= wsp and wsp < n; }
  bool Valid(int x, int y) const { return Valid(x) and Valid(y); }

  Pole& Get(int r, int c) const {
    #ifdef LOCAL
    assert(Valid(r, c));
    #endif
    return *pola[r][c].get();
  }

  void EndTurn() {
    for (auto& row : pola) {
      for (auto& p : row) {
        p->EndTurn();
      }
    }
  }

  void Print() const {
    #ifdef LOCAL
    debug deb;
    deb << "================\n";
    deb << "Zloto = " << zloto << "\n";
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (j) deb << " ";
        pola[i][j]->Print(deb);
      }
      deb << "\n";
    }
    deb << "================\n";
    #endif
  }
};

struct Gold : Pole {
  int gold;

  Gold(Plansza* plansza_, int g) : Pole(plansza_), gold(g) { assert(1 <= g); }

  int IleZlota() const override {
    return gold;
  }

  bool JestSkala() const override {
    return false;
  }

  void EndTurnInner() override {
    Farmer* f = GetFarmer();
    if (!f) return;
    const int ile = min(gold, 10);
    gold -= ile;
    f->AddGold(ile);
  }

  void PrintInner(debug& deb) const override {
    deb << "\033[43m" << ToString(IleZlota(), 3);
  }
};

struct Stone : Pole {
  int stone;

  Stone(Plansza* plansza_, int s) : Pole(plansza_), stone(s) { assert(1 <= s); }

  int IleZlota() const override {
    return 0;
  }

  bool JestSkala() const override {
    return stone > 0;
  }

  void EndTurnInner() override {
    Tank* t = GetTank();
    if (!t) return;
    const int ile = min(stone, 10);
    stone -= ile;
  }

  void PrintInner(debug& deb) const override {
    deb << "\033[100m" << ToString(IleZlota(), 3);
  }
};

struct Base : Pole {
  Base(Plansza* plansza_) : Pole(plansza_) {}

  int IleZlota() const override {
    return 0;
  }

  bool JestSkala() const override {
    return false;
  }

  void EndTurnInner() override {
    Farmer* f = GetFarmer();
    if (!f) return;
    plansza->zloto += f->Gold();
    f->ZeroGold();
  }

  void PrintInner(debug& deb) const override {
    deb << "\033[42m" << ToString(IleZlota(), 3);
  }
};

Gold* Pole::ToGold() { return dynamic_cast<Gold*>(this); }
Stone* Pole::ToStone() { return dynamic_cast<Stone*>(this); }
Base* Pole::ToBase() { return dynamic_cast<Base*>(this); }

istream& operator>>(istream& stream, Plansza& p) {
  stream >> p.n;
  p.pola.clear();
  p.pola.resize(p.n);
  for (auto& row : p.pola) {
    row.resize(p.n);
  }
  for (auto& v : p.pola) {
    for (auto& pole : v) {
      int x;
      stream >> x;
      if (x == 0) {
        pole = unique_ptr<Pole>(new Base(&p));
      } else if (x > 0) {
        pole = unique_ptr<Gold>(new Gold(&p, x));
      } else if (x < 0) {
        pole = unique_ptr<Stone>(new Stone(&p, -x));
      } else {
        assert(false);
      }
    }
  }
  return stream;
}

struct Ruch {
  virtual ~Ruch() {}

  virtual void Apply(Plansza& p) const = 0;
  virtual void Print(ostream& stream) const = 0;
  virtual void Print(debug& deb) const = 0;
};

struct Move : Ruch {
  int r1, c1, r2, c2;

  Move(int r1_, int c1_, int r2_, int c2_)
      : r1(r1_), c1(c1_), r2(r2_), c2(c2_) {}

  void Apply(Plansza& p) const override {
    assert(p.Valid(r1, c1));
    assert(p.Valid(r2, c2));
    assert(abs(r1 - r2) + abs(c1 - c2) == 1);
    assert(p.Get(r1, c1).unit);
    assert(!p.Get(r1, c1).ruszony);
    assert(!p.Get(r2, c2).unit);
    assert(!p.Get(r2, c2).JestSkala() or !p.Get(r1, c1).GetFarmer());
    swap(p.Get(r1, c1).unit, p.Get(r2, c2).unit);
    p.Get(r2, c2).ruszony = true;
  }

  void Print(ostream& stream) const override {
    stream << "M " << r1 << " " << c1 << " " << r2 << " " << c2 << endl;
  }

  void Print(debug& deb) const override {
    #ifdef LOCAL
    deb << "Move(" imie(r1) imie(c1) imie(r2) imie(c2) ")";
    #endif
  }
};

template <typename T>
struct Recruit : Ruch {
  void Apply(Plansza& p) const override {
    assert(p.zloto >= 100);
    assert(p.Get(0, 0).unit == nullptr);
    p.zloto -= 100;;
    auto unit = unique_ptr<Unit>(new T());
    p.Get(0, 0).unit = unit.get();
    p.units.push_back(move(unit));
  }

  void Print(ostream& stream) const override {
    stream << "R " << T::Name() << endl;
  }

  void Print(debug& deb) const override {
    #ifdef LOCAL
    deb << "Recruit<" << T::Name() << ">";
    #endif
  }
};

// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //

Plansza* plansza;
int cut;
vector<vector<unique_ptr<Ruch>>> ruchy;

vector<unique_ptr<Ruch>>& Tura(int x) {
  assert(0 <= x);
  x += cut;
  if (x >= (int) ruchy.size()) {
    ruchy.resize(x + 1);
  }
  return ruchy[x];
}

void Przytnij() {
  for (int i = cut; i < (int) ruchy.size(); i++) {
    for (auto& r : ruchy[i]) {
      r->Apply(*plansza);
    }
    plansza->EndTurn();
  }
  cut = (int) ruchy.size();
  plansza->Print();
  debug() << imie(cut);
}

// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //

class Ogarniacz {
 public:
  int n;
  vector<int> czasy;
  vector<pair<int, int>> podzial;

  bool Ok(int c, int k) {
    podzial.clear();
    int nast = 0;
    for (int powt = 0; powt < k; powt++) {
      const int prawo = k - 1 - (int) podzial.size();
      const int poz = n - prawo;
      int suma = 0;
      int dokad = nast - 1;
      for (int i = nast; i < poz; i++) {
        if (suma + czasy[i] > c) {
          break;
        }
        suma += czasy[i];
        dokad = i;
      }
      assert(nast <= dokad and dokad < poz);
      assert(suma <= c);
      podzial.emplace_back(nast, dokad);
      nast = dokad + 1;
    }
    assert((int) podzial.size() == k);
    return nast == n;
  }

  void LiczDp(int k) {
    assert(n >= k);
    for (int i = 0; i < n; i++) {
      czasy[i] = max(0, czasy[i] - min(i, k));
    }
    debug() << imie(czasy);
    int a = *max_element(czasy.begin(), czasy.end()),
        b = numeric_limits<int>::max() / 10;
    while (a < b) {
      const int c = (a + b) / 2;
      if (Ok(c, k)) {
        b = c;
      } else {
        a = c + 1;
      }
    }
    Ok(a, k);
    debug() << imie(a);
  }

 public:
  Ogarniacz(int k, vector<int> czasy_) : czasy(move(czasy_)) {
    n = (int) czasy.size();
    LiczDp(k);
  }

  bool MamStop(int ktory, int pozycja) {
    assert(0 <= pozycja and pozycja < (int) czasy.size());
    assert(0 <= ktory and ktory < (int) podzial.size());
    if (podzial[ktory].first <= pozycja and pozycja <= podzial[ktory].second) {
      // pass.
    } else {
      return false;
    }
    if (czasy[pozycja] <= 0) return false;
    czasy[pozycja]--;
    return true;
  }
};

// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //

vector<pair<int, int>> Sciezka(int a, int b) {
  const int n = plansza->n;
  vector<pair<int, int>> res;
  for (int i = 1; i < n - 1; i += 2) {
    for (int j = a; j < b; j++) {
      res.emplace_back(i, j);
    }
    for (int j = b - 1; j >= a; j--) {
      res.emplace_back(i + 1, j);
    }
  }
  for (int i = a; i < b; i++) {
    res.emplace_back(n - 1, i);
  }
  for (int i = n - 1; i >= 0; i--) {
    res.emplace_back(i, b);
  }
  for (int i = b - 1; i >= a; i--) {
    res.emplace_back(0, i);
  }
  return res;
}

bool Splaw(vector<unique_ptr<Ruch>>& tura,
           vector<vector<vector<pair<int, pair<int, int>>>>>& odw_cnt,
           const vector<pair<int, int>>& kolejnosc,
           set<pair<int, int>>& pozycje) {
  bool cos_bylo = false;
  for (auto& k : kolejnosc) {
    const int r = k.first;
    const int c = k.second;
    auto it = pozycje.find(make_pair(r, c));
    if (it == pozycje.end()) continue;
    for (auto& p : odw_cnt[r][c]) {
      if (pozycje.count(p.second)) {
        continue;
      }
      if (p.first > 0) {
        p.first--;
        tura.emplace_back(new Move(r, c, p.second.first, p.second.second));
        debug() << "MOVE " << r << " " << c << " -> " << p.second;
        cos_bylo = true;
        pozycje.erase(it);
        pozycje.insert(p.second);
        break;
      }
    }
  }
  return cos_bylo;
}

void BezKamieni() {
  const int n = plansza->n;
  assert(n == 20);

  int nast;
  plansza->Print();

  // W dół.
  nast = 0;
  Tura(nast).emplace_back(new Recruit<Farmer>());
  for (int i = 1; i < n; i++) {
    Tura(nast++).emplace_back(new Move(i - 1, 0, i, 0));
  }
  for (int i = 1; i + 1 < n; i++) {
    Tura(nast++).emplace_back(new Move(n - 1, i - 1, n - 1, i));
  }
  for (int i = n - 2; i >= 1; i--) {
    Tura(nast++).emplace_back(new Move(i + 1, n - 2, i, n - 2));
  }
  for (int i = n - 3; i >= 0; i--) {
    Tura(nast++).emplace_back(new Move(1, i + 1, 1, i));
  }
  Tura(nast++).emplace_back(new Move(1, 0, 0, 0));

  // W prawo.
  nast = 1;
  Tura(nast).emplace_back(new Recruit<Farmer>());
  for (int i = 1; i < n; i++) {
    Tura(nast++).emplace_back(new Move(0, i - 1, 0, i));
  }
  for (int i = 1; i + 1 < n; i++) {
    Tura(nast++).emplace_back(new Move(i - 1, n - 1, i, n - 1));
  }
  for (int i = n - 2; i >= 1; i--) {
    Tura(nast++).emplace_back(new Move(n - 2, i + 1, n - 2, i));
  }
  for (int i = n - 3; i >= 0; i--) {
    Tura(nast++).emplace_back(new Move(i + 1, 1, i, 1));
  }

  Przytnij();

  // Oba.
  Tura(0).emplace_back(new Move(0, 0, 1, 0));
  Tura(0).emplace_back(new Move(0, 1, 0, 0));

  Przytnij();

  const int ile = min(10, 2 + plansza->zloto / 100);
  assert(ile == 10);
  nast = 0;
  for (int i = 0; i < ile - 2; i++) {
    auto& tura = Tura(nast++);
    for (int j = i + 1; j >= 0; j--) {
      tura.emplace_back(new Move(j, 0, j + 1, 0));
    }
    tura.emplace_back(new Recruit<Farmer>());
  }
  for (int i = 1; i < n; i++) {
    auto& tura = Tura(nast++);
    for (int j = 0; j < ile; j++) {
      tura.emplace_back(new Move(j, i - 1, j, i));
    }
  }
  for (int i = 0; i < n - ile; i++) {
    auto& tura = Tura(nast++);
    for (int j = i + ile; j > i; j--) {
      tura.emplace_back(new Move(j - 1, n - 1, j, n - 1));
    }
  }
//  for (int i = n - 2; i >= 0; i--) {
//    auto& tura = Tura(nast++);
//    for (int j = 1; j <= ile; j++) {
//      tura.emplace_back(new Move(n - j, i + 1, n - j, i));
//    }
//  }
  Przytnij();
  for (int j = n - ile; j < n; j++) {
    vector<int> pola(n);
    int jest = 0;
    for (int i = 1; i <= 18; i++) {
      pola[i] = plansza->Get(j, i).IleZlota() / 10;
      jest += pola[i];
    }
    int cnt = min(5, jest);
    vector<int> gdzie;
    while (cnt--) {
      int pos = losuj(1, 18);
      while (pola[pos] == 0) pos = losuj(1, 18);
      pola[pos]--;
      gdzie.push_back(pos);
    }
    sort(gdzie.begin(), gdzie.end());

    nast = 0;
    for (int i = 18; i >= 0; i--) {
      Tura(nast++).emplace_back(new Move(j, i + 1, j, i));
      while (!gdzie.empty() and gdzie.back() == i) {
        Tura(nast++);
        gdzie.pop_back();
      }
    }
  }
  Przytnij();
  nast = 0;
  for (int i = 0; i < n - ile; i++) {
    auto& tura = Tura(nast++);
    for (int j = 0; j < ile; j++) {
      tura.emplace_back(new Move(n - ile + j - i, 0, n - ile + j - 1 - i, 0));
    }
  }
  for (int i = 0; i < ile; i++) {
    auto& tura = Tura(nast++);
    for (int j = i; j >= 0; j--) {
      tura.emplace_back(new Move(0, j, 0, j + 1));
    }
    for (int j = 1; j < ile - i; j++) {
      tura.emplace_back(new Move(j, 0, j - 1, 0));
    }
  }

  Przytnij();

  assert(ile == 10);
  //assert(plansza->zloto >= 3000);
  const int na_slupek = min(50, ile + plansza->zloto / 100) / 10;
  debug() << imie(na_slupek);

  vector<vector<vector<pair<int, pair<int, int>>>>> odw_cnt(
      20, vector<vector<pair<int,pair<int, int>>>>(20));
  for (int i = 0; i <= 17; i++) {
    int x = 1000;
    if (i == 16) x = na_slupek;
    if (i == 14) x = na_slupek * 2;
    if (i == 12) x = na_slupek * 3;
    if (i == 10) x = na_slupek * 4;
    if (i == 8) x = na_slupek * 5 - 2;
    if (i == 6) x = na_slupek * 6 - 4;
    if (i == 4) x = na_slupek * 7 - 6;
    if (i == 2) x = na_slupek * 8 - 8;
    if (i == 0) x = na_slupek * 9 - 10;
    odw_cnt[0][i].emplace_back(x, make_pair(0, i + 1));
  }
  vector<pair<int, int>> kolejnosc;
  for (int i = 0; i < 20; i += 2) {
    odw_cnt[0][i].emplace_back(1000, make_pair(1, i));
    auto s = Sciezka(i, i + 1);
    //debug() << imie(s.size()) imie(na_slupek);
    assert((int) s.size() >= na_slupek);
    for (int j = 0; j < na_slupek - 1; j++) {
      kolejnosc.push_back(s[j]);
      odw_cnt[s[j].first][s[j].second].emplace_back(1000, s[j + 1]);
    }
  }
  reverse(kolejnosc.begin(), kolejnosc.end());
  set<pair<int, int>> pozycje;
  for (int i = 1; i <= 10; i++) pozycje.insert(make_pair(0, i));
  for (int i = 19; i >= 0; i--)  kolejnosc.emplace_back(0, i);
  debug() << imie(kolejnosc) imie(pozycje);
  int cnt_nowe = na_slupek * 10 - 10;
  while (true) {
    bool cos_bylo = false;
    auto& tura = Tura(0);
    if (cnt_nowe > 0) {
      cos_bylo = true;
      cnt_nowe--;
      tura.emplace_back(new Recruit<Farmer>());
      pozycje.insert(make_pair(0, 0));
    }
    if (Splaw(tura, odw_cnt, kolejnosc, pozycje)) {
      cos_bylo = true;
    }
    Przytnij();
    //int x; cin >> x;
    if (!cos_bylo) break;
  }

  // Wężyk.
  for (int pow = 0; pow < 10; pow++) {
    vector<pair<int, int>> sciezka = Sciezka(pow * 2, pow * 2 + 1);
    vector<int> czasy;
    for (auto& p : sciezka) {
      const int zlo = plansza->Get(p.first, p.second).IleZlota();
      czasy.push_back((zlo + 9) / 10);
    }
    debug() << imie(czasy);
    const int mam = na_slupek;
    vector<int> gdzie(mam);
    iota(gdzie.begin(), gdzie.end(), 0);
    Ogarniacz ogarniacz(mam, czasy);
    debug() << imie(ogarniacz.podzial);
    nast = 0;
    while (true) {
      auto& tura = Tura(nast++);
      bool cos_bylo = false;
      for (int i = mam - 1; i >= 0; i--) {
        debug() << imie(i) imie(gdzie[i]) imie(sciezka[gdzie[i]]);
        if (ogarniacz.MamStop(i, gdzie[i])) {
          debug() << "NIE_1" imie(i);
          cos_bylo = true;
          continue;
        }
        if (i < mam - 1 and gdzie[i] + 1 == gdzie[i + 1]) {
          debug() << "NIE_2" imie(i);
          continue;
        }
        if (gdzie[i] + 1 == (int) sciezka.size()) {
          debug() << "NIE_3" imie(i);
          continue;
        }
        // W prawo.
        cos_bylo = true;
        const int g = gdzie[i];
        const int y = ++gdzie[i];
        debug() << "MOVE " << sciezka[g].first << " " << sciezka[g].second
                << " " << sciezka[y].first << " " << sciezka[y].second;
        tura.emplace_back(new Move(sciezka[g].first, sciezka[g].second,
                                   sciezka[y].first, sciezka[y].second));
      }
      //Przytnij(); nast = 0; int x; cin >> x;
      if (!cos_bylo) break;
    }
  }
  Przytnij();

  odw_cnt.clear();
  odw_cnt.resize(20, vector<vector<pair<int, pair<int, int>>>>(20));
  for (int r = 1; r < 18; r++) {
    for (int c = 1; c < 20; c++) {
      odw_cnt[r][c].emplace_back(1000, make_pair(r - 1, c));
    }
  }
  for (int c = 1; c < 20; c++) {
    odw_cnt[0][c].emplace_back(1000, make_pair(0, c - 1));
  }
  for (int r = 0; r < 19; r++) {
    odw_cnt[r][0].emplace_back(1000, make_pair(r + 1, 0));
  }
  for (int c = 0; c < 19; c++) {
    odw_cnt[n - 1][c].emplace_back(1000, make_pair(n - 1, c + 1));
    if (c > 0) {
      odw_cnt[n - 2][c + 1].emplace_back(1000, make_pair(n - 2, c));
    }
  }
  odw_cnt[n - 1][n - 1].emplace_back(1000, make_pair(n - 2, n - 1));
  kolejnosc.clear();
  for (int i = 1; i <= 19; i++) kolejnosc.emplace_back(18, i);
  for (int i = 19; i >= 0; i--) kolejnosc.emplace_back(19, i);
  for (int i = 18; i >= 0; i--) kolejnosc.emplace_back(i, 0);
  for (int i = 1; i <= 19; i++) kolejnosc.emplace_back(0, i);
  for (int r = 1; r <= 17; r++) {
    for (int c = 1; c <= 19; c++) {
      kolejnosc.emplace_back(r, c);
    }
  }
  pozycje.clear();
  for (int r = 0; r < 20; r++) {
    for (int c = 0; c < 20; c++) {
      if (plansza->Get(r, c).unit) {
        pozycje.insert(make_pair(r, c));
      }
    }
  }
  while (Splaw(Tura(0), odw_cnt, kolejnosc, pozycje)) {
    Przytnij();
    if (!plansza->Get(0, 0).unit) break;
  }
}

void Wypisz() {
  for (auto& tura : ruchy) {
    for (auto& ruch : tura) {
      ruch->Print(cout);
    }
    cout << "=" << endl;
  }
  cout << "===" << endl;
}

int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t, k;
  cin >> t >> k;
  while (t--) {
    Plansza p;
    plansza = &p;
    cin >> p;
    bool sa_kamienie = false;
    for (auto& row : p.pola) {
      for (auto& r : row) {
        if (r->JestSkala()) {
          sa_kamienie = true;
        }
      }
    }
    if (!sa_kamienie) {
      BezKamieni();
    } else {
      assert(false);
    }
    Wypisz();
    ruchy.clear();
    cut = 0;
  }
  return 0;
}