Under construction

Still being built. Things may change

Easy Homework

Time limit1.5sMemory limit256 MB

Summary
Count the indices n in [L, R] where f(n) mod p equals x, for the sequence f(0)=0, f(1)=1, f(n)=A·f(n-1)+f(n-2).
Level

Hard8 of 10

Topics
Number theory, Matrix, Math
Solved
No attempts yet

Problem

Fix an integer AA. A sequence {f(n)}\{f(n)\} satisfies the following two conditions.

  1. f(0)=0f(0) = 0, f(1)=1f(1) = 1
  2. f(n)=A⋅f(n−1)+f(n−2)f(n) = A \cdot f(n - 1) + f(n - 2) for every integer n>1n > 1.

Given a prime pp and an integer xx (0≤x≤p0 \leq x \leq p), count the indices nn with L≤n≤RL \leq n \leq R that satisfy f(n) mod p=xf(n) \bmod p = x.

Input

The first line contains an integer TT, the number of test cases (1≤T≤421 \leq T \leq 42).

Each of the next TT lines contains five integers AA, pp, xx, LL and RR (0≤A<1090 \leq A < 10^{9}, 2<p<1092 < p < 10^{9}, 0≤x<p0 \leq x < p, 1≤L≤R≤10181 \leq L \leq R \leq 10^{18}). It is guaranteed that pp is prime.

Output

Print TT lines. Line ii contains the answer for the ii-th test case.

Examples1

  1. Example 1

    Input
    2
    1 5 0 1 5
    2 29 12 3 6
    
    Expected output
    1
    2