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
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
//#define READ_NUM_TEST_CASES
//#define ASSERTS		// force asserts on online judge
#define FASTIO

//#include<atablash/splay/common.h>
//#include<atablash/splay/node.h>



#include<vector>




#include<stdint.h>

#include<utility>

#include<vector>
#include<memory>




// abbrevs

typedef unsigned int uint;

typedef int8_t i8;
typedef uint8_t ui8;
typedef int16_t i16;
typedef uint16_t ui16;
typedef int32_t i32;
typedef uint32_t ui32;
typedef int64_t i64;
typedef uint64_t ui64;

// old:
typedef i64 ll;
typedef ui64 ull;

typedef std::pair<i32,i32> pii;
typedef std::pair<double,double> pdd;

typedef std::vector<i32> vi;
#define VE std::vector

#define umap std::unordered_map
#define umultimap std::unordered_multimap
#define uset std::unordered_set
#define umultiset std::unordered_multiset

typedef unsigned short ushort;
typedef unsigned char uchar;


template<class T> using sptr = std::shared_ptr<T>;
template<class T> using uptr = std::unique_ptr<T>;
template<class A, class B> using p = std::pair<A, B>;



namespace ab{

struct Void {};


// convert to float with enough precision
template<typename I>struct FloatFrom{typedef Void R;};
template<>struct FloatFrom<i32>{typedef double R;};
template<>struct FloatFrom<ui32>{typedef double R;};
template<>struct FloatFrom<i64>{typedef long double R;};
template<>struct FloatFrom<ui64>{typedef long double R;};
template<>struct FloatFrom<i16>{typedef float R;};
template<>struct FloatFrom<ui16>{typedef float R;};
template<>struct FloatFrom<i8>{typedef float R;};
template<>struct FloatFrom<ui8>{typedef float R;};
template<>struct FloatFrom<float>{typedef float R;};
template<>struct FloatFrom<double>{typedef double R;};
template<>struct FloatFrom<long double>{typedef long double R;};

// get type capable of storing multiplication result
template<typename I>struct Double{typedef Void R;};
template<>struct Double<i8>{typedef i16 R;};
template<>struct Double<ui8>{typedef ui16 R;};
template<>struct Double<i16>{typedef i32 R;};
template<>struct Double<ui16>{typedef ui32 R;};
template<>struct Double<i32>{typedef i64 R;};
template<>struct Double<ui32>{typedef ui64 R;};

template<>struct Double<float>{typedef float R;};
template<>struct Double<double>{typedef double R;};
template<>struct Double<long double>{typedef long double R;};



#if defined __GNUC__ && ( defined __x86_64__ || defined __ppc64__ || defined __MINGW64__ )
typedef __int128_t i128;
typedef __uint128_t ui128;
template<>struct Double<i64>{typedef i128 R;};
template<>struct Double<ui64>{typedef ui128 R;};
#endif


}


// enable debug if any debug macro is defined
#if (defined _DEBUG || defined DEBUG) && !defined DEBUG_BUILD
#define DEBUG_BUILD
#endif

// DEBUG_BUILD (set or not set) overrides NDEBUG
#ifdef DEBUG_BUILD
	#undef NDEBUG
	//#pragma message "Compiling in DEBUG mode."
#else
	#ifndef NDEBUG
	#define NDEBUG
	#endif
#endif

//#include <cstdio>
//#include <cfloat>




//#include<type_traits>


#define _CAT(a,b) a##b
#define CAT(a,b) _CAT(a,b)
#define _10TH(a,b,c,d,e,f,g,h,i,X,...) X
#define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1,0)
#define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__)

#define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0,0)
#define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__)


#define MIN_GCC(a,b,c) (__GNUC__ > (a) || \
              (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \
                                 (__GNUC_MINOR__ == (b) && \
								 __GNUC_PATCHLEVEL__ >= (c)))))





// TODO: po co to bylo??
/*
#include<functional>
#if __cplusplus >= 201103L
namespace std
{
    template<class T>
    struct hash
    {
        typedef T argument_type;
        typedef typename std::underlying_type< argument_type >::type underlying_type;
        typedef typename std::hash< underlying_type >::result_type result_type;
        result_type operator()( const argument_type& arg ) const
        {
            std::hash< underlying_type > hasher;
            return hasher( static_cast< underlying_type >( arg ) );
        }
    };
}
#endif
*/


#define bac back()
#define fro front()
#define emp empty()
#define fi first
#define se second
#define siz size()
#define ins insert
#define era erase
#define mp make_pair
#define be begin()
#define en end()
#define rbe rbegin()
#define ren rend()
#define pub push_back
#define puf push_front
#define pob pop_back()
#define pof pop_front()
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define pq priority_queue
#define si (int)


#undef FO
#undef OF

#define FOR(i,n) for(int i=0; i<(int)(n); ++i)
#define FO(i,a,b) for(int i=(a); i<(int)(b); ++i)
#define OF(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);--i)
#define FE(it,c)  for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define FER(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); )
#define ALL(c) (c).begin(),(c).end()
#define ZERO(c) memset(c,0,sizeof(c))

#define AU(v,val) __typeof(val) v(val)

const char NL='\n';
const char SP=' ';






// Visual C++ intristics
#ifdef _MSC_VER
	#include <intrin.h>
	#define __builtin_popcount __popcnt

	#ifdef _WIN64
		#define __builtin_popcountll __popcnt64
	#else
		inline int __builtin_popcountll(__int64 a){ return __builtin_popcount((ui32)a) + __builtin_popcount(a >> 32); }
	#endif 
#endif


// Visual C++ 'and', 'or', 'not'
#ifdef _MSC_VER
	#define and &&
	#define or ||
	#define not !
#endif


namespace ab{

template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));} // cycle bits left
inline int popcount(uint i){return __builtin_popcount(i);}
inline int popcount(ull i){return __builtin_popcountll(i);}

#ifdef __GNUC__
inline int parity(uint i){return __builtin_parity(i);}
inline int parity(ull i){return __builtin_parityll(i);}
inline int ilog2(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i))
inline int ilog2(ull i){return 63-__builtin_clzll(i);}
#endif


template<class T>inline bool isPowerOf2(const T&x){return !(x&(x-1));}



struct Static{
	Static() = default;
	Static(const Static&) = delete;
	Static(Static&&) = delete;
	Static(const Static&&) = delete;
	Static& operator=(const Static&) = delete;
	Static& operator=(Static&&) = delete;
	Static& operator=(const Static&&) = delete;
};



}






#include<vector>
#include<unordered_map>


// ?
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<mutex>

// to_string fix for MinGW





#include<map>
#include<iostream>



#if __cplusplus >= 201103L or defined _MSC_VER
#include <unordered_map>
#include <unordered_set>
namespace std{
#else
#include <ext/hash_map>
#include <ext/hash_set>
namespace __gnu_cxx{
#define unordered_map hash_map
#define unordered_multimap hash_multimap
#define unordered_set hash_set
#define unordered_multiset hash_multiset
/*
template<class K, class V> class unordered_map : public hash_map<K,V>{
public:
	typename hash_map<K,V>::iterator erase(const typename hash_map<K,V>::iterator& it){
		typename hash_map<K,V>::iterator r = it;
		++r;
		if(r!=hash_map<K,V>::end()){
			K key = r->first;
			hash_map<K,V>::erase(it);
			return hash_map<K,V>::find(key);
		}
		else{
			hash_map<K,V>::erase(it);
			return r;
		}
	}
	typename hash_map<K,V>::size_type erase(const K& key){
		return hash_map<K,V>::erase(key);
	}
};
*/
#endif

// 1,2,last (!!!!!)
/*template<>*/ template<class A,class B>struct hash<std::map<A,B> >{
    size_t operator()(const std::map<A,B>& m) const{
        size_t r = m.size();
        if(!r)return 0;
        __typeof(m.begin()) i=m.be;
        r^=hash<__typeof(i)>()(i);
        ++i;
        r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/4);
        i=m.end();
        --i;
        r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/2);
        return r;
    }
};



// hash pairs:
/*template<>*/ template<class A, class B>struct hash<pair<A,B> >{
    size_t operator()(const pair<A,B>& p) const{
        return hash<A>()(p.first) ^ ab::cbitl(hash<B>()(p.second),sizeof(size_t)*8/2);
    }
};



// hash enums:
//
// TODO: fix for msvc
#ifdef __GNUC__
template<class E> struct hash {
    using sfinae = typename std::enable_if<std::is_enum<E>::value, E>::type;
public:
    size_t operator()(const E&e) const {
      return std::hash<typename std::underlying_type<E>::type>()((typename std::underlying_type<E>::type)e);
    }
};
#endif
  
} // namespace std / __gnu_cxx








