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
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
// Generated by include.py
// Jakub Staroń, 2016
// For more info see https://github.com/staronj/ProgrammingContestsLibrary
// Jakub Staroń, 2016

// Jakub Staroń, 2016

#include <iostream>
#include <sstream>
#include <iterator>
#include <type_traits>
#include <algorithm>
#include <memory>
#include <utility>
#include <functional>
#include <bitset>
#include <vector>
#include <array>
#include <list>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cinttypes>
#include <ctime>
#include <cctype>
#include <cstring>
#include <random>
#include <cassert>

namespace lib {

using int8    = std::int8_t;
using uint8   = std::uint8_t;
using int16   = std::int16_t;
using uint16  = std::uint16_t;
using int32   = std::int32_t;
using uint32  = std::uint32_t;
using int64   = std::int64_t;
using uint64  = std::uint64_t;

/**
 * Random number generator. Returns 32 bits numbers.
 *
 * You should prefer this random number generator over
 * C's rand(), because rand() max range is not strictly
 * defined and can be supringsly small (like 10000).
 */
std::knuth_b Random(time(0));

/**
 * 32 bits random generator.
 */
uint32 Random32() {
  return Random();
}

/**
 * 64 bits random generator.
 */
uint64 Random64() {
  return uint64(Random32()) << 32 | uint64(Random32());
}

#ifdef __SIZEOF_INT128__
#define HAVE_INT128_TYPES
#endif

#define USE_INT128_TYPES_IF_AVAILABLE 0

#if defined(HAVE_INT128_TYPES) && USE_INT128_TYPES_IF_AVAILABLE
#define USE_INT128_TYPES
#endif

#ifdef USE_INT128_TYPES
using int128 = __int128;
using uint128 = unsigned __int128;
#endif

using bit_vector = std::vector<bool>;

using char_pair     = std::pair<char, char>;
using bool_pair     = std::pair<bool, bool>;
using int32_pair    = std::pair<int32, int32>;
using uint32_pair   = std::pair<uint32, uint32>;
using int64_pair    = std::pair<int64, int64>;
using uint64_pair   = std::pair<uint64, uint64>;

} // namespace lib
// Jakub Staroń, 2016

// Jakub Staroń, 2016


namespace lib {

// post-incrementation -> pre-incrementation
template <typename T>
inline T operator++(T& obj, int) {
  T temp = obj;
  ++obj;
  return temp;
}

// post-decrementation -> pre-decrementation
template <typename T>
inline T operator--(T& obj, int) {
  T temp = obj;
  --obj;
  return temp;
}

// obj += n -> obj + n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator+=(T& obj, Integral n) {
  obj = obj + n;
  return obj;
}

// obj1 += obj2 -> obj1 + obj2
template <typename T>
inline T& operator+=(T& obj1, const T& obj2) {
  obj1 = obj1 + obj2;
  return obj1;
}

// obj -= n -> obj - n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator-=(T& obj, Integral n) {
  obj = obj - n;
  return obj;
}

// obj1 -= obj2 -> obj1 - obj2
template <typename T>
inline T& operator-=(T& obj1, const T& obj2) {
  obj1 = obj1 - obj2;
  return obj1;
}

// obj *= n -> obj * n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator*=(T& obj, Integral n) {
  obj = obj * n;
  return obj;
}

// obj1 *= obj2 -> obj1 * obj2
template <typename T>
inline T& operator*=(T& obj1, const T& obj2) {
  obj1 = obj1 * obj2;
  return obj1;
}

// obj %= n -> obj % n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator%=(T& obj, Integral n) {
  obj = obj % n;
  return obj;
}

// obj1 %= obj2 -> obj1 % obj2
template <typename T>
inline T& operator%=(T& obj1, const T& obj2) {
  obj1 = obj1 % obj2;
  return obj1;
}

// n + obj -> obj + n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T>::type
operator+(Integral n, const T& obj) {
  return obj + n;
}

// n * obj -> obj * n
template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T>::type
operator*(Integral n, const T& obj) {
  return obj * n;
}

// obj - n -> obj + (-n)
template <typename T, typename Integral>
inline typename std::enable_if<std::is_signed<Integral>::value, T>::type
operator-(const T& obj, Integral n) {
  return obj + (-n);
}

