10
4 108 114
5 30 155
8 193 197
9 71 169
2 163 166
8 120 144
8 22 151
4 21 166
2 46 127
8 38 51
 20
9 142 172
7 132 256
8 245 315
9 496 496
3 345 362
8 13 162
5 342 470
9 16 488
9 467 482
4 471 478
10 92 224
6 228 261
9 54 167
2 402 409
5 9 10
7 225 360
3 438 483
6 252 342
3 491 493
6 296 367
 1
8 1234567890123 123456789012345

0
3
0
1
3
0
5
16
9
1
 0
0
0
0
4
6
13
7
0
0
1
2
3
0
0
2
12
9
1
4
 774752389568

import java.util.*;
import java.io.IOException;

public class Test {
    long readLong() {
        long ans = 0;
        boolean neg = false;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c == '-') {
                    start = true;
                    neg = true;
                    continue;
                } else if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return neg ? -ans : ans;
    }


    int[] ds = new int[64];
    int[][][] dp = new int[11][65][1234];

    int getDigits(long v, int b, int[] ds) {
        int lp = ds.length;
        while (v > 0) {
            lp--;
            ds[lp] = (int)(v % b);
            v /= b;
        }
        return lp;
    }
    long solve(long n, int b) {
        int p = getDigits(n, b, ds);
        long ans = 0;
        for (int i = 1; ds.length - i > p; i++) ans += dp[b][i][0] - dp[b][i - 1][1];
        int mask = 0;
        for (int i = p; i < ds.length; i++) {
            for (int d = 0; d < ds[i]; d++) {
                if (i == p && d == 0) continue;
                ans += dp[b][ds.length - i - 1][mask ^ (1 << d)];
            }
            mask ^= (1 << ds[i]);
        }
        return ans;
    }

    void start() {
        for (int b = 2; b <= 10; b++) {
            dp[b][0][0] = 1;
            for (int p = 1; p <= 64; p++) {
                for (int t = 0; t < (1 << b); t++) {
                    for (int d = 0; d < b; d++)
                        dp[b][p][t] += dp[b][p - 1][t ^ (1 << d)];
                }
            }
        }
        long q = readLong();
        while (q > 0) {
            q--;
            int b = (int)readLong();
            long l = readLong(), r = readLong();
            System.out.println(solve(r + 1, b) - solve(l, b));
        }
    }

    public static void main(String[] args) {
        new Test().start();
    }
}