atcoder#ABC283H. [ABC283Ex] Popcount Sum

[ABC283Ex] Popcount Sum

Score : 600600 points

Problem Statement

Find the sum of popcounts of all integers between 11 and NN, inclusive, such that the remainder when divided by MM equals RR. Here, the popcount of a positive integer XX is the number of 11s in the binary notation of XX, that is, the number of non-negative integers kk such that the 2k2^ks place is 11. For each input, process TT test cases.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 1MN1091 \leq M \leq N \leq 10^9
  • 0R<M0 \leq R < M
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format. The first line is as follows:

TT

TT test cases follow. Each of them is given in the following format:

NN MM RR

Output

Print TT lines. The ii-th line should contain the answer to the ii-th test case.

2
12 5 1
6 1 0
6
9

In the 11-st test case, the popcount of 11 is 11, the popcount of 66 is 22, and the popcount of 1111 is 33, so 1+2+3=61+2+3=6 should be printed. In the 22-nd test case, the popcount of 11 is 11, 22 is 11, 33 is 22, 44 is 11, 55 is 22, and 66 is 22, so 1+1+2+1+2+2=91+1+2+1+2+2=9 should be printed.