// obj -= n -> obj += (-n)
template <typename T, typename Integral>
inline typename std::enable_if<std::is_signed<Integral>::value, T&>::type
operator-=(const T& obj, Integral n) {
  return obj += (-n);
}

// lhs > rhs -> rhs < lhs
template <typename T>
inline bool operator>(const T& lhs, const T& rhs) {
  return rhs < lhs;
}

// lhs <= rhs -> !(rhs < lhs)
template <typename T>
inline bool operator<=(const T& lhs, const T& rhs) {
  return !(rhs < lhs);
}

// lhs >= rhs -> !(rhs < lhs)
template <typename T>
inline bool operator>=(const T& lhs, const T& rhs) {
  return rhs <= lhs;
}

// lhs >= rhs -> !(rhs < lhs)
template <typename T>
inline bool operator!=(const T& lhs, const T& rhs) {
  return !(lhs == rhs);
}

// + for pairs
template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator+(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first + rhs.first, lhs.second + rhs.second);
}

// - for pairs
template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator-(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first - rhs.first, lhs.second - rhs.second);
}

// % for pairs
template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator%(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first % rhs.first, lhs.second % rhs.second);
}

// * for pairs
template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator*(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first * rhs.first, lhs.second * rhs.second);
}

// % for (pair, Integral)
template <typename Integral1, typename Integral2, typename Integral3>
constexpr std::pair<Integral1, Integral2> operator%(const std::pair<Integral1, Integral2>& lhs, const Integral3& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first % rhs, lhs.second % rhs);
}

// * for (pair, Integral)
template <typename Integral1, typename Integral2, typename Integral3>
constexpr std::pair<Integral1, Integral2> operator*(const std::pair<Integral1, Integral2>& lhs, const Integral3& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first * rhs, lhs.second * rhs);
}


} // namespace lib

namespace lib {

/**
 * type_traits-like predicate. Equal to true_type if argument is iterator.
 *
 * Example:
 * <pre>
 * static_assert(is_iterator<std::vector<int>::iterator>::value, "vector iterator is not iterator! help!");
 * </pre>
 */
template<typename Iterator>
struct is_iterator {
private:
  template<typename I> static constexpr auto test(int)
  -> decltype(
      *std::declval<const I>(),
      std::declval<const I>() == std::declval<const I>(),
      std::declval<const I>() != std::declval<const I>(),
      ++ std::declval<I&>(),
      std::declval<I&>() ++,
      std::iterator_traits<I>(),
      bool()) { return
        std::is_destructible<I>::value;
  }

  template<typename I> static constexpr bool test(...) { return false; }

public:
  static constexpr bool value = test<Iterator>(0);
};

/**
 * type_traits-like predicate. Equal to true_type if argument is iteratorable.
 *
 * Example:
 * <pre>
 * static_assert(is_iteratorable<std::vector<int>>::value, "vector should be iterable!");
 * </pre>
 */
template<typename T>
struct is_iterable {
  template <typename C>
  using begin_t = decltype(std::begin(std::declval<C&>()));

  template <typename C>
  using end_t = decltype(std::end(std::declval<C&>()));

  template<typename I> static constexpr auto test(int)
  -> decltype(
      std::declval<begin_t<I>>(),
      std::declval<end_t<I>>(),
      bool()) { return
        std::is_same<begin_t<I>, end_t<I>>::value &&
        is_iterator<begin_t<I>>::value;
  }

  template<typename I> static constexpr bool test(...) { return false; }

public:
  static constexpr bool value = test<T>(0);
};

/**
 * Random access iterator for iterating over integral type.
 *
 * Example:
 * <pre>
 * std::vector<int> v(counting_iterator<int>(0), counting_iterator<int>(5));
 * </pre>
 *
 * Produces v with elements {0, 1, 2, 3, 4}
 */
template <typename Integral>
class counting_iterator {
public:
  static_assert(std::is_integral<Integral>::value, "counting_iterator's template argument should be integral");
  using self_type = counting_iterator;
  using value_type = Integral;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = typename std::make_signed<Integral>::type;
  using iterator_category = std::random_access_iterator_tag;

  counting_iterator(): value_() { }

  explicit counting_iterator(Integral n): value_(n) { }

  reference operator*() const {
    return value_;
  }

