TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Ha!! Another easy problem for you!! Or is it not that easy?

Given the integers a, b, M, x,

Plase print (ax+(a+1)x+(a+2)x+...+bx) mod M

Input Format

First line contains a number T, denoting the number of testcases.

Each testcase consists of the four numbers a, b, x, M in a line.

T <= 500


1 <= a <= b <= 2147483647

0 <= x <= 250

1 <= M <= 1000000010, M is always prime.

Output Format

For each testcase, output the result of the summation in a line.

Sample Input 1

3
3 5 2 107
53 57 3 97
1 2 1 2

Sample Output 1

50
4
1

Hints

Problem Source

Migrated from old NTUJ.

kelvin

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 4000 65536 200