Under construction

Still being built. Things may change

Binary Strings

Time limit1sMemory limit512 MB

Summary
Count binary strings whose length lies in [L, R], is a multiple of K, and contains no two adjacent 1s, modulo 1e9+7.
Level

Hard8 of 10

Topics
Math, Combinatorics, Dynamic programming, Matrix
Solved
No attempts yet

Problem

A string made of only 0s and 1s is called a binary string. A boy became interested in binary strings that never place two 1s side by side. At first he counted how many such strings there are for a fixed length.

After solving that, he added more conditions. He now wants the number of binary strings that satisfy all three of the following.

  • The length of the string is between LL and RR, inclusive (1≤L≤R≤10181 \le L \le R \le 10^{18}).
  • The length of the string is a multiple of an integer KK (3≤K≤1093 \le K \le 10^9).
  • No two 1s appear consecutively in the string.

The count can be very large, so print it modulo 1,000,000,007.

Input

The first line contains the number of test cases TT (1≤T≤10 0001 \le T \le 10\,000).

Each of the next TT lines contains three integers LL, RR and KK, separated by spaces.

Output

For each test case, print one line in the form Case x: y. Here xx is the test case number starting from 1, and yy is the number of binary strings that satisfy the conditions, modulo 1,000,000,007.

Hint

For L=1L = 1, R=10R = 10 and K=3K = 3, only the lengths 3, 6 and 9 are counted. Strings that satisfy the conditions include 101, 000, 010, 101001 and 000010000.

Examples2

  1. Example 1

    Input
    2
    1 10 3
    1 10 5
    
    Expected output
    Case 1: 115
    Case 2: 157
    
  2. Example 2

    Input
    3
    1 2 3
    3 3 3
    5 5 5
    
    Expected output
    Case 1: 0
    Case 2: 5
    Case 3: 13