  self_type& operator++() {
    ++value_;
    return *this;
  }

  self_type& operator--() {
    --value_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    value_ += n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.value_ + n);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ - rhs.value_;
  }

  value_type operator[](difference_type n) {
    return value_type(value_ + n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ < rhs.value_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ == rhs.value_;
  }

private:
  Integral value_;
};

/**
 * Helper function for building counting_iterator.
 *
 * Example:
 * <pre>
 * std::vector<int> v(make_counting_iterator(0), make_counting_iterator(5));
 * </pre>
 *
 * Produces v with elements {0, 1, 2, 3, 4}
 */
template <typename Integral>
auto make_counting_iterator(Integral n) -> counting_iterator<Integral> {
  return counting_iterator<Integral>(n);
}

/**
 * Random access iterator for iterating over integral type in reversed order.
 *
 * Example:
 * <pre>
 * std::vector<int> v(reverse_counting_iterator<int>(4), reverse_counting_iterator<int>(-1));
 * </pre>
 *
 * Produces v with elements {4, 3, 2, 1, 0}
 */
template <typename Integral>
class reverse_counting_iterator {
public:
  static_assert(std::is_integral<Integral>::value, "reverse_counting_iterator's template argument should be integral");
  using self_type = reverse_counting_iterator;
  using value_type = Integral;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = typename std::make_signed<Integral>::type;
  using iterator_category = std::random_access_iterator_tag;

  reverse_counting_iterator(): value_() { }

  explicit reverse_counting_iterator(Integral n): value_(n) { }

  reference operator*() const {
    return value_;
  }

  self_type& operator++() {
    --value_;
    return *this;
  }

  self_type& operator--() {
    ++value_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    value_ -= n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.value_ - n);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return rhs.value_ - lhs.value_;
  }

  value_type operator[](difference_type n) {
    return value_type(value_ - n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return rhs.value_ < lhs.value_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ == rhs.value_;
  }

private:
  Integral value_;
};

/**
 * Helper function for building reverse_counting_iterator.
 *
 * Example:
 * <pre>
 * std::vector<int> v(make_reverse_counting_iterator(4), make_reverse_counting_iterator(-1));
 * </pre>
 *
 * Produces v with elements {4, 3, 2, 1, 0}
 */
template <typename Integral>
auto make_reverse_counting_iterator(Integral n) -> reverse_counting_iterator<Integral> {
  return reverse_counting_iterator<Integral>(n);
}

/**
 * Helper class for encapsulating iterator pair as iterable.
 *
 * Example:
 * <pre>
 * void foo(Iterator begin, Iterator end) {
 *    for (const auto& elem: iterator_range<Iterator>(begin, end)) {
 *      ...
 *    }
 * }
 * </pre>
 */
template <typename Iterator>
class iterator_range {
public:
  using iterator_type = Iterator;
  using reference = const Iterator&;

  static_assert(is_iterator<Iterator>::value, "iterator_range's template argument should be iterator!");

  explicit iterator_range(Iterator begin, Iterator end):
      begin_(std::move(begin)), end_(std::move(end)) { }

  reference begin() const {
    return begin_;
  }

