import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main {

//	static int MAX = 10000;
	static int MAX = 2000000;
	static Object[] All = new Object[MAX+1];
	
	static int size( int x ) {
		if(x>999999) return 7;
		if(x>99999)  return 6;
		if(x>9999)   return 5;
		if(x>999)    return 4;
		if(x>99)     return 3;
		if(x>9)      return 2;
		return 1;
	}
	static int[] rotate( int value ) {
		List<Integer> result = new ArrayList<Integer>();
		int[] V = new int[8];
		int s = size(value);
		int x = value;
		
		for(int i=s-1;i>=0;i--) {
			V[i] = x%10;
			x /=10;
		}
		int rot;
		for(int i=1; i<s; i++) {
			if(V[i]!=0){
				rot=0;
				for(int j=0,ii=i;j<s; j++,ii=(ii+1)%s){
					rot=rot*10+V[ii];
				}
				if(rot>value && !result.contains(rot))
					result.add(rot);
			}
		}
		if( result.isEmpty() )
			return null;
		int[] r = new int[result.size()];
		for(int i=0; i<r.length; i++)
			r[i]=result.get(i);
		return r;
	}
	
	static void precalculate() {
		for(int i=1; i<=MAX; i++){
			All[i] = rotate(i);
		}
	}
	static int solve( Scanner in ) {
		int A, B, sol=0;
		
		A = in.nextInt();
		B = in.nextInt();

		for( int i=A; i<=B; i++) {
//			List<Integer> result = rotate(i);
			if( All[i]!=null ) {
				for(int value: (int[])All[i] ) {
					if( value <= B )
						sol++;
				}
			}
		}

		return sol;
	}
	
	public static void main ( String args[] ) {
		int T; 
		Scanner in = new Scanner(System.in);
		T = in.nextInt();
		int cnt=0;
		int sol;
		precalculate();
		for( cnt=1; cnt<=T; cnt++ ) {
			sol = solve( in );
			System.out.printf("Case #%d: %d\n", cnt, sol);
		}

	}
}

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;


public class CodeJamQ3 {

  public static String[] read(final String name) throws IOException {
    final InputStreamReader reader = new FileReader(new File(name));
    final char[] buf = new char[1024];
    int count = 0;
    final StringBuilder res = new StringBuilder();
    do {
      count = reader.read(buf);
      if (count > 0)
      res.append(buf, 0, count);
    } while (count != -1);
    return res.toString().trim().split("(\\r?\\n)+");
  }

  public static void main(final String[] args) throws Exception {
    final long start = System.currentTimeMillis();
    final String[] input = read("C-large.in");
    final FileWriter out = new FileWriter("out");

    final int casesCount = Integer.parseInt(input[0]);
    for (int caseN = 1; caseN <= casesCount; caseN++) {
      final String[] data = input[caseN].split("\\s+");
      final int a = Integer.parseInt(data[0]);
      final int b = Integer.parseInt(data[1]);

      out.write("Case #" + caseN + ": " + solve(a, b) + "\n");

    }

    out.close();
    System.out.println((System.currentTimeMillis() - start));
  }

  private static long solve(final int a, final int b) {
    System.out.print(".");
    long count = 0;
    final HashSet<Tuple> check= new HashSet<Tuple>();
    final char[] number = String.valueOf(b).toCharArray(), test= new char[number.length];
    final char[] numberB = String.valueOf(b).toCharArray();
    for (int x = a; x <= b; x++) {
      final String s = String.valueOf(x);
      s.getChars(0, s.length(), number, 0);
      s.getChars(0, s.length(), test, 0);
      for (int i = 1; i < number.length; i++) {

        final char c = test[0];
        for (int j = 1; j < number.length; j++) {
          test[j - 1] = test[j];
        }
        test[test.length - 1] = c;

        if (number[0] <= number[i] && number[i] != '0') {
          // check the number
          boolean ok = true, larger = false, smaller = false;
          int eq = 0;
          for (int j = 0; j < number.length; j++) {
            if (test[j] == number[j]) { eq++; }
            if (test[j] > number[j]) { larger = true; }
            if (test[j] < numberB[j]) { smaller = true; }
            if (larger && smaller) { break; }
            if (!larger && test[j] < number[j] || !smaller && test[j] > numberB[j]) {
              ok = false;
              break;
            }
          }
          if (!ok || eq == number.length) {continue; }

          final Tuple t = new Tuple(x, Integer.parseInt(new String(test)));
          if (check.contains(t)) { continue; }
          check.add(t);
          ++count;
        }
      }
    }
    return count;
  }

  private static class Tuple {
    int x, y;
    Tuple(final int x, final int y) {this.x = x; this.y = y; }
    @Override
    public int hashCode() {
      return x + y;
    }
    @Override
    public boolean equals(final Object obj) {
      final Tuple t = (Tuple)obj;
      return t.x == x && t.y == y || t.x == y && t.y ==x;
    }
  }

}