// component-wise pair operators
template<class A, class B, class C, class D>
inline std::pair<A,B>& operator+=(std::pair<A,B>& p, const std::pair<C,D>& o){
	p.first+=o.first;
	p.second+=o.second;
	return p;
}
template<class A, class B, class C, class D>
inline std::pair<A,B>& operator-=(std::pair<A,B>& p, const std::pair<C,D>& o){
	p.first-=o.first;
	p.second-=o.second;
	return p;
}
template<class A, class B, class C, class D>
inline std::pair<A,B>& operator*=(std::pair<A,B>& p, const std::pair<C,D>& o){
	p.first*=o.first;
	p.second*=o.second;
	return p;
}
template<class A, class B, class C, class D>
inline std::pair<A,B>& operator/=(std::pair<A,B>& p, const std::pair<C,D>& o){
	p.first/=o.first;
	p.second/=o.second;
	return p;
}

template<class Afi, class Ase, class Bfi, class Bse>
inline std::pair<decltype(Afi() + Bfi()), decltype(Ase() + Bse())>&
operator+(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){
	return std::make_pair(a.fi+b.fi, a.se+b.se);
}
template<class Afi, class Ase, class Bfi, class Bse>
inline std::pair<decltype(Afi() - Bfi()), decltype(Ase() - Bse())>&
operator-(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){
	return std::make_pair(a.fi-b.fi, a.se-b.se);
}
template<class Afi, class Ase, class Bfi, class Bse>
inline std::pair<decltype(Afi()*Bfi()), decltype(Ase()*Bse())>&
operator*(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){
	return std::make_pair(a.fi*b.fi, a.se*b.se);
}
template<class Afi, class Ase, class Bfi, class Bse>
inline std::pair<decltype(Afi() / Bfi()), decltype(Ase() / Bse())>&
operator/(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){
	return std::make_pair(a.fi/b.fi, a.se/b.se);
}








// vector operators
template<class T, class U>
inline std::vector<T>& operator+=(std::vector<T>& v, const std::vector<U>& o){
	ASS(v.siz == o.siz);
	FOR(i,v.siz) v[i] += o[i];
	return v;
}


template<class T>
inline std::vector<T>& operator*=(std::vector<T>& v, const T& a){
	FOR(i,v.siz) v[i] *= a;
	return v;
}





// serialization
template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::pair<A,B>&p){o<<'<'<<p.first<<", "<<p.second<<'>';return o;}
template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::map<A,B>&m){o<<'\n';FE(k,m)o<<"map["<<k->first<<"] -> "<<k->se<<'\n';return o;}
template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const umap<A,B>&m){o<<'\n';FE(k,m)o<<"umap["<<k->first<<"] -> "<<k->se<<'\n';return o;}
template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const uset<A,B>&_m){o<<"uset{\n";FE(k,_m)o<<*k<<'\n';o<<'\n';return o;}
template<class A>inline std::ostream&operator<<(std::ostream&o,const std::vector<A>&_v){o<<'\n';FOR(i,_v.siz)o<<"vector["<<i<<"] = "<<_v[i]<<"}\n";return o;}





// fix for MinGW: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015
#ifdef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
namespace std{
	template<class T>
	inline string to_string(const T& t){
		stringstream ss;
		ss << t;
		return ss.str();
	}
}
#endif





//#define  LOG0(str) 			fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__)
//#define  LOG1(str,...)		fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__)
//#define LOG(...) VA_GTONE(LOG,__VA_ARGS__)


#define LOGD(str) if(ab::Log::global()) ab::Log::global()->debug(str,__FILE__,__LINE__)
#define LOGI(str) if(ab::Log::global()) ab::Log::global()->info(str,__FILE__,__LINE__)
#define LOGW(str) if(ab::Log::global()) ab::Log::global()->warning(str,__FILE__,__LINE__)
#define LOGE(str) if(ab::Log::global()) ab::Log::global()->error(str,__FILE__,__LINE__)
#define LOGF(str) if(ab::Log::global()) ab::Log::global()->fail(str,__FILE__,__LINE__)

	


namespace ab{


// TODO: czas logowania
// TODO: interfejs do dodawania nazw message levelom, ustawiania nazwy pliku z logiem, czy printowac na stderr, ...
//
// level >= DEBUG powoduje ignorowanie logowania w release
class Log{
public:
	enum{
		DEBUG = 5000,
		INFO = 4000, // default?
		WARNING = 3000,
		ERROR = 2000,
		FAIL = 1000};

public:
	void debug(const std::string& _message, const std::string& file="", int line=0){
		message(DEBUG, _message, file, line);
	}
	void info(const std::string& _message, const std::string& file = "", int line = 0){
		message(INFO, _message, file, line);
	}
	void warning(const std::string& _message, const std::string& file = "", int line = 0){
		message(WARNING, _message, file, line);
	}
	void error(const std::string& _message, const std::string& file = "", int line = 0){
		message(ERROR, _message, file, line);
	}
	void fail(const std::string& _message, const std::string& file = "", int line = 0){
		message(FAIL, _message, file, line);
	}

	// slow
	void flushConsole(){
		// avoid slow cout during lock
		// copy messages to print
		static const uint max_messages_flush = 128;
		std::vector<Message> messages_copy;
		{
			std::lock_guard<std::mutex> lock(mut);
			messages_copy.assign(messages.begin() + numPrinted, messages.begin() + numPrinted + std::min(max_messages_flush, (uint)messages.size() - numPrinted));
		}

		for (uint i = 0; i < messages_copy.size(); ++i){
			std::cout << messageToString(messages_copy[i]) << '\n';
		}
		std::cout.flush();

		numPrinted += messages_copy.size();
	}

	void message(int level, const std::string& messageText, const std::string& file = "", int line = 0){

		#if !defined DEBUG_BUILD
			if (level >= DEBUG) return;
		#endif
		
		std::lock_guard<std::mutex> lock(mut);

		messages.push_back(Message{ level, messageText, file, line });
		
		//if(autoFlush) flushConsole();

		#ifdef DEBUG_BUILD
			if (level <= ERROR){
				#ifdef _MSC_VER
					__debugbreak();
				#endif
				#ifdef __GNUC__
					__builtin_trap();
				#endif
			}
		#endif

	}


	// reference to pointer. client should create the log himself
	static Log*& global(){
		static Log* log = nullptr;
		return log;
	}

public:
	Log(){
		levelNames[DEBUG] = "DEBUG";
		levelNames[INFO] = "INFO";
		levelNames[WARNING] = "WARNING";
		levelNames[ERROR] = "ERROR";
		levelNames[FAIL] = "FAIL";
	}
	~Log(){
		if (filename != ""){
			std::ofstream os(filename);
			for (auto&m : messages){
				os << messageToString(m) << '\n';
			}
		}
	}
	
	//bool autoFlush = false;

private:
	struct Message{
		int level;
		std::string text;
		std::string file;
		int line;
	};
	
	static std::string shortenPath(const std::string& s){
		std::string result = s;
		std::string ss[] = {"include/", "include\\", "src/", "src\\"};
		for(uint i=0; i<sizeof(ss)/sizeof(std::string); ++i){
			auto pos = result.rfind(ss[i]);
			if(pos == std::string::npos) continue;
			
			result = result.substr(pos + ss[i].size(), std::string::npos);
		}
		return result;
	}

	std::string messageToString(const Message& m) const {
		std::string levelName;
		auto r = levelNames.find(m.level);
		if (r == levelNames.end()) levelName = std::to_string(m.level);
		else levelName = r->second;
		std::string result = levelName + ": " + m.text;
		if (m.file != "") result += " at " + shortenPath(m.file) + ":" + std::to_string(m.line);
		return result;
	}

	uint numPrinted = 0;
	std::vector<Message> messages;
	std::mutex mut; // TODO: spinlock

	std::unordered_map<int, std::string> levelNames;
	std::string filename = "atablash.log";

private:
	Log(const Log&) = delete;
	Log& operator=(const Log&) = delete;
};










}





inline void breakpoint(){}
#ifdef NDEBUG
	#define DE if(0)
#else // DEBUG
	#define DE
#endif


#if defined NDEBUG && not defined ASSERTS
	#define ASS(x)
	#define FLOAT_OK(x)
	#define ASSERT_FLOAT(x)
#else
	#include<cassert>
	#define ASS(x) assert(x)
	#define FLOAT_OK(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX)
	#define ASSERT_FLOAT(f) ASS(FLOAT_OK(f))