  reference end() const {
    return end_;
  }

private:
  iterator_type begin_;
  iterator_type end_;
};

/**
 * Helper function for encapsulating iterator pair as iterable.
 *
 * Example:
 * <pre>
 * void foo(Iterator begin, Iterator end) {
 *    for (const auto& elem: make_range(begin, end)) {
 *      ...
 *    }
 * }
 * </pre>
 */
template <typename Iterator>
auto make_range(Iterator begin, Iterator end) -> iterator_range<Iterator> {
  return iterator_range<Iterator>(std::move(begin), std::move(end));
}

/**
 * Python-like range function.
 *
 * Example:
 * <pre>
 * for (auto i: range(0, 5)) {
 *    std::cout << i << ' ';
 * }
 * </pre>
 * Prints: 0 1 2 3 4
 *
 * You can also explicitly specify the type:
 * <pre>
 * for (auto i: range<uint32>(0, 5)) {
 *    std::cout << i << ' ';
 * }
 * </pre>
 */
template <typename Integral>
auto range(Integral begin, Integral end) ->
typename std::enable_if<std::is_integral<Integral>::value, iterator_range<counting_iterator<Integral>>>::type {
  return make_range(make_counting_iterator(begin), make_counting_iterator(end));
}

/**
 * Python-like function for iterating over reversed range.
 *
 * Example:
 * <pre>
 * for (auto i: rrange(0, 5)) {
 *    std::cout << i << ' ';
 * }
 * </pre>
 * Prints: 4 3 2 1 0
 *
 * You can also explicitly specify the type:
 * <pre>
 * for (auto i: rrange<uint32>(0, 5)) {
 *    std::cout << i << ' ';
 * }
 * </pre>
 */
template <typename Integral>
auto rrange(Integral begin, Integral end) ->
typename std::enable_if<std::is_integral<Integral>::value, iterator_range<reverse_counting_iterator<Integral>>>::type {
  return make_range(make_reverse_counting_iterator(end - 1), make_reverse_counting_iterator(begin - 1));
}

/**
 * Iterator performing transformation on values.
 *
 * Example:
 * <pre>
 * std::vector<int> v = {1, 2, 3};
 * using iterator = std::vector<int>::iterator;
 * using mapping_it = mapping_iterator<iterator, std::function<int(int)>>;
 * mapping_it it(v.begin(), [](int n) { return 2*n; });
 * mapping_it end(v.end());
 *
 * for (const auto& elem: make_range(it, end)) {
 *   std::cout << elem << ' ';
 * }
 * </pre>
 * Prints 2 4 6
 */
template <typename Iterator, typename Mapper>
class mapping_iterator {
public:
  using self_type = mapping_iterator;
  using underlying_value_type = decltype(*std::declval<Iterator>());
  using value_type = decltype(std::declval<Mapper>()(std::declval<underlying_value_type>()));
  using reference = value_type;
  using pointer = typename std::remove_reference<value_type>::type*;
  using difference_type = typename std::iterator_traits<Iterator>::difference_type;
  using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;

  mapping_iterator(const Mapper& mapper = Mapper()):
      iterator_(), mapper_(mapper) { }

  explicit mapping_iterator(Iterator iterator, const Mapper& mapper = Mapper()):
      iterator_(iterator), mapper_(mapper) { }

  reference operator*() const {
    return mapper_(*iterator_);
  }

  self_type& operator++() {
    ++iterator_;
    return *this;
  }

  self_type& operator--() {
    --iterator_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    iterator_ += n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.iterator_ + n, obj.mapper_);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ - rhs.iterator_;
  }

  value_type operator[](difference_type n) {
    return *(*this + n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ < rhs.iterator_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ == rhs.iterator_;
  }

private:
  Iterator iterator_;
  Mapper mapper_;
};

namespace detail {

template <typename ValuesIterator>
struct IndirectMapper {
  static_assert(is_iterator<ValuesIterator>::value, "first indirect_iterator's template argument must be iterator.");
  static_assert(
      std::is_same<typename std::iterator_traits<ValuesIterator>::iterator_category, std::random_access_iterator_tag>::value,
      "ValuesIterator must be random assess iterator."
  );

  using value_type = typename std::iterator_traits<ValuesIterator>::value_type;
  using reference = const value_type&;
  using index_type = typename std::iterator_traits<ValuesIterator>::difference_type;

  IndirectMapper() = default;

  IndirectMapper(ValuesIterator values):
      values_(values) { }

  reference operator()(index_type n) const {
    return *(values_ + n);
  }

private:
  ValuesIterator values_;
};

} // namespace detail

template <typename ValuesIterator, typename IndexesIterator>
using indirect_iterator = mapping_iterator<IndexesIterator, detail::IndirectMapper<ValuesIterator>>;

/**
 * Iterates over random acess iterator with usage of permutation.
 *
 * Example:
 * <pre>
 * std::vector<int> perm = {2, 0, 1};
 * std::vector<std::string> values = {"Ala", "ma", "kota"};
 * auto it = make_indirect_iterator(values.begin(), perm.begin());
 * auto end = make_indirect_iterator(values.begin(), perm.end());
 * for (const auto& elem: make_range(it, end)) {
 *    std::cout << elem << ' ';
 * }
 * </pre>
 * Prints: kota Ala ma
 */
template <typename ValuesIterator, typename IndexesIterator>
auto make_indirect_iterator(ValuesIterator values, IndexesIterator indexes) ->
indirect_iterator<ValuesIterator, IndexesIterator> {
  detail::IndirectMapper<ValuesIterator> mapper(values);
  return indirect_iterator<ValuesIterator, IndexesIterator>(std::move(indexes), std::move(mapper));
};

/**
 * Base class for generators implementation.
 *
 * Example:
 * <pre>
 * for (auto gen = new foo_generator(); gen->hasNext(); ) {
 *    auto value = gen->next();
 *    ...
 * }
 * </pre>
 */
template <typename ValueType>
class generator {
public:
  using ptr = std::shared_ptr<generator>;
  using value_type = ValueType;

