TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Little Peter is collecting stamps. Recently, he went shopping with his mother Lucia and guess what: As they were going around the post office, he started to blackmail his mom, as only the little boys can. At the post office, they were selling N different types of one-dollar stamps and M different types of two-dollar stamps.


Peter got exactly K dollars from his mom, and he wants to spend all of them on stamps. Note that he can buy more stamps of the same type. You may assume that the post office has an infinite stock of each type of stamp.


Now, Peter is wondering in how many ways he can buy the stamps.


Given are the integers N, M, K, and a prime number P.


Your task is to compute the value Z mod P, where Z is the (possibly huge) number of ways in which Peter can spend all K dollars on stamps.

Input Format

The first line of the input file contains an integer T<=105 specifying the number of test cases. Each test case is preceded by a blank line.


Each test case consists of a single line containing the integers N, M, K, and P.

You may assume that 3 ≤ P ≤ 1000000, and 0 ≤ N,M ≤ 300 and 1 ≤ K ≤ 1000000000000 = 1012.

Output Format

For each test case output a single line with a single integer: The number of different ways to buy stamps, modulo P.

Sample Input 1

3

0 10 2 47

2 2 4 47

5 5 10 47 

Sample Output 1

10
14
6

Hints

Problem Source

Migrated from old NTUJ.

IPSC2010 problem L

Subtasks

No. Testdata Range Score

Testdata and Limits

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