#endif

#define DV(x) (E<#x<": "<(x)<N)
#define XX E<"XX at line "<__LINE__<NL;


#if defined DEBUG_BUILD or defined COLOR_CONSOLE
	#ifdef _WIN32
	
		#ifndef NOMINMAX
		#define NOMINMAX
		#endif
		
		#ifndef WIN32_LEAN_AND_MEAN
		#define WIN32_LEAN_AND_MEAN
		#endif
		
		#include<windows.h>
		inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);}	// color 12
		inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);}	// color 10
	#else
		// *nix includes for debug
	#endif
#else
	#define ec(x)
	#define oc(x)
#endif



#include <iostream>
struct _E{template<class T>_E&operator<(const T&_t){DE{ec(1);std::cerr<<_t;ec(0);}return*this;}};





#ifdef __GNUC__
	#pragma GCC diagnostic push
	#pragma GCC diagnostic ignored "-Wunused-variable"	// not working
#endif

static _E E;

#ifdef __GNUC__
	#pragma GCC diagnostic pop
#endif




// E unused
#ifdef __GNUC__
	template<class T>void ___fhsdhsdhrtsh(){(void)E;}
#endif






namespace ab
{

	/*
	template<typename Ret, typename T>
	class Binder
	{
	public:
		Binder(Ret T::*_var) : var(_var) {}
		Ret& operator()(T& obj) const
		{
			return obj.*var;
		}
		const Ret& operator()(const T& obj) const
		{
			return obj.*var;
		}
	private:
		Ret T::*var;
	};

	template<typename Ret, typename T>
	Binder<Ret,T> bind(Ret T::*var)
	{
		return Binder<Ret,T>(var);
	}*/
	
	
	
	
	
	/*
	
	template<class A, class B>
	__typeof(A()+B()) plus(const A& a, const B& b){return a+b;}
	
	template<class A, class B>
	__typeof(A()-B()) minus(const A& a, const B& b){return a-b;}
	
	template<class A, class B>
	__typeof(A()*B()) multiplies(const A& a, const B& b){return a*b;}
	
	template<class A, class B>
	__typeof(A()/B()) divides(const A& a, const B& b){return a/b;}
	
	
	
	template<class A, class B>
	A& plus_assign(A& a,const B& b){return a+=b;}
	
	template<class A, class B>
	A& minus_assign(A& a,const B& b){return a-=b;}
	
	template<class A, class B>
	A& multiplies_assign(A& a,const B& b){return a*=b;}
	
	template<class A, class B>
	A& divides_assign(A& a,const B& b){return a/=b;}
	
	*/
	

}






//#include<type_traits>

namespace ab
{


template<bool cond, typename T, typename F> struct IF {typedef T R;};
template<typename T, typename F> struct IF<false,T,F> {typedef F R;};

template <bool B, class T = void> struct ENABLE_IF {typedef T R;};
template <class T> struct ENABLE_IF<false, T> {};

template <bool B, class T = void> struct DISABLE_IF {typedef T R;};
template <class T> struct DISABLE_IF<true, T> {};



template<class A, class B> struct SAME{ static const bool r = false;};
template<typename T> struct SAME<T,T>{	static const bool r = true;};


template<typename T>
struct IS_CLASS{
	template<typename C> static char (&f(int C::*))[1];
	template<typename C> static char (&f(...))[2];

	static bool const r = sizeof(f<T>(0))==sizeof(char);
};




// remove reference
template<class T> struct RR{typedef T R;};
template<class T> struct RR<T&>{typedef T R;};

//remove const
template<class T> struct RC{typedef T R;};
template<class T> struct RC<const T>{typedef T R;};

// remove const and reference
template<class T> struct RRC{typedef typename RC<typename RR<T>::R>::R R;};


#define TYPE(T,...) typename T<__VA_ARGS__>::R





struct TRU{static const bool r = true;  typedef TRU R;};
struct FAL{static const bool r = false; typedef FAL R;};



// SFINAE test
#define HAS_MEMBER(member_alias, member)																									\
template<typename __T> struct HAS_CLASS_##member_alias {																											\
    struct Fallback { void member(); };																											\
    struct Derived : __T, Fallback { };																											\
																											\
    template<typename C, C> struct ChT;																											\
																											\
    template<typename C> static char (&f(ChT<void (Fallback::*)(), &C::member>*))[1];													\
    template<typename C> static char (&f(...))[2];																								\
																											\
    static const bool r = sizeof(f<Derived>(0)) == 2;																						\
};																										\
template<typename __T>																											\
struct HAS_##member_alias																											\
{																											\
	typedef typename IF<IS_CLASS<__T>::r, HAS_CLASS_##member_alias<__T>, FAL>::R R;														\
	static const bool r = R::r;																									\
};






// Dzia│a dok│adnie, uwzglŕdnia typ (const, referencje) wyniku i argumentˇw, uwzglŕdnia const membera
// Przyk│ad dla non-static klasy A:
// typedef int (A::*sign1)(unsigned short&); 
// Przyk│ad dla static dowolnej klasy:
// typedef int (*sign1)(unsigned short&); 
#define HAS_MEMBER_SIG(member_name, member_signature)																			\
template<typename CLS>																							\
struct HAS_SIG_##member_name																															\
{																									\
    template<typename C, C> struct ChT;																													\
	template<typename C> static char (&f(ChT<member_signature, &C::member_name>*))[1];																\
	template<typename C> static char (&f(...))[2];																									\
	static const bool r = sizeof(f<CLS>(0)) == sizeof(char);																							\
};




HAS_MEMBER(OPERATOR_CALL, operator())




/*
// get &T::x or &T::first
template<class T,class=void>struct GET_X{};
template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::x)>::value>::type>{typedef decltype(&T::x) R;};
template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::first)>::value>::type>{typedef decltype(&T::first) R;};

// get &T::y or &T::second
template<class T,class=void>struct GET_Y{};
template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::y)>::value>::type>{typedef decltype(&T::y) R;};
template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::second)>::value>::type>{typedef decltype(&T::second) R;};
*/

}






#include<utility>

namespace ab{

	struct Identity{
		template<class TT>
		TT operator()(TT&& x)const {
			return std::forward<TT>(x);
		}
		template<class TT>
		TT operator[](TT&& x) const {
			return std::forward<TT>(x);
		}
	};

}




namespace ab{

struct Lambda1{
	template<class A1>
	const A1& operator()(const A1& a1)const{return a1;}
	template<class A1>
	      A1& operator()(      A1& a1)const{return a1;}
	
	template<class A1,class A2>
	const A1& operator()(const A1& a1, const A2&)const{return a1;}
	template<class A1,class A2>
	      A1& operator()(      A1& a1, const A2&)const{return a1;}
	
	template<class A1,class A2,class A3>
	const A1& operator()(const A1& a1, const A2&, const A3&)const{return a1;}
	template<class A1,class A2,class A3>
	      A1& operator()(      A1& a1, const A2&, const A3&)const{return a1;}
};


struct Lambda2{
	template<class A1,class A2>
	const A2& operator()(const A1& a1, const A2& a2)const{return a2;}
	template<class A1,class A2>
	      A2& operator()(const A1& a1,       A2& a2)const{return a2;}
	
	template<class A1,class A2,class A3>
	const A2& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a2;}
	template<class A1,class A2,class A3>
	      A2& operator()(const A1& a1,       A2& a2, const A3& a3)const{return a2;}
};


struct Lambda3{
	template<class A1,class A2,class A3>
	const A3& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a3;}
	template<class A1,class A2,class A3>
	      A3& operator()(const A1& a1, const A2& a2,       A3& a3)const{return a3;}
};


#ifdef __GNUC__
	#pragma GCC diagnostic push
	#pragma GCC diagnostic ignored "-Wunused-variable"	// not working
#endif


static Lambda1 _1;
static Lambda2 _2;
static Lambda3 _3;
//#define _1 Lambda1()
//#define _2 Lambda2()
//#define _3 Lambda3()



#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif




#ifdef __GNUC__
	// _1 _2 _3 unused
	// pragma diagnostic ignored not working?
	template<class T>void ___fhsdhsdhrtshx(){(void)_1;(void)_2;(void)_3;}
#endif







template<class L, class A1, class A2, class A3> struct RET_TYPE3{typedef L& R;};

template<class L, class A1> struct RET_TYPE1{typedef typename RET_TYPE3<L,A1,A1,A1>::R R;};
template<class L, class A1, class A2> struct RET_TYPE2{typedef typename RET_TYPE3<L,A1,A2,A2>::R R;};