  generator() = default;
  generator(const generator&) = delete;
  generator& operator=(const generator&) = delete;
  generator(generator&&) = default;
  virtual ~generator() = default;

  /**
   * Generates new value and returns it.
   *
   * If there is no next value behaviour is undefined.
   */
  virtual value_type next() = 0;

  /**
   * Returns true if generator can generate new value.
   */
  virtual bool hasNext() = 0;
};

/**
 * Iterator for iterating over values returned by generator.
 *
 * Example:
 * <pre>
 * std::vector<uint32> v(generator_iterator<uint32>(new foo_generator()), generator_iterator<uint32>());
 * </pre>
 */
template <typename ValueType>
class generator_iterator {
public:
  using self_type = generator_iterator;
  using generator_type = generator<ValueType>;
  using generator_pointer = typename generator<ValueType>::ptr;
  using value_type = typename generator_type::value_type;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = std::ptrdiff_t;
  using iterator_category = std::input_iterator_tag;

  generator_iterator() = default;

  generator_iterator(generator_type* generator):
      generator_iterator(generator_pointer(generator)) { }

  generator_iterator(generator_pointer generator):
      generator_(std::move(generator)) {
    advance();
  }

  reference operator*() const {
    return value_;
  }

  pointer operator->() const {
    return &value_;
  }

  self_type& operator++() {
    advance();
    return *this;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return (&lhs == &rhs) || (lhs.is_end() && rhs.is_end());
  }

  bool is_end() const {
    return !bool(generator_.get());
  }

private:
  void advance() {
    if (generator_->hasNext())
      value_ = generator_->next();
    else
      generator_.reset();
  }

  generator_pointer generator_;
  value_type value_;
};

/**
 * Returns iterator range to values generated by generator.
 *
 * Example:
 * <pre>
 * for (const auto& elem: iterate_generator(new foo_generator())) {
 *    ...
 * }
 * </pre>
 */
template <typename T>
iterator_range<generator_iterator<T>> iterate_generator(generator<T>* gen) {
  return make_range(generator_iterator<T>(gen), generator_iterator<T>());
}

} // namespace lib
// Jakub Staroń, 2016

// Jakub Staroń, 2016


namespace lib {

namespace detail {

const int kSimpleFancyFlagID = std::ios_base::xalloc();

enum printing_type {
  simple_printing_type = 0,
  fancy_printing_type = 1
};

class delimiter_printer {
public:
  delimiter_printer(std::ostream& stream): first_(true) {
    fancy_ = (stream.iword(kSimpleFancyFlagID) == fancy_printing_type);
  }

  const char* prefix() {
    return fancy_? "(" : "";
  }

  const char* delimiter() {
    if (!first_) {
      return fancy_? ", " : " ";
    }
    else {
      first_ = false;
      return "";
    }
  }

  const char* postfix() {
    return fancy_? ")" : "";
  }

private:
  bool first_;
  bool fancy_;
};

template <typename T>
constexpr bool allow_print_operator() {
  return
      is_iterable<T>::value &&
      !std::is_same<T, std::string>::value &&
      !std::is_same<T, const char*>::value &&
      !std::is_same<typename std::remove_extent<T>::type, char>::value;
}

} // namespace detail

/**
 * Sets stream printing mode to simple. Default option.
 *
 * Example:
 * std::cout << simple << std::make_pair(1, 2) << std::endl;
 * Prints:
 * 1 2
 */
std::ostream& simple(std::ostream& stream) {
  stream.iword(detail::kSimpleFancyFlagID) = detail::simple_printing_type;
  return stream;
}

/**
 * Sets stream printing mode to fancy. Helpful for printing debug messages.
 *
 * Example:
 * std::cout << simple << std::make_pair(1, 2) << std::endl;
 * Prints:
 * (1, 2)
 */
std::ostream& fancy(std::ostream& stream) {
  stream.iword(detail::kSimpleFancyFlagID) = detail::fancy_printing_type;
  return stream;
}

/**
 * Overload operator<< for ostream and pair.
 */
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& stream, const std::pair<T1, T2>& pair);

