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 bi, li and ri, 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 bi, li, ri (2 ≤ bi ≤ 10, 1 ≤ li ≤ ri ≤ 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.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        final int N = 100;
        long[][] C = new long[N][N];
        boolean[][][][] was = new boolean[2][11][66][1 << 10];
        long[][][][] dp = new long[2][11][66][1 << 10];

        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            C[0][0] = 1;
            for (int i = 1; i < N; i++) {
                C[i][0] = 1;
                for (int j = 1; j < N; j++) {
                    C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
                }
            }

            boolean stress = false;
            if (stress) {
                Random random = new Random(0);
                for (int i = 0; i < (int) 1e5; i++) {
                    int b = random.nextInt(9) + 2;
                    long r = (long) 1e18 - random.nextInt(1000000);
                    long l = (long) 1e18 - random.nextInt(1000000);
                    solve(b, l);
                    solve(b, r);
                    if (i < (int) 1e3) {
                        b = 10;
                        r = random.nextInt((int) 1e4);
                        long u = solve(b, r);
                        long v = dummy(b, r);
                        if (u != v) {
                            throw new AssertionError(b + " " + r + " " + u + " " + v);
                        }
                    }
                }
            }

            int numQueries = in.nextInt();
            for (int queryId = 0; queryId < numQueries; queryId++) {
                int b = in.nextInt();
                long l = in.nextLong();
                long r = in.nextLong();
                out.println(solve(b, r) - solve(b, l - 1));
            }
        }

        private long solve(int b, long r) {
            if (r == 0) {
                return 1;
            }
            int[] digits = getDigits(b, r);
            int n = digits.length;

            long res = 1;
            for (int numberLen = 1; numberLen < n; numberLen++) {
                res += calc(b, numberLen, 0, 0);
            }

            // number len equals n
            for (int prefLen = 0; prefLen <= n; prefLen++) {
                int mask = 0;
                for (int i = 0; i < prefLen; i++) {
                    mask ^= 1 << digits[n - i - 1];
                }
                if (prefLen == n) {
                    res += calc(b, 0, mask, 1);
                    continue;
                }
                for (int last = 0; last < digits[n - prefLen - 1]; last++) {
                    if (last == 0 && prefLen == 0) {
                        continue;
                    }
                    mask ^= 1 << last;
                    res += calc(b, n - prefLen - 1, mask, 1);
                    mask ^= 1 << last;
                }
            }
            return res;
        }

        private long calc(int base, int n, int mask, int leadingZeroAllowed) {
            if (n == 0) {
                return mask == 0 ? 1 : 0;
            }
            if (was[leadingZeroAllowed][base][n][mask]) {
                return dp[leadingZeroAllowed][base][n][mask];
            }
            was[leadingZeroAllowed][base][n][mask] = true;
            long[] d = new long[n + 1];
            long[] nd = new long[n + 1];
            d[0] = 1;
            for (int digit = 0; digit < base; digit++) {
                Arrays.fill(nd, 0);
                for (int old = 0; old <= n; old++) {
                    if (d[old] == 0) {
                        continue;
                    }
                    for (int here = 0; here + old <= n; here++) {
                        if (here % 2 != ((mask >> digit) & 1)) {
                            continue;
                        }
                        long ways = C[n - old][here];
                        if (leadingZeroAllowed == 0 && digit == 0) {
                            ways = C[n - 1][here];
                        }
                        nd[old + here] += d[old] * ways;
                    }
                }
                long[] t = d;
                d = nd;
                nd = t;
            }
            dp[leadingZeroAllowed][base][n][mask] = d[n];
            return d[n];
        }

        private int[] getDigits(int b, long r) {
            int n = 0;
            long x = r;
            while (x > 0) {
                x /= b;
                ++n;
            }
            int[] digits = new int[n];
            n = 0;
            x = r;
            while (x > 0) {
                digits[n] = (int) (x % b);
                x /= b;
                ++n;
            }
            return digits;
        }

        private long dummy(int b, long r) {
            int res = 1; // zero
            for (int i = 1; i <= r; i++) {
                int[] d = getDigits(b, i);
                int mask = 0;
                for (int j = 0; j < d.length; j++) {
                    mask ^= 1 << d[j];
                }
                if (mask == 0) {
                    ++res;
                }
            }
            return res;
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

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

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

    }
}