// is lambda before evaluation
template<class L> struct IS_LAMBDA{static const bool r = false;};





// is lambda after evaluation
template<class L, class A1, class A2, class A3> struct IS_LAMBDA3{
static const bool r = !SAME<L&, typename RET_TYPE3<L,A1,A2,A3>::R&>::r;};

template<class L, class A1, class A2> struct IS_LAMBDA2{
static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE2<L,A1,A2>::R>::R>::r;};

template<class L, class A1> struct IS_LAMBDA1{
static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE1<L,A1>::R>::R>::r;};




template<class T, class A1>
typename ENABLE_IF  < IS_LAMBDA1<T,A1>::r,      typename RET_TYPE1<T,const A1>::R>::R Eval(const T& t, const A1& a1){return t(a1);}
template<class T, class A1>
typename ENABLE_IF  < IS_LAMBDA1<T,A1>::r,      typename RET_TYPE1<T,      A1>::R>::R Eval(const T& t,       A1& a1){return t(a1);}

template<class T, class A1>
typename DISABLE_IF < IS_LAMBDA1<T,A1>::r,      typename RET_TYPE1<T,A1>::R&>::R Eval(      T& t, const A1& a1){return t;}




template<class T, class A1, class A2>
typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r,      typename RET_TYPE2<T,const A1,const A2>::R>::R Eval(const T& t, const A1& a1, const A2& a2){return t(a1,a2);}
template<class T, class A1, class A2>
typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r,      typename RET_TYPE2<T,const A1,      A2>::R>::R Eval(const T& t, const A1& a1,       A2& a2){return t(a1,a2);}
template<class T, class A1, class A2>
typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r,      typename RET_TYPE2<T,      A1,const A2>::R>::R Eval(const T& t,       A1& a1, const A2& a2){return t(a1,a2);}
template<class T, class A1, class A2>
typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r,      typename RET_TYPE2<T,      A1,      A2>::R>::R Eval(const T& t,       A1& a1,       A2& a2){return t(a1,a2);}

template<class T, class A1, class A2>
typename DISABLE_IF< IS_LAMBDA2<T,A1,A2>::r,      typename RET_TYPE2<T,A1,A2>::R&>::R Eval(      T& t, const A1& a1, const A2& a2){return t;}




template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,const A1,const A2,const A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,const A1,const A2,      A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2,       A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,const A1,      A2,const A3>::R>::R Eval(const T& t, const A1& a1,       A2& a2, const A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,const A1,      A2,      A3>::R>::R Eval(const T& t, const A1& a1,       A2& a2,       A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,      A1,const A2,const A3>::R>::R Eval(const T& t,       A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,      A1,const A2,      A3>::R>::R Eval(const T& t,       A1& a1, const A2& a2,       A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,      A1,      A2,const A3>::R>::R Eval(const T& t,       A1& a1,       A2& a2, const A3& a3){return t(a1,a2,a3);}
template<class T, class A1, class A2, class A3>
typename ENABLE_IF  < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,      A1,      A2,      A3>::R>::R Eval(const T& t,       A1& a1,       A2& a2,       A3& a3){return t(a1,a2,a3);}

template<class T, class A1, class A2, class A3>
typename DISABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r,      typename RET_TYPE3<T,A1,A2,A3>::R&>::R Eval(      T& t, const A1& a1, const A2& a2, const A3& a3){return t;}







template<class L1, class L2>
class LambdaPlus{
public:
	LambdaPlus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaPlus,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) + Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaPlus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) + Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaPlus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) + Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};

template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaPlus<L1,L2> >::R operator+(const L1& l1, const L2& l2){
	return LambdaPlus<L1,L2>(l1,l2);
}



template<class L1, class L2>
class LambdaMinus{
public:
	LambdaMinus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaMinus,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) - Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) - Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) - Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};

template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMinus<L1,L2> >::R operator-(const L1& l1, const L2& l2){
	return LambdaMinus<L1,L2>(l1,l2);
}






template<class L1>
class LambdaUnaryMinus{
public:
	LambdaUnaryMinus(const L1& _l1) : l1(_l1){}
	
	template<class A1>
	typename RET_TYPE1<LambdaUnaryMinus,A1>::R operator()(const A1& a1)const{
		return -Eval(l1,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaUnaryMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return -Eval(l1,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaUnaryMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return -Eval(l1,a1,a2,a3);
	}
	
private:
	const L1& l1;
};

template<class L1>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r, LambdaUnaryMinus<L1> >::R operator-(const L1& l1){
	return LambdaUnaryMinus<L1>(l1);
}






template<class L1, class L2>
class LambdaMultiplies{
public:
	LambdaMultiplies(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaMultiplies,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) * Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaMultiplies,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) * Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaMultiplies,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) * Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};

template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMultiplies<L1,L2> >::R operator*(const L1& l1, const L2& l2){
	return LambdaMultiplies<L1,L2>(l1,l2);
}




template<class L1, class L2>
class LambdaDivides{
public:
	LambdaDivides(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaDivides,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) / Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaDivides,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) / Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaDivides,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) / Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};

template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaDivides<L1,L2> >::R operator/(const L1& l1, const L2& l2){
	return LambdaDivides<L1,L2>(l1,l2);
}














template<class L1, class L2>
class LambdaPlusAssign{
public:
	LambdaPlusAssign(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()(const A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);}
	template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()(      A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);}
	
	template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1,      A2>::R operator()(const A1& a1,       A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,      A1,const A2>::R operator()(      A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,      A1,      A2>::R operator()(      A1& a1,       A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);}
	
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1,       A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1,       A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(      A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(      A1& a1, const A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(      A1& a1,       A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(      A1& a1,       A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);}
	
private:
	L1& l1;
	const L2& l2;
};


template<class L1, class L2>
typename ENABLE_IF<IS_LAMBDA<L1>::r or IS_LAMBDA<L2>::r, LambdaPlusAssign<L1,L2> >::R operator+=(L1& l1, const L2& l2){
	return LambdaPlusAssign<L1,L2>(l1,l2);
}







// LOGICAL

template<class L1, class L2>
class LambdaLess{
public:
	LambdaLess(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaLess,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) < Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaLess,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) < Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaLess,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) < Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};
template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaLess<L1,L2> >::R operator<(const L1& l1, const L2& l2){
	return LambdaLess<L1,L2>(l1,l2);
}





template<class L1, class L2>
class LambdaGreater{
public:
	LambdaGreater(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){}
	
	template<class A1>
	typename RET_TYPE1<LambdaGreater,A1>::R operator()(const A1& a1)const{
		return Eval(l1,a1) > Eval(l2,a1);
	}
	
	template<class A1, class A2>
	typename RET_TYPE2<LambdaGreater,A1,A2>::R operator()(const A1& a1, const A2& a2)const{
		return Eval(l1,a1,a2) > Eval(l2,a1,a2);
	}
	
	template<class A1, class A2, class A3>
	typename RET_TYPE3<LambdaGreater,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{
		return Eval(l1,a1,a2,a3) > Eval(l2,a1,a2,a3);
	}
	
private:
	const L1& l1;
	const L2& l2;
};

template<class L1, class L2>
typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaGreater<L1,L2> >::R operator>(const L1& l1, const L2& l2){
	return LambdaGreater<L1,L2>(l1,l2);
}






template<class L1, class L2>
class LambdaDotStar{
public:
	LambdaDotStar(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2) {}
	