/**
 * Overload operator<< for ostream and tuple.
 */
template <typename... Args>
std::ostream& operator<<(std::ostream& stream, const std::tuple<Args...>& tuple);

/**
 * Overload operator<< for ostream and every iterable (eg vector, map, array).
 */
template <typename Iterable>
typename std::enable_if<detail::allow_print_operator<Iterable>(), std::ostream&>::type
operator<<(std::ostream& stream, const Iterable& iterable);

#ifdef USE_INT128_TYPES

/**
 * Overload operator<< for ostream and uint128.
 */
std::ostream& operator<<(std::ostream& stream, uint128 n) {
  constexpr int32 buffer_size = 64;
  constexpr uint128 ten = 10;
  char buffer[buffer_size];
  int32 index = buffer_size - 1;

  do {
    uint128 digit = n % 10;
    buffer[index--] = char('0' + digit);
    n /= 10;
  } while (n > 0 && index >= 0);

  stream.write(buffer + index + 1, buffer_size - index - 1);
  return stream;
}

/**
 * Overload operator<< for ostream and int128.
 */
std::ostream& operator<<(std::ostream& stream, int128 n) {
  if (n < 0) {
    stream.put('-');
    n = -n;
  }
  stream << uint128(n);
  return stream;
}

#endif

/**
 * Overload operator<< for istream and pair.
 */
template <typename T1, typename T2>
std::istream& operator>>(std::istream& stream, std::pair<T1, T2>& pair);

/**
 * Overload operator<< for istream and tuple.
 */
template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>& tuple);

/**
 * Helper for marking input as ignored.
 *
 * Example:
 * int a, b;
 * std::cin >> a >> ignore<int>() >> b;
 */
template <typename T>
struct ignore {
  ignore() = default;

  friend std::istream& operator>>(std::istream& stream, const ignore&&) {
    T ignored;
    return stream >> ignored;
  }

  friend std::istream& operator>>(std::istream& stream, const ignore&) {
    T ignored;
    return stream >> ignored;
  }
};

namespace detail {

template<std::size_t...>
struct integer_sequence{};

template <size_t N>
struct generate_sequence {
private:
  template<std::size_t M, std::size_t... Is>
  struct helper {
    using type = typename helper<M-1, M-1, Is...>::type;
  };;

  template<std::size_t... Is>
  struct helper<0, Is...> {
    using type = integer_sequence<Is...>;
  };

public:
  using type = typename helper<N>::type;
};

template <typename Functor, size_t N>
struct dynamize {
  using functor_type = Functor;
private:
  using index_sequence = typename generate_sequence<N>::type;
  using function_type = void(functor_type::*)();
  using table_type = std::array<function_type, N>;

  template <size_t... Indexes>
  static constexpr table_type build_table(integer_sequence<Indexes...>) {
    return {{&functor_type::template operator()<Indexes>...}};
  }

  static constexpr table_type functions_ = build_table(index_sequence());
public:
  dynamize(functor_type&& functor):
      functor_(std::move(functor)) { }

  void call(size_t i) {
    if (i >= N)
      throw std::out_of_range("tuple_printer: out of range");

    (functor_.*functions_[i])();
  }

private:
  functor_type functor_;
};

template <typename Functor, size_t N>
constexpr typename dynamize<Functor, N>::table_type dynamize<Functor, N>::functions_;

template <typename... Args>
class tuple_printer {
  static constexpr size_t arguments_count = sizeof...(Args);
  using tuple_type = std::tuple<Args...>;

  struct impl {
    std::ostream& stream_;
    const tuple_type& tuple_;

    template <size_t N>
    void operator()() {
      stream_ << std::get<N>(tuple_);
    }
  };

public:
  tuple_printer (std::ostream& stream, const tuple_type& tuple):
    dynamize_(impl{stream, tuple}) { }

