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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

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

  static class TaskE {
    static int MAXB = 10;
    static int MAXL = 32;
    static int MAXMASK = 1 << MAXB;
    int[][][] anyCnt;
    int[][] positiveCnt;
    int totalLength;
    int[] digits;

    public void solve(int testNumber, QuickScanner in, QuickWriter out) {
      init();
      for (int remQ = in.nextInt(); remQ > 0; --remQ) {
        int b = in.nextInt();
        int l = in.nextInt();
        int r = in.nextInt();
        out.println(calc(b, r) - calc(b, l - 1));
      }
    }

    void init() {
      anyCnt = new int[MAXB + 1][MAXL][MAXMASK];
      for (int b = 2; b <= MAXB; ++b) {
        anyCnt[b][0][0] = 1;
        for (int length = 1; length < MAXL; ++length)
          for (int mask = (1 << b) - 1; mask >= 0; --mask) {
            int sum = 0;
            for (int digit = 0; digit < b; ++digit) {
              sum += anyCnt[b][length - 1][mask ^ (1 << digit)];
            }
            anyCnt[b][length][mask] = sum;
          }
      }
      positiveCnt = new int[MAXB + 1][MAXL];
      for (int b = 2; b <= MAXB; ++b)
        for (int length = 1; length < MAXL; ++length) {
          int sum = 0;
          for (int digit = 1; digit < b; ++digit) {
            sum += anyCnt[b][length - 1][1 << digit];
          }
          positiveCnt[b][length] = sum;
        }
    }

    int calc(int base, int x) {
      if (x == 0) return 0;
      if (digits == null) {
        digits = new int[MAXL];
      }
      for (totalLength = 0; x > 0; x /= base) {
        digits[totalLength++] = x % base;
      }
      int res = 0;
      for (int length = 1; length < totalLength; ++length) {
        res += positiveCnt[base][length];
      }
      int mask = 0;
      for (int i = totalLength - 1; i >= 0; --i) {
        for (int digit = i + 1 == totalLength ? 1 : 0; digit < digits[i]; ++digit) {
          res += anyCnt[base][i][mask ^ (1 << digit)];
        }
        mask ^= 1 << digits[i];
      }
      return res + (mask == 0 ? 1 : 0);
    }

  }

  static class QuickScanner {
    private static final int BUFFER_SIZE = 1024;
    private InputStream stream;
    private byte[] buffer;
    private int currentPosition;
    private int numberOfChars;

    public QuickScanner(InputStream stream) {
      this.stream = stream;
      this.buffer = new byte[BUFFER_SIZE];
      this.currentPosition = 0;
      this.numberOfChars = 0;
    }

    public int nextInt() {
      int c = nextNonSpaceChar();
      boolean positive = true;
      if (c == '-') {
        positive = false;
        c = nextChar();
      }
      int res = 0;
      do {
        if (c < '0' || '9' < c) throw new RuntimeException();
        res = res * 10 + c - '0';
        c = nextChar();
      } while (!isSpaceChar(c));
      return positive ? res : -res;
    }

    public int nextNonSpaceChar() {
      int res = nextChar();
      for (; isSpaceChar(res) || res < 0; res = nextChar()) ;
      return res;
    }

    public int nextChar() {
      if (numberOfChars == -1) {
        throw new RuntimeException();
      }
      if (currentPosition >= numberOfChars) {
        currentPosition = 0;
        try {
          numberOfChars = stream.read(buffer);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        if (numberOfChars <= 0) {
          return -1;
        }
      }
      return buffer[currentPosition++];
    }

    public boolean isSpaceChar(int c) {
      return c == ' ' || c == '\t' || isEndOfLineChar(c);
    }

    public boolean isEndOfLineChar(int c) {
      return c == '\n' || c == '\r' || c < 0;
    }

  }

  static class QuickWriter {
    private final PrintWriter writer;

    public QuickWriter(OutputStream outputStream) {
      this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public QuickWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
      for (int i = 0; i < objects.length; ++i) {
        if (i > 0) {
          writer.print(' ');
        }
        writer.print(objects[i]);
      }
    }

    public void println(Object... objects) {
      print(objects);
      writer.print('\n');
    }

    public void close() {
      writer.close();
    }

  }
}

