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 sun.dc.pr.PRError;

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

public class Solution {


    static MyScanner sc;
    private static PrintWriter out;
    static long M = 1000000007;


    public static void main(String[] s) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
//
//        stringBuilder.append("5 9\n" +
//                "mahmoud mahmoudbadawy drmahmoud drevil mahmoud\n" +
//                "1 1 5\n" +
//                "1 1 2\n" +
//                "1 2 3\n" +
//                "2 3 mahmoud\n" +
//                "2 4 mahmoud\n" +
//                "2 2 mahmouu\n" +
//                "1 1 5\n" +
//                "1 2 3\n" +
//                "1 1 1\n");


        if (stringBuilder.length() == 0) {
            sc = new MyScanner(System.in);
        } else {
            sc = new MyScanner(new BufferedReader(new StringReader(stringBuilder.toString())));
        }

        out = new PrintWriter(new OutputStreamWriter(System.out));

        initData();
        solveT();
        out.flush();
    }


    static long[][][] tt = new long[11][19][];
    static long[][][] tt2 = new long[11][19][];

    private static void initData() {
        for (int k = 2; k <= 10; k++) {
            tt[k][0] = new long[(1 << k)];
            tt2[k][0] = new long[(1 << k)];
            tt[k][0][0] = 1;


            for (int r = 1; r < 19; r++) {
                tt[k][r] = new long[(1 << k)];
                tt2[k][r] = new long[(1 << k)];
                for (int i = 0; i < k; i++) {
                    for (int t = 0; t < (1 << k); t++) {
                        tt[k][r][t ^ (1 << i)] += tt[k][r - 1][t];
                    }
                }
                for (int i = 1; i < k; i++) {
                    for (int t = 0; t < (1 << k); t++) {
                        tt2[k][r][t ^ (1 << i)] += tt[k][r - 1][t];
                    }
                }
            }
        }
    }


    private static void solve() throws IOException {
        int n = sc.nextInt();
        int l = sc.nextInt();
        int k = sc.nextInt();
        String f = Integer.toString(l - 1, n);
        String r = Integer.toString(k, n);

        out.println(count(tt[n], tt2[n], r) - count(tt[n], tt2[n], f));
    }

    private static long count(long[][] longs, long[][] longs1, String f) {
        StringBuilder x = new StringBuilder();
        long res = 0;
        for (int c = 0; c < f.length(); c++) {
            res += longs1[c][0];
        }
        return res + count(longs, f, 0, true);
    }


    private static long count(long[][] longs, String f, int i, boolean fO) {
        if (f.length() == 0) return 0;
        long res = 0;
        int o = f.length();
        int r = fO ? '1' : '0';
        for (; r < f.charAt(0); r++) {
            res += longs[o - 1][i ^ (1 << (r - '0'))];
        }
        res += count(longs, f.substring(1, f.length()), i ^ (1 << (f.charAt(0) - '0')), false);
        return res;
    }


    private static void solveT() throws IOException {
        int t = sc.nextInt();
        while (t-- > 0) {
            solve();
        }
    }


    private static long gcd(long l, long l1) {
        if (l > l1) return gcd(l1, l);
        if (l == 0) return l1;
        return gcd(l1 % l, l);
    }

    private static long pow(long a, long b, long m) {
        if (b == 0) return 1;
        if (b == 1) return a;
        long pp = pow(a, b / 2, m);
        pp *= pp;
        pp %= m;
        return (pp * (b % 2 == 0 ? 1 : a)) % m;
    }


    static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        MyScanner(BufferedReader br) {
            this.br = br;
        }

        public MyScanner(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

        void findToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        String next() {
            findToken();
            return st.nextToken();
        }

        Integer[] nab(int n) {
            Integer[] k = new Integer[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.fi();
            }
            return k;
        }

        int[] na(int n) {
            int[] k = new int[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.fi();
            }
            return k;
        }

        long[] nl(int n) {
            long[] k = new long[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.nextLong();
            }
            return k;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int fi() {
            String t = next();
            int cur = 0;
            boolean n = t.charAt(0) == '-';
            for (int a = n ? 1 : 0; a < t.length(); a++) {
                cur = cur * 10 + t.charAt(a) - '0';
            }
            return n ? -cur : cur;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }


}