	template<class A1> typename RET_TYPE1<LambdaDotStar,const A1>::R operator()(const A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);}
	template<class A1> typename RET_TYPE1<LambdaDotStar,      A1>::R operator()(      A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);}
	
	template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1,      A2>::R operator()(const A1& a1,       A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,      A1,const A2>::R operator()(      A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);}
	template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,      A1,      A2>::R operator()(      A1& a1,       A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);}
	
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2,const A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2,      A3>::R operator()(const A1& a1, const A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,      A2,const A3>::R operator()(const A1& a1,       A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,      A2,      A3>::R operator()(const A1& a1,       A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,      A1,const A2,const A3>::R operator()(      A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,      A1,const A2,      A3>::R operator()(      A1& a1, const A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,      A1,      A2,const A3>::R operator()(      A1& a1,       A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}
	template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,      A1,      A2,      A3>::R operator()(      A1& a1,       A2& a2,       A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);}

private:
	L1& l1;
	const L2& l2;
};

// bv == bind var
template<class PTR, class T>
LambdaDotStar<const T,PTR> bv(const PTR& ptr, const T& t){
	return LambdaDotStar<const T,PTR>(t,ptr);
}
template<class PTR, class T>
LambdaDotStar<T,PTR> bv(const PTR& ptr, T& t){
	return LambdaDotStar<T,PTR>(t,ptr);
}








// is lambda (before evaluation)
template<> struct IS_LAMBDA<Lambda1>{static const bool r = true;};
template<> struct IS_LAMBDA<Lambda2>{static const bool r = true;};
template<> struct IS_LAMBDA<Lambda3>{static const bool r = true;};
template<class A,class B> struct IS_LAMBDA<LambdaPlus<A,B> >{static const bool r = true;};
template<class A,class B> struct IS_LAMBDA<LambdaMinus<A,B> >{static const bool r = true;};
template<class A,class B> struct IS_LAMBDA<LambdaMultiplies<A,B> >{static const bool r = true;};
template<class A,class B> struct IS_LAMBDA<LambdaDivides<A,B> >{static const bool r = true;};
template<class A,class B> struct IS_LAMBDA<LambdaPlusAssign<A,B> >{static const bool r = true;};
// TODO
















// lambda return types

/*
template<class A1> struct RET_TYPE1<Lambda1,A1>{typedef A1 R;};

template<class A1, class A2> struct RET_TYPE2<Lambda1,A1,A2>{typedef A1 R;};
template<class A1, class A2> struct RET_TYPE2<Lambda2,A1,A2>{typedef A2 R;};
*/
template<class A1, class A2, class A3> struct RET_TYPE3<Lambda1,A1,A2,A3>{typedef A1& R;};
template<class A1, class A2, class A3> struct RET_TYPE3<Lambda2,A1,A2,A3>{typedef A2& R;};
template<class A1, class A2, class A3> struct RET_TYPE3<Lambda3,A1,A2,A3>{typedef A3& R;};




#ifdef _MSC_VER
	#define __typeof decltype
#endif



template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlus<L1,L2>,A1,A2,A3>{
	typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() + TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R;
};

template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMinus<L1,L2>,A1,A2,A3>{
	typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() - TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R;
};

template<class L1, class A1, class A2, class A3> struct RET_TYPE3<LambdaUnaryMinus<L1>,A1,A2,A3>{
	typedef __typeof(- TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)()) R;
};

template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMultiplies<L1,L2>,A1,A2,A3>{
	typedef __typeof(*(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R*)nullptr * *(typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R*)nullptr) R;
};

template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDivides<L1,L2>,A1,A2,A3>{
	typedef __typeof(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R() / typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R()) R;
};



template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlusAssign<L1,L2>,A1,A2,A3>{
	typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) += *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R))& R;
};




template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaLess<L1,L2>,A1,A2,A3>{
	typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) < *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R;
};
template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaGreater<L1,L2>,A1,A2,A3>{
	typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) > *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R;
};



template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDotStar<L1,L2>,A1,A2,A3>{
	typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) .* *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R;
};






template<class F, class L1>
class LambdaBinder1{
public:
	LambdaBinder1(const F& _f, const L1& _l1) : f(_f), l1(_l1) {}
	
	template<class A1>
	int operator()(const A1& a1){
		return f(Eval(l1,a1));
	}
	template<class A1, class A2>
	int operator()(const A1& a1, const A2& a2){
		return f(Eval(l1,a1,a2));
	}
	template<class A1, class A2, class A3>
	int operator()(const A1& a1, const A2& a2, const A3& a3){
		return f(Eval(l1,a1,a2,a3));
	}
	
private:
	const F& f;
	const L1& l1;
};

template<typename F, class L1>
LambdaBinder1<F,L1> bf(const F& f, const L1& l1){
	return LambdaBinder1<F,L1>(f,l1);
}



template<class F, class L1, class L2>
class LambdaBinder2{
public:
	LambdaBinder2(const F& _f, const L1& _l1, const L2& _l2) : f(_f), l1(_l1), l2(_l2) {}
	
	template<class A1>
	int operator()(const A1& a1){
		return f(Eval(l1,a1),Eval(l2,a1));
	}
	template<class A1, class A2>
	int operator()(const A1& a1, const A2& a2){
		return f(Eval(l1,a1,a2),Eval(l2,a1,a2));
	}
	template<class A1, class A2, class A3>
	int operator()(const A1& a1, const A2& a2, const A3& a3){
		return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3));
	}
	
private:
	const F& f;
	const L1& l1;
	const L2& l2;
};

template<typename F, class L1, class L2>
LambdaBinder2<F,L1,L2> bf(const F& f, const L1& l1, const L2& l2){
	return LambdaBinder2<F,L1,L2>(f,l1,l2);
}



template<class F, class L1, class L2, class L3>
class LambdaBinder3{
public:
	LambdaBinder3(const F& _f, const L1& _l1, const L2& _l2, const L3& _l3) : f(_f), l1(_l1), l2(_l2), l3(_l3) {}
	
	template<class A1>
	int operator()(const A1& a1){
		return f(Eval(l1,a1),Eval(l2,a1),Eval(l3,a1));
	}
	template<class A1, class A2>
	int operator()(const A1& a1, const A2& a2){
		return f(Eval(l1,a1,a2),Eval(l2,a1,a2),Eval(l3,a1,a2));
	}
	template<class A1, class A2, class A3>
	int operator()(const A1& a1, const A2& a2, const A3& a3){
		return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3),Eval(l3,a1,a2,a3));
	}
	
private:
	const F& f;
	const L1& l1;
	const L2& l2;
	const L3& l3;
};

template<typename F, class L1, class L2, class L3>
LambdaBinder3<F,L1,L2,L3> bf(const F& f, const L1& l1, const L2& l2, const L3& l3){
	return LambdaBinder3<F,L1,L2,L3>(f,l1,l2,l3);
}












}




namespace ab{


// INTERVAL TREE (simple)
// not tested
template<class T, class AGG_FUN>
class ITree{

private:
	std::vector<T> tree;
	uint tsize;
	const AGG_FUN& aggFun;
	
public:
	ITree(const AGG_FUN& _fun) : tsize(1), aggFun(_fun){
		tree.resize(tsize*2);
	}
	
	ITree(const ITree& o) : tree(o.tree), tsize(o.tsize), aggFun(o.aggFun){}
	ITree& operator=(const ITree& o){
		tree = o.tree;
		tsize = o.tsize;
		aggFun = o.aggFun;
		return *this;
	}
	
	// todo: operator= instead of set
	//       operator[] should return smart object
	void set(uint where, const T& what){
		check(where);
		
		where+=tsize;
		tree[where] = what;
		while(where!=1){
			where>>=1;
			tree[where] = (*aggFun)(tree[where*2],tree[where*2+1]);
		}
	}
	
	const T& get(uint where)const{
		if(where>=tsize) return T();
		return tree[tsize+where];
	}
	
	const T& operator[](uint where)const{
		return get(where);
	}
	
	T get(uint fr, uint to)const{
		ASS(fr<to);
		if(fr>=tsize)return T();
		if(to>tsize) to=tsize;
		
		fr += tsize;  to += tsize-1;
		T lresult = T();
		T rresult = T();
		while(fr<to){
			if((fr&1)==1) lresult = aggFun(tree[fr],lresult), fr>>=1, ++fr;
			else fr>>=1;
			if((to&1)==0) rresult = aggFun(rresult,tree[to]), to>>=1, --to;
			else to>>=1;
		}
		if(fr==to)lresult=aggFun(lresult,tree[fr]);
		return aggFun(lresult,rresult);
	}
	
	const T& root()const{
		return tree[1];
	}
	
	// can only make bigger
	void resize(uint new_size){
		check(new_size-1);
	}
	
private:
	void check(uint where){
		if(where<tsize)return;
		
		uint oldPos = tsize;
		uint moves = 0;
		while(where >= tsize) tsize <<= 1, ++moves;
		
		tree.resize(tsize*2);
		uint newPos = tsize;
		
		while(oldPos){
			for(uint i=0; i<oldPos; ++i){
				tree[newPos+i] = tree[oldPos+i];
				tree[oldPos+i] = T();
			}
			oldPos >>= 1;
			newPos >>= 1;
		}
		
		// update
		while(newPos>0){
			tree[newPos] = aggFun(tree[newPos*2], tree[newPos*2+1]);
			newPos >>= 1;
		}
	}
	
};

template<class T, class AGG_FUN>
ITree<T,AGG_FUN> getITree(const AGG_FUN& aggFun = std::plus<T>()){
	return ITree<T,AGG_FUN>(aggFun);
}





//	INTERVAL TREE with subree modifiers (no propagation)
//
//
//	Example for sum:
//		//					[0,1000000)  VAL agg,    T += MOD*INTERVAL
//		ITreeMod<int,int> tree(1000000,  (_1 + _2), (_1 += _2*_3));
//
//		// add 69 to every modifier in [a,b):
//		tree.alter(fr,to, _1 += 69);
//		// or:
//		tree.plus(fr,to, 69);
//
// tested only for MAX (ignoring subtree_size)
//
//
// TODO: copy to new class ITreePro: propagate modifiers!!
// gives some possibilities like "set value on interval"
// it would require another function: apply modifier to another modifier
//
template<class T, class MOD, class AGG_FUN, class MOD_FUN>
class ITreeMod{
public:
	ITreeMod(const AGG_FUN& _fun, const MOD_FUN& _modFun) : tsize(1), aggFun(_fun), modFun(_modFun){
		tree.resize(tsize*2);
	}
	
