RoAlgo Contest #8 | Banda lu' Bahoi

This was the problem page during the contest. Access the current page here.
Time limit: 2s Memory limit: 64MB Input: Output:

As Christmas approaches, things become very busy at the North Pole. The elves, Santa's main workforce, have an infinitely long conveyor belt divided into slots. Initially, there is only one gift on the conveyor belt, located at position 11. To determine the number of gifts in slot kk, Bahoi, Santa's chief executive, will move the conveyor belt one position to the right, adding the number of gifts from position k1k-1 (after the move) to the number of gifts from position k1k-1 (before the move).

In other words, if qkq_k is the number of gifts in slot kk, then qk=qk1+qk2q_k = q_{k-1} + q_{k-2}, for any k2k \geq 2.

Task

Due to the chaos at the North Pole, chief executive Bahoi wants to verify if he has correctly calculated the number of gifts in each slot. Thus, he asks himself QQ questions, each question being in the form: what is the total number of gifts in the slots between ll and rr? Due to his busy schedule, he considers it sufficient to find the remainder of the total number of gifts when divided by MM.

Input data

The first line contains the number QQ and the next QQ lines contain 3 numbers each, representing ll, rr, and MM.

Output data

On line ii of the output, you will find the answer to the question number ii.

Constraints and clarifications

  • We will consider that there are no gifts at position 00.
  • 1Q2000001 \leq Q \leq 200\,000
  • 0lr10180 \leq l \leq r \leq 10^{18}
  • 1M21091 \leq M \leq 2 \cdot 10^9
  • For 4040 points, r200000r \leq 200\,000 and M=109+7M = 10^9 + 7.

Example

stdin

3
0 5 1000
1 3 1000000007
8 8 18

stdout

12
4
3

Explanation

For instance, for the first query, the total number of gifts in slots 00 to 55 is 1212, and 12mod100012 \mod 1000 is 1212. For the second query, the total number of gifts in slots 11 to 33 is 44, and 4mod10000000074 \mod 1000000007 is 44. For the third query, the number of gifts in slot 88 is 2121, and 21mod1821 \mod 18 is 33.

Log in or sign up to be able to send submissions!