Salazar Slytherin’s Locket

Harry came to know from Dumbledore that Salazar Slytherin’s locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black’s mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry’s former Defense Against the Dark Arts teacher.

Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge’s office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l and r (both inclusive).

Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b, all the digits from 0 to b - 1 appear even number of times in its representation without any leading zeros.

You have to answer q queries to unlock the office. Each query has three integers b il i and r i, the base and the range for which you have to find the count of magic numbers.

Input

First line of input contains q (1 ≤ q ≤ 105) — number of queries.

Each of the next q lines contain three space separated integers b il ir i (2 ≤ b i ≤ 10, 1 ≤ l i ≤ r i ≤ 1018).

Output

You have to output q lines, each containing a single integer, the answer to the corresponding query.

Examples

input

2
2 4 9
3 1 10

output

1
2

input

2
2 1 100
5 1 100

output

21
4

Note

In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get:

  • 4 = 1002,
  • 5 = 1012,
  • 6 = 1102,
  • 7 = 1112,
  • 8 = 10002,
  • 9 = 10012.

Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.

Solution:

#include <bits/stdc++.h>

using namespace std;

long long dp[13][77][1234];

long long solve(int b, long long n) {
vector<int> d;
while (n > 0) {
d.push_back(n % b);
n /= b;
}
int len = d.size();
long long res = 0;
for (int small_len = 1; small_len < len; small_len++) {
    res += dp[b][small_len][0] - dp[b][small_len - 1][1];
  }
  int mask = 0;
  for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < d[i]; j++) {
      if (i == len - 1 && j == 0) {
        continue;
      }
      res += dp[b][i][mask ^ (1 << j)];
    }
    mask ^= (1 << d[i]);
  }
  return res;
}

int main() {
  for (int b = 2; b <= 10; b++) {
    dp[b][0][0] = 1;
    for (int i = 1; i <= 63; i++) {
      for (int t = 0; t < (1 << b); t++) {
        dp[b][i][t] = 0;
        for (int d = 0; d < b; d++) {
          dp[b][i][t] += dp[b][i - 1][t ^ (1 << d)];
        }
      }
    }
  }
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    int b;
    long long from, to;
    scanf("%d %I64d %I64d", &b, &from, &to);
    printf("%I64d\n", solve(b, to + 1) - solve(b, from));
  }
  return 0;
}