Easy Homework

아직 제출이 없습니다시간 제한1.5초메모리 제한256 MB

문제

Let us fix an integer AA. Consider a sequence f(n)\\{f(n)\\} which satisfies the following two conditions:

  1. f(0)=0f(0) = 0, f(1)=1f(1) = 1;
  2. f(n)=Af(n1)+f(n2)f(n) = A \cdot f(n - 1) + f(n - 2) for any integer n>1n > 1.

Given a prime pp and an integer xx (0xp0 \leq x \leq p), your task is to calculate n:LnR, f(n)modp=x|\\{n : L \leq n \leq R, \ f(n) \bmod p = x\\}|, that is, the number of indices nn between LL and RR such that f(n)modp=xf(n) \bmod p = x.

입력

There are one or more test cases.

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

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

출력

Print TT lines, one for each test case, containing the answers to the problem.