	template<class FUN>
	void mod(uint where, const FUN& fun){
		check(where);
		
		where += size;
		fun(tree[where].se);
		modFun(tree[where].fi = T(), tree[where].se);
		updateToRoot(where);
	}
	
	// requires T::operator-
	void set(uint where, const T& what){
		T was = get(where);
		mod(where, what - was);
	}
	
	const T& get(uint where)const{
		if(where >= size) return T();
		
		where += size;
		T result = tree[where].fi;
		while(where != 1){
			where >>= 1;
			modFun(result, tree[where].se, 1);
		}
		return result;
	}
	
	const T& operator[](uint where)const{
		return get(where);
	}
	
	T get(uint fr, uint to)const{
		ASS(fr<to);
		if(fr>=tsize) return T();
		if(to>tsize) to = tsize;
		
		fr+=tsize; to+=tsize-1;
		T lresult = T();
		T rresult = T();
		uint lagg=0;
		uint ragg=0;
		uint lsize = 0;
		uint rsize = 0;
		uint csize = 1;
		while(fr<to){
			if(lagg) modFun(lresult,tree[lagg].second,lsize), lagg>>=1;
			if(ragg) modFun(rresult,tree[ragg].second,rsize), ragg>>=1;
			
			if((fr&1)==1) lresult = aggFun(lresult,tree[fr].first), lsize+=csize, fr>>=1, lagg=fr, ++fr;
			else fr>>=1;
			if((to&1)==0) rresult = aggFun(tree[to].first,rresult), rsize+=csize, to>>=1, ragg=to, --to;
			else to>>=1;
			
			csize <<= 1;
		}
		if(lagg and fr==to and ragg){
			for(;;){
				T cresult = tree[fr].first;
				modFun(lresult,tree[lagg].second,lsize), lagg>>=1;
				modFun(rresult,tree[ragg].second,rsize), ragg>>=1;
				fr>>=1;
				if		(lagg==fr){
					lresult = aggFun(lresult,cresult), lsize += csize;
					break;
				}
				else if	(fr==ragg){
					rresult = aggFun(cresult,rresult), rsize += csize;
					break;
				}
				modFun(cresult,tree[fr].second,csize);
			}
			modFun(lresult,tree[lagg].second,lsize);
			modFun(rresult,tree[ragg].second,rsize);
		}
		else if(lagg and fr==to){
			rresult = tree[fr].first;
			ragg = fr;
			rsize = csize;
			
			modFun(lresult,tree[lagg].second,lsize);
		}
		else if(fr==to and ragg){
			lresult = tree[fr].first;
			lagg = fr;
			lsize = csize;
			
			modFun(rresult,tree[ragg].second,rsize);
		}
		else if(!lagg and !ragg){
			T result = tree[fr].first;
			while(fr!=1){
				fr>>=1;
				modFun(result, tree[fr].second, csize);
			}
			return result;
		}
		else{	// lagg and ragg
			modFun(lresult, tree[lagg].second,lsize);
			modFun(rresult, tree[ragg].second,rsize);
		}
		
		// lagg and ragg
		ASS(lagg and ragg);
		ASS(lagg != ragg);
		for(;;){
			lagg >>= 1;
			ragg >>= 1;
			if(lagg==ragg){
				lresult = aggFun(lresult,rresult);
				lsize += rsize;
				modFun(lresult, tree[lagg].second, lsize);
				break;
			}
			modFun(lresult, tree[lagg].second, lsize);
			modFun(rresult, tree[ragg].second, rsize);
		}
		while(lagg != 1){
			lagg>>=1;
			modFun(lresult, tree[lagg].second, lsize);
		}
		return lresult;
	}
	
	template<class FUN>
	void alter(uint fr, uint to,  const FUN& f){
		ASS(fr<to);
		
		check(to-1);
		
		fr += tsize; to += tsize-1;
		uint lupd = 0, lupd_size=0;
		uint rupd = 0, rupd_size=0;
		uint csize = 1;
		
		{
			if((fr&1)==1){
				f(tree[fr].second);
				modFun(tree[fr].first=T(), tree[fr].second, csize);
				if(!lupd)lupd=fr, lupd_size=csize;
				fr>>=1;
				++fr;
			}
			else fr>>=1;
			
			if((to&1)==0){
				f(tree[to].second);
				modFun(tree[to].first=T(), tree[to].second, csize);
				if(!rupd)rupd=to, rupd_size=csize;
				to>>=1;
				--to;
			}
			else to>>=1;
			
			csize <<= 1;
		}
		
		while(fr<to){
			if((fr&1)==1){
				f(tree[fr].second);
				update(fr,csize);
				if(!lupd)lupd=fr, lupd_size=csize;
				fr>>=1;
				++fr;
			}
			else fr>>=1;
			
			if((to&1)==0){
				f(tree[to].second);
				update(to,csize);
				if(!rupd)rupd=to, rupd_size=csize;
				to>>=1;
				--to;
			}
			else to>>=1;
			
			csize <<= 1;
		}
		if(fr==to){
			f(tree[fr].second);
			update(fr,csize);
			if(!lupd)lupd=fr, lupd_size=csize;
			else if(!rupd)rupd=fr, rupd_size=csize;
		}
		
		if(lupd and rupd){
			while(lupd_size < rupd_size)update(lupd >>= 1, lupd_size <<= 1);
			while(lupd_size > rupd_size)update(rupd >>= 1, rupd_size <<= 1);
			for(;;){
				lupd >>= 1; lupd_size <<= 1;
				rupd >>= 1;
				update(lupd, lupd_size);
				if(lupd == rupd)break;
				update(rupd, lupd_size);
			}
		}
		else if(rupd) lupd=rupd, lupd_size=rupd_size;
		
		while(lupd > 1) update(lupd >>= 1, lupd_size <<= 1);
	}
	
	template<class ARG>
	void plus(int fr, int to, const ARG& arg){
		alter(fr,to, _1 += arg);
	}
	template<class ARG>
	void minus(int fr, int to, const ARG& arg){
		alter(fr,to,arg, _1 -= _2);
	}
	template<class ARG>
	void multiplies(int fr, int to, const ARG& arg){
		alter(fr,to,arg, _1 *= _2);
	}
	template<class ARG>
	void divides(int fr, int to, const ARG& arg){
		alter(fr,to,arg, _1 /= _2);
	}
	
private:
	void update(int where, int ssize){
		tree[where].first = aggFun(tree[where*2].first, tree[where*2+1].first);
		modFun(tree[where].first, tree[where].second, ssize);
	}
	void updateToRoot(int where, int ssize){
		while(where>1){
			where>>=1;
			ssize<<=1;
			update(where,ssize);
		}
	}
	
public:
	uint size()const{
		return tsize;
	}
	
