Problem hidden

REC - Recurrence

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

Input

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

Constraints

T <= 20000
0 <= a, b, n <=  10^100
1 <= M <= 100000

Output

Output T lines, one corresponding to each test case.

Example

Input:
3
1 1 1 10
2 1 2 5
5 2 20 30

Output:
2
2
7

Adicionado por:abhijith reddy d
Data:2009-11-14
Tempo limite:1s-2.516s
Tamanho do fonte:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Linguagem permitida:ADA95 ASM32 BASH BF C CSHARP C++ 4.3.2 CPP CPP14 C99 CLPS LISP sbcl LISP clisp D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM guile SCM qobi ST WHITESPACE
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.