  void print(size_t i) {
    dynamize_.call(i);
  }

private:
  dynamize<impl, arguments_count> dynamize_;
};

template <typename... Args>
class tuple_reader {
  static constexpr size_t arguments_count = sizeof...(Args);
  using tuple_type = std::tuple<Args...>;

  struct impl {
    std::istream& stream_;
    tuple_type& tuple_;

    template <size_t N>
    void operator()() {
      stream_ >> std::get<N>(tuple_);
    }
  };

public:
  tuple_reader (std::istream& stream, tuple_type& tuple):
      dynamize_(impl{stream, tuple}) { }

  void read(size_t i) {
    dynamize_.call(i);
  }

private:
  dynamize<impl, arguments_count> dynamize_;
};

} // namespace detail

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& stream, const std::pair<T1, T2>& pair) {
  detail::delimiter_printer printer(stream);
  stream << printer.prefix();
  stream << printer.delimiter() << pair.first;
  stream << printer.delimiter() << pair.second;
  stream << printer.postfix();
  return stream;
}

template <typename... Args>
std::ostream& operator<<(std::ostream& stream, const std::tuple<Args...>& tuple) {
  detail::delimiter_printer delimiter_printer(stream);
  detail::tuple_printer<Args...> tuple_printer(stream, tuple);
  stream << delimiter_printer.prefix();
  for (auto i: range<size_t>(0, sizeof...(Args))) {
    stream << delimiter_printer.delimiter();
    tuple_printer.print(i);
  }
  stream << delimiter_printer.postfix();
  return stream;
}

template <typename Iterable>
typename std::enable_if<detail::allow_print_operator<Iterable>(), std::ostream&>::type
operator<<(std::ostream& stream, const Iterable& iterable) {
  detail::delimiter_printer printer(stream);
  stream << printer.prefix();
  for (const auto& elem: iterable) {
    stream << printer.delimiter() << elem;
  }
  stream << printer.postfix();
  return stream;
}

template <typename T1, typename T2>
std::istream& operator>>(std::istream& stream, std::pair<T1, T2>& pair) {
  return stream >> pair.first >> pair.second;
}

template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>& tuple) {
  detail::tuple_reader<Args...> tuple_reader(stream, tuple);
  for (auto i: range<size_t>(0, sizeof...(Args))) {
    tuple_reader.read(i);
  }
  return stream;
}


/**
 * Second version of overload operator>> for istream and pair.
 *
 * Needed to allow constructions like
 * int a, b, c;
 * std::cin >> std::tie(a, b, c);
 */
template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>&& tuple) {
  return stream >> tuple;
}

/**
 * Python-like print function.
 *
 * Example:
 * print(std::cerr, "1 + 2 = %0, 2 + 3 = %1", 3, 5);
 */
template <typename... Args>
void print(std::ostream& stream, const char* format, const Args&... args) {
  auto tuple = std::make_tuple(std::cref(args)...);
  detail::tuple_printer<const Args&...> tuple_printer(stream, tuple);
  constexpr char null = '\0';
  constexpr char percent = '%';
  for (const char* it = format, *prev = format; *it != '\0'; ) {
    while (*it != null && *it != percent)
      it++;

    stream.write(prev, it - prev);
    if (*it == percent) {
      ++it;
      if (*it == percent) {
        stream.put(percent);
      }
      else if (std::isdigit(*it)) {
        size_t index = size_t(*it - '0');
        tuple_printer.print(index);
      }
      else {
        throw std::invalid_argument("print - invalid character after %");
      }
      prev = ++it;
    }
  }
  stream.put('\n');
}

/**
 * Python-like print function. Prints to std::cout.
 */
template <typename... Args>
void print(const char* format, const Args&... args) {
  print(std::cout, format, args...);
}

/**
 * flush operator for usage with print.
 *
 * Example
 * print("important message%0", lib::flush);
 */
std::ostream& flush(std::ostream& stream) {
  return stream.flush();
}

/**
 * Python-like read function.
 *
 * Example:
 * int a, b;
 * read(std::cin, a, ignore<int>(), b);
 */
template <typename... Args>
void read(std::istream& stream, Args&&... args) {
  auto tuple = std::make_tuple(std::ref(args)...);
  stream >> tuple;
}

/**
 * Generator for reading lines from std::istream.
 */