	const std::pair<T,MOD>* getTree()const{
		return tree;
	}
	
private:
	void check(uint where){
		if(where<tsize)return;
		
		uint oldPos = tsize;
		uint moves = 0;
		while(where >= tsize) tsize <<= 1, ++moves;
		
		tree.resize(tsize*2);
		uint newPos = tsize;
		
		while(oldPos){
			for(uint i=0; i<oldPos; ++i){
				tree[newPos+i] = tree[oldPos+i];
				tree[oldPos+i] = std::make_pair(T(),MOD());
			}
			oldPos >>= 1;
			newPos >>= 1;
		}
		updateToRoot(newPos << 1, tsize >> moves);
	}
	uint tsize;
	std::vector<std::pair<T,MOD> > tree;
	const AGG_FUN& aggFun;
	const MOD_FUN& modFun;
};


template<class T, class MOD, class AGG_FUN, class MOD_FUN>
ITreeMod<T,MOD,AGG_FUN,MOD_FUN> getITree(
			const AGG_FUN& _fun,
			const MOD_FUN& _modFun){
	return ITreeMod<T,MOD,AGG_FUN,MOD_FUN>(_fun,_modFun);
}

template<class T, class MOD, class AGG_FUN>
ITreeMod<T,MOD,AGG_FUN,LambdaPlusAssign<Lambda1,Lambda2> > getITree(
			const AGG_FUN& _fun){
	return ITreeMod<T,MOD,AGG_FUN,__typeof(_1 += _2)> (_fun,(_1 += _2));
}






template<class T, class MOD, class AGG_FUN, class MOD_FUN>
std::ostream& operator<<(std::ostream& os, const ITreeMod<T,MOD,AGG_FUN,MOD_FUN>& t){

	os << "CUMs:\n";
	uint csize = t.getSize();
	for(uint fi=1; fi<=t.getSize(); fi<<=1){
		for(uint j=fi; j<(fi<<1); ++j){
			os << t.getTree()[j].first;
			for(uint i=0; i<csize-1; ++i)os<<' ';
		}
		os<<'\n';
		csize/=2;
	}
	os << "\nMODs:\n";
	csize = t.getSize();
	for(uint fi=1; fi<=t.getSize(); fi<<=1){
		for(uint j=fi; j<(fi<<1); ++j){
			os << t.getTree()[j].second;
			for(uint i=0; i<csize-1; ++i)os<<' ';
		}
		os<<'\n';
		csize/=2;
	}
	os<<'\n'<<'\n';
	return os;
}



}






//#include<atablash/linalg/2/matrix.hpp>
//#include<atablash/hash.hpp>


















////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  ACTUAL SOLUTION IS AT THE END OF THIS FILE!!!                             //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////


















////////////////////////// PROTOTYPING TEMPLATE /////////////////////////////////
//
// Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014
/*
    This program is licensed under GNU General Public License version 3.
    See <http://www.gnu.org/licenses/>.
*/
//
//

// enable c++ iostream; only if no fastio
//#define IOSTREAM

//#define NO_UNLOCKED	// fread_unlocked, fwrite_unlocked



// PRIi64, PRIui64
#define __STDC_FORMAT_MACROS
#include<inttypes.h>



//#if MIN_GCC(4,1,3)
//#include<bits/stdc++.h>
//#else
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<string>
#include<stdint.h>
#include<cstring>
#include<iostream>
#include<set>
#include<climits>
#include<bitset>
#include<limits>
#include<sstream>
#include<cmath>
//#endif










/*
#define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
#define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i)

#define fo2(i,n) fo3(i,0,n)
#define of2(i,n) of3(i,0,n)

#define fo(...) VA_SEL(fo,__VA_ARGS__)
#define of(...) VA_SEL(of,__VA_ARGS__)
*/

#define inl		inline __attribute__ ((always_inline))
#define ninl	__attribute__ ((noinline))







#ifdef DEBUG_BUILD
	#undef FASTIO
#endif





#ifdef _WIN32

// TODO
#ifndef NO_UNLOCKED
#define NO_UNLOCKED
#endif

//#define fread_unlocked _fread_nolock
//#define fwrite_unlocked _fwrite_nolock

#endif



using namespace std;
using namespace __gnu_cxx;

namespace ab{}
using namespace ab;





template<class T>inline void sortu(T& t){t.resize(std::unique(ALL(t))-t.be);}


inline uint _rand(){
	if(RAND_MAX < (1<<15)) return (rand()<<15) | rand();
	else return rand();
}
#define RND _rand()


#define ri _rint()
#define rui _ruint()
#define rll _rll()
#define rull _rull()
#define rchar _rchar()
#define rstr _rstr()


#ifndef FASTIO
inline void _fin(int){}
inline void _fout(int){}
#ifdef IOSTREAM
inline int _rint(){int r;cin>>r;return r;}
inline uint _ruint(){uint r;cin>>r;return r;}
inline ull _rull(){ull r;cin>>r;return r;}
inline ll _rll(){ll r;cin>>r;return r;}
inline char _rchar(){return cin.get();}
inline string _rstr(){string s;cin>>s;return s;}
template<typename T>inline void W(const T& i){cout<<i; cout.flush();}
inline void W(){W('\n');}
#else
inline int _rint(){int r;scanf("%d",&r);return r;}
inline uint _ruint(){uint r;scanf("%u",&r);return r;}
inline ull _rull(){ull r;scanf("%" SCNu64,&r);return r;}
inline ull _rll(){ll r;scanf("%" SCNd64,&r);return r;}
inline char _rchar(){return getchar();}
inline string _rstr(){
	string s;
	char c=rchar;
	while(c!=-1 && !isgraph(c))c=rchar;
	for(;;){
		if(!isgraph(c))break;
		s+=c;
		c=rchar;
	}
	return s;
}
inline void W(char a='\n'){printf("%c",a);}
inline void W(const string&s){printf("%s",s.c_str());}
inline void W(int a){printf("%d",a);}
inline void W(uint a){printf("%u",a);}
inline void W(const ui64& a){printf("%" PRIu64,a);}
inline void W(const i64& a){printf("%" PRIi64,a);}
inline void W(const char *str){fputs(str,stdout);}
inline void W(const double& d){printf("%.20f",d);}
inline void W(const long double& d){printf("%.50Lf",d);}
#endif
#else

#ifndef NO_UNLOCKED
#define FREAD fread_unlocked
#define FWRITE fwrite_unlocked
#else
#define FREAD fread
#define FWRITE fwrite
#endif

// todo: test on pointers
const int _IB=32768;const int _OB=32768;
char _iB[_IB];int _iP = _IB;
char _oB[_OB];int _oP = 0;
inline void _fin(int mc){
	int r=_IB-_iP;
	if(r>mc)return;
	FOR(i,r) _iB[i]=_iB[i+_iP];	// TODO: try memcpy
    int rd=FREAD(_iB+r,1,_iP,stdin);
	if(rd!=_iP) _iB[r+rd]=0;
	_iP = 0;
}
inline void _fout(int mc){
	if(_OB-_oP>mc)return;
	FWRITE(_oB,1,_oP,stdout);
	_oP = 0;
}
inline void _W(char c='\n'){_oB[_oP++]=c;} // unchecked!
inline char _rc(){return _iB[_iP++];}
inline char _rchar(){_fin(1);return _rc();}
inline string _rstr(){
	string s;
	for(;;){
		char c=rchar;
		if(!isprint(c)||isspace(c))break;
		s+=c;
	}
	return s;
}
template<class T>inline T _rur(){ // unchecked!
    T r=0;char c=_rc();
    do c-='0',r=r*10+c,c=_rc();while(c>='0');
    return r;}
template<class T>inline T _rsi(){ // unchecked!
	char c=_rc();
	while(c<'-')c=_rc();
	if(c=='-')return -_rur<T>();
	else{--_iP;return _rur<T>();}}
template<class T>inline T _ru(){  // unchecked!
	while(_rc()<'0');--_iP;
	return _rur<T>();
}

inline int _rint(){_fin(15);return _rsi<int>();}
inline uint _ruint(){_fin(15);return _ru<uint>();}
inline ll _rll(){_fin(25);return _rsi<ll>();}
inline ull _rull(){_fin(25);return _ru<ull>();}

const int _rs = 20; char _r[_rs];
template<class T>inline void _w(T i){	// unchecked!
	if(numeric_limits<T>::is_signed and i<0) _W('-'), i=-i;
	else if(i==0){_W('0'); return;}
	int pos = _rs;
	do _r[--pos]='0'+i%10, i/=10; while(i);
	do _W(_r[pos++]); while(pos<_rs);
}
inline void W(const char c){_fout(2);_W(c);}
inline void W(const int32_t& i){_fout(15);_w(i);}
inline void W(const uint32_t& i){_fout(15);_w(i);}
inline void W(const int64_t& i){_fout(25);_w(i);}
inline void W(const uint64_t& i){_fout(25);_w(i);}
inline void W(const char*str, int l){
	for(int b=0; b<l; b+=_OB){
		int num = min(_OB,l-b);
		_fout(num);
		memcpy(&_oB[_oP], str+b, num);
		_oP+=num;
	}
}
inline void W(const char*str){W(str,strlen(str));}
inline void W(const string&s){W(s.c_str(),s.siz);}
inline void W(double d){_fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);}	// todo
#endif
template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);}
template<class A,class B,class C>
inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);}
template<class A,class B,class C,class DD>
inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);}
template<class T>inline void sortall(T&t){sort(t.be,t.en);}
template<class T>inline void uniqall(T&t){t.resize(unique(t.be,t.en)-t.be);}


