P21489 [HackerEarth] Let's consider some array A. The following algorithm calculates it's force:
普及-
题目描述

Let's consider some array A. The following algorithm calculates it's force:
Find all the continuous blocks where all the elemens of A are equal.
Calculate sum of squared lengths of these blocks.

For example if array A = {2, 3, 3, 1, 1, 1, 4, 2, 2} its force will be 1^2 + 2^2 + 3^2 + 1^2 + 2^2 = 19

We can reorder some elements and get array with greater force. In the example above we can move the first element of A to the end and get array A = {3, 3, 1, 1, 1, 4, 2, 2, 2} with force 2^2 + 3^2 + 1^2 + 3^2 = 23.

You are given an array. What is the maximum force you can get by reordering some of its elements?

Input
The first line contains integer T denoting the number of test cases.
The following T lines contain 4 integers each: A[0], A[1], N, MOD.

Array A of N elements is generated by the following way:
A[0], A[1] are given
A[i] = (A[i - 1] + A[i - 2]) modulo MOD for 1 < i < N.

Output
For each test case output one integer on the separate line - answer for the question.

Constraints
1 ≤ T ≤100
0 ≤ A[0], A[1] ≤ 10^6
2 ≤ N ≤ 10^6
max(A[0] , A[1]) < MOD < 10^6

SAMPLE INPUT
2
0 1 6 4
1 1 4 2

SAMPLE OUTPUT
12
10

Explanation

Test case 1: array A = {0, 1, 1, 2, 3, 1}. The greatest possible force will be with the reordering {2, 0, 1, 1, 1, 3}

Test case 2: array A = {1, 1, 0, 1}. The greatest possible force will be with the reordering {1, 1, 1, 0}

输入格式

见题面

输出格式

见题面

样例输入
100 881349 341648 836 918783 2 2 221 3 5 4 763 7 4 1 992 5 7 1 595 9 19785 19735 881 44670 10951 1861 46 80965 0 1 344 2 279081 448772 72 973258 3 0 471 6 1 4 850 8 544198 454458 757 863475 0 0 652 2 1 2 863 5 0 0 905 2 605117 224732 722 957445 1 0 987 5 2 0 649 3 0 0 940 3 23356 208578 813 214181 583830 808167 368 988057 2 4 906 9 868193 126726 247 884609 6 3 521 7 44612 8517 353 82538 0 2 278 3 482788 42270 112 873757 41278 29273 492 51577 2 1 315 3 8 4 643 9 5 0 925 8 2 1 985 3 0 0 149 2 700125 470797 258 957201 20043 8969 744 191281 2 1 787 3 266281 202066 930 659831 0 0 880 2 47741 31555 431 135558 508192 494505 834 941406 3 3 991 5 0 1 341 2 4 0 673 6 147019 120456 735 168034 0 1 313 2 0 0 56 4 0 1 216 6 0 3 697 6 89986 460202 846 558094 279735 445901 346 781688 1 3 182 8 0 1 156 3 1 0 461 2 331259 58442 354 390184 1 0 302 5 0 0 768 1 1 3 302 4 0 2 67 4 3 1 499 4 122478 555320 308 581086 1 6 784 10 334910 336570 913 351627 213092 48653 483 326294 5 4 76 8 749650 541615 829 827034 706174 113270 379 860569 41413 14754 578 47178 1 2 233 4 2 4 51 5 4 5 590 6 30620 280153 515 308359 0 0 260 4 0 2 116 4 259043 305664 596 347599 34296 538862 177 685839 0 3 550 5 378962 122484 113 520506 607301 67992 840 713282 7 4 982 9 130413 150764 74 259707 219834 107357 255 513408 2 0 360 7 130894 388838 135 584708 4 1 341 6 159531 68868 439 171838 6910 11597 420 162939 4 1 481 6 1 1 258 2 3 3 902 5 102479 51899 504 201251 0 0 870 1 592440 93296 224 593368 4 3 725 5 23919 130909 561 262197 134290 57493 470 397379 1 8 744 9 145355 31550 631 747988 0 2 465 3 211063 138593 609 343494 7 3 736 8
样例输出
33265 150498486 11666603 23596610 216150312 1416 5627 274436770 71460 483537780 93933973 8527 550137025 228772009 198612649 9187 6697879 340975697 1440050704 678 380155 77162265 22145 17088556 645 314443089 16665 59655 63682041 145255124 100878701 5624701 286760356 6430 11806 6570570 33022 1517336209 642 3535 13281245 505273834 195005410 41188 684356404 1291396096 85933643 139455368 42640 2237 45849604 160866369 544613098 723042 183496830 174002481 30793645 24050632 398638242 52932 23468221 117913 40304 65640095 32603 40478 72671 228988034 212486807 141505063 26438 596434084 11759645 378301 110989 4685120 80715 37338 18915454 159990 90239 251924405 25348 136070999 70762 25220 272317784 1425780 6962000 28092 910651329 71318 4468911 518241 17850 6696222 100 347939777 103996 96746080