class lines_generator: public generator<std::string> {
public:
  lines_generator(std::istream* stream):
      stream_(stream) { }

  value_type next() final {
    if (!stream_->good())
      throw std::ios::failure("lines_generator - stream in invalid state after line read.");
    else
      std::getline(*stream_, line_);
    return line_;
  }

  bool hasNext() final {
    return !stream_->eof();
  }

private:
  std::istream* stream_;
  std::string line_;
};

/**
 * Input iterator for reading lines from input.
 */
class lines_iterator: public generator_iterator<std::string> {
public:
  lines_iterator() = default;

  lines_iterator(std::istream* stream):
      generator_iterator(new lines_generator(stream)) { }
};

/**
 * Returns range of lines_iterator to iterate over all lines in stream.
 */
iterator_range<lines_iterator> iterate_lines(std::istream& stream) {
  return make_range(lines_iterator(&stream), lines_iterator());
}

} // namespace lib

namespace lib {
namespace logging {

enum class log_level {
  debug = 15,
  info = 10,
  error = 5,
  none = 0
};

class Logger {
public:
  friend Logger& get_logger(const std::string& name);

  Logger(Logger&& other):
      level_(other.level_),
      streams_(std::move(other.streams_)) { }

  Logger(const Logger&) = delete;
  Logger& operator=(const Logger&) = delete;

  template <typename... Args>
  void log(log_level level, const char* format, const Args&... args) {
    if (level > level_)
      return;

    for (const auto& pair: streams_) {
      if (level > pair.second)
        continue;

      print(*pair.first, format, args...);
    }
  }

  template <typename... Args>
  void debug(const char* format, const Args&... args) {
    log(log_level::debug, format, args...);
  }

  template <typename... Args>
  void info(const char* format, const Args&... args) {
    log(log_level::info, format, args...);
  }

  template <typename... Args>
  void error(const char* format, const Args&... args) {
    log(log_level::error, format, args...);
  }

  void add_stream(std::ostream* stream, log_level level) {
    streams_.emplace_back(stream, level);
  }

  void set_log_level(log_level level) {
    level_ = level;
  }

protected:
  Logger() = default;

private:
  log_level level_ = log_level::debug;
  std::vector<std::pair<std::ostream*, log_level>> streams_;
};

Logger& get_logger(const std::string& name) {
  static std::map<std::string, Logger> loggers;
  auto it = loggers.find(name);

  if (it == loggers.end()) {
    it = loggers.emplace(name, Logger()).first;
  }
  return it->second;
}

} // namespace logging
} // namespace lib

using namespace lib;

logging::Logger& logger = logging::get_logger("kar");

const char* const kWinning =   "WYGRANA";
const char* const kTying =     "REMIS";
const char* const kLoosing =   "PRZEGRANA";


class Application {
public:
  void Run() {
    std::vector<uint32> WinningWith(N, 0);
    std::vector<uint32> TyingWith(N, N);

    for (auto i: range<uint32>(0, M)) {
      uint32 a, b;
      char w;
      read(std::cin, a, w, b);
      a--, b--;

      logger.info("a = %0, w = %1, b = %2", a, w, b);

      if (w == '>') {
        TyingWith.at(b)--;
      }
      else if (w == '<') {
        WinningWith.at(b)++;
      }
      else {
        logger.error("Invalid type of edge: %0", w);
      }
    }

    auto Winning = std::count_if(WinningWith.begin(), WinningWith.end(),
                                 [](uint32 n) {
                                   return n > 0;
                                 });

    auto Tying = std::count_if(TyingWith.begin(), TyingWith.end(),
                                 [](uint32 n) {
                                   return n > 0;
                                 });

    if (Winning == N) {
      print(kLoosing); // Second player wins
    }
    else if (Tying == N) {
      print(kTying);
    }
    else {
      print(kWinning); // Second player can't defend himself
    }
  }

  void LoadData() {
    read(std::cin, N, M);
  }

private:
  uint32 N, M;
};

int main(int argc, const char* argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  logger.set_log_level(logging::log_level::none);
  logger.add_stream(&std::cerr, logging::log_level::debug);
  uint32 T;
  read(std::cin, T);
  for (auto i: range<uint32>(0, T)) {
    logger.info("test no = %0", i);
    Application application;
    application.LoadData();
    application.Run();
  }
  return 0;
}