#define rgraph _rgraph()
inline char _rgraph(){char c;do c=rchar;while(!isgraph(c));return c;}

#define subset(a,b) (a&b==a)
#define kbegin(k) ((1<<k)-1)
#define kend(k,n) ((kbegin(k)<<n-k)+1)
inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));}
#define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x))

struct _O{template<class T>_O&operator<(const T&_t){
		oc(1);W(_t);oc(0);
		return *this;
}}O;

struct _I{
	_I&operator>(int32_t&_t){_t=ri;return*this;}
	_I&operator>(uint32_t&_t){_t=rui;return*this;}
	_I&operator>(int64_t&_t){_t=rll;return*this;}
	_I&operator>(uint64_t&_t){_t=rull;return*this;}
	_I&operator>(char&_t){_t=rchar;return*this;}
	_I&operator>(string&_t){_t=rstr;return*this;}
}I;




inline void tc();
#ifdef GEN
int argc;  char **argv;
int main(int _argc, char *_argv[]){
	argc = _argc;  argv = _argv;
	if(argc<2){
		O < "Usage: " < argv[0] < " NR_TESTU" < N;
		return 1;
	}
	srand(atoi(argv[1]));
#else
int main(){
#endif

	//Log log;
	//log.autoFlush = true;
	//Log::global() = &log;

#ifdef FASTIO
    setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0);
#elif defined IOSTREAM
	ios_base::sync_with_stdio(0);cout.precision(10);cout.setf(ios::fixed,ios::floatfield);
	#if not defined DEBUG_BUILD and not defined COLOR_CONSOLE
	cin.tie(0);
	#endif
#endif

#ifdef DEBUG_BUILD
#ifdef _WIN32
	ec(0);
	oc(0);
#else
// TODO: *nix
#endif
#endif
	
#ifdef READ_NUM_TEST_CASES
	int nz=rui;FOR(i,nz)
#endif
	tc();
#ifdef FASTIO
    FWRITE(_oB,1,_oP,stdout);
#endif
	return 0;
}
// END OF TEMPLATE





































////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  ACTUAL SOLUTION BEGINS HERE:                                              //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////


inline int minint(int a, int b){
	return min((int)a,(int)b);
}

struct Cycle{
	vector<char> vals;
	vector<int> prefsums;
	vector<int> prefsums_minpref;
	vector<int> prefsums_minsuf;
	umap<int,vector<int>> val_to_idx;
	
	// compute prefsums and itree
	void init(){
		prefsums.resize(vals.size()+1);
		val_to_idx[0].push_back(0);
		
		FOR(i,vals.size()){
			prefsums[i+1] = prefsums[i] + vals[i];
			val_to_idx[prefsums[i+1]].push_back(i+1);
		}
		
		prefsums_minpref.resize(prefsums.size()+1);
		prefsums_minpref[0] = INT_MAX;
		FOR(i,prefsums.size()){
			prefsums_minpref[i+1] = min(prefsums_minpref[i],prefsums[i]);
		}
		
		prefsums_minsuf.resize(prefsums.size()+1);
		prefsums_minsuf[prefsums.size()] = INT_MAX;
		OF(i,0,prefsums.size()){
			prefsums_minsuf[i] = min(prefsums_minsuf[i+1],prefsums[i]);
		}
	}
	
	// returns minimum win after at most 'maxNumGames' games
	int getMinWin(const int ipos){
		int min_r = prefsums_minsuf[ipos] - prefsums[ipos];
		int maxNumGames = vals.size();
		if(ipos + maxNumGames <= si vals.size()){
			E < "getMinWin( ipos==" < ipos < ", maxNumGames==" < maxNumGames < ") == " < min_r < NL;
			return min_r;
		}
		
		int rMaxNumGames = maxNumGames - (vals.size()-ipos);
		
		int winAtTheEnd = prefsums[vals.size()] - prefsums[ipos];
		//int min_rl = minTree.get(0,rMaxNumGames+1) + winAtTheEnd;
		int min_rl = prefsums_minpref[rMaxNumGames+1] + winAtTheEnd;
		
		int result = min(min_r, min_rl);
		E < "getMinWin( ipos==" < ipos < ", maxNumGames==" < maxNumGames < ") == " < result < NL;
		
		return result;
	}
	
	// returns time to bancrupt or ULLONG_MAX
	ull getTimeToBancrupt(int ipos, int budget){
		ull time = 0;
		
		// cycle negative, will loose eventually
		int winPerCycle = prefsums[vals.size()];
		E < NL < "winPerCycle " < winPerCycle < NL;
		if(winPerCycle < 0){
			E < "cycle negative, will loose eventually" < NL;
			int minWinPerCycle = getMinWin(ipos);
			int fullCycles = (budget + minWinPerCycle) / -winPerCycle;
			fullCycles = max(fullCycles, 0);
			E < "fullCycles " < fullCycles < NL;
			time += 1ULL*fullCycles*vals.size();
			budget += 1LL*fullCycles*winPerCycle;
			
			if(budget + minWinPerCycle > 0){
				E < "fullCycles++" < NL;
				time += vals.size();
				budget += winPerCycle;
			}
		}
		
		E < "already spent " < time < " time. remaining budget: " < budget < NL;
		
		int b = vals.size()+1;
		
		int desiredPref = prefsums[ipos] - budget;
		E < "desiredPref: " < desiredPref < NL;
		auto& indices = val_to_idx[desiredPref];
		auto lbr = lower_bound(indices.begin(), indices.end(), ipos);
		if(lbr != indices.end()){
			//auto r = lbr - indices.begin();
			b = *lbr - ipos;
			E < "bancrupt after " < b < " more moves" < NL;
		}
		else{
			// check left part
			E < "check left part" < NL;
			desiredPref -= prefsums[vals.size()];
			auto& indices2 = val_to_idx[desiredPref];
			if(!indices2.empty()){
				auto where = indices2.front();
				if(where <= ipos){
					b = vals.size()-ipos + where;
				}
			}
		}
		
		if(b == si vals.size()+1 and winPerCycle >= 0) return ULLONG_MAX;
		
		return time + b;
	}
};

vector<char> data;
vector<Cycle> cycles;
umap<int,pii> whichCycle;

vector<int> oszczednosci;

vector<int> vis;

inline void tc(){
	int n = ri;
	oszczednosci.resize(n);
	
	FOR(i,n) oszczednosci[i] = ri;
	
	cycles.reserve(n);
	
	int m = ri;
	data.resize(m);
	FOR(i,m){
		char c = rgraph;
		if(c=='W')c=1;
		else c=-1;
		data[i]=c;
	}
	
	E < "input loaded" < NL;
	
	int delta = n%m;
	vis.resize(m);
	FOR(i,m){
		// start cycle from i
		if(vis[i])continue;
		
		E < "starting cycle from ipos " < i < NL;
		
		cycles.push_back(Cycle());
		Cycle& cycle = cycles.back();
		for(int j=i;;){
			vis[j] = 1;
			whichCycle[j] = mp(cycles.size()-1, cycle.vals.size());
			cycle.vals.push_back(data[j]);
			
			j=(j+delta)%m;
			if(j==i)break;
		}
		E < "init cycle " < cycles.size()-1 < " that starts at " < i < NL;
		cycle.init();
		//cycles.push_back(cycle);
		//cycles.back().init();
	}
	E < "have " < cycles.size() < " cycles" < NL;
	
	ull result = ULLONG_MAX;
	int iresult = 0;
	
	// check every person
	FOR(i,oszczednosci.size()){
		auto icycle_ipos = whichCycle[i%m];
		auto icycle = icycle_ipos.fi;
		auto ipos = icycle_ipos.se;
		
		Cycle& cycle = cycles[icycle];
		E < NL < "current player: " < i < "   icycle " < icycle < "   ipos " < ipos < NL;
		
		// optim: ignore this player if he doesn't loose earlier
		//ull minWin = cycle.getMinWin(ipos, result);
		//if(budget + minWin > 0) continue;
		
		ull cresult = cycle.getTimeToBancrupt(ipos, oszczednosci[i]);
		E < "time to bancrupt for player " < i < ": " < cresult < NL;
		if(cresult < result){
			result = cresult;
			iresult = i;
		}
	}
	
	if(result == ULLONG_MAX){
		O < "-1" < NL;
		return;
	}
	
	result = (result-1) * oszczednosci.size() + iresult + 1;
	
	O < result < NL;
}