SP5294 REC - Recurrence

Description

Let F0 = 1. Fn = a\*Fn-1 + b for n >= 1. Find Fn (mod M).

Input Format

The first line contains T the number of test cases. Each of the next T lines contains 4 space seperated integers a, b, n and M.

Output Format

Output T lines, one corresponding to each test case.