package Main;

import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: arran
 * Date: 14/04/12
 * Time: 3:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class Round {
    public StringBuilder parse(BufferedReader in) throws IOException {
        StringBuilder out = new StringBuilder();
        String lineCount = in.readLine();

        for (int i = 1; i <= Integer.parseInt(lineCount); i++)
        {
            out.append("Case #"+i+": ");
            System.err.println("Case #"+i+": ");
            String line = in.readLine();
            String[] splits = line.split(" ");
            int p1 = Integer.valueOf(splits[0]);
            int p2 = Integer.valueOf(splits[1]);
            int r = pairRecyclable(p1, p2);
            out.append(r);

//            if (i < Integer.parseInt(lineCount))
                out.append("\n");
        }
        return out;
    }

    public static int pairRecyclable(int i1, int i2) {
        HashSet<String> hash = new HashSet<String>();
        for (int i = i1; i < i2; i++)
        {
            String istr = String.valueOf(i);
            if (istr.length() < 2) continue;
            for (int p = 0; p < istr.length() ; p++)
            {
                String nistr = istr.substring(p,istr.length()).concat(istr.substring(0,p));
                if (Integer.valueOf(nistr) < i1) continue;
                if (Integer.valueOf(nistr) > i2) continue;
                if (nistr.equals(istr)) continue;
                String cnistr = (Integer.valueOf(nistr) > Integer.valueOf(istr)) ?  istr + "," + nistr : nistr + "," + istr;
                hash.add(cnistr);
            }
        }
        return hash.size();
    }

    public static void main(String[] args) {
        InputStreamReader converter = null;
        try {
            int attempt = 0;
            String quest = "C";
//            String size = "small-attempt";
            String size = "large";
            converter = new InputStreamReader(new FileInputStream("src/resource/"+quest+"-"+size+".in"));
            BufferedReader in = new BufferedReader(converter);
            Round r = new Round();
            String str = r.parse(in).toString();
            System.out.print(str);
            FileOutputStream fos = new FileOutputStream(quest+"-"+size+".out");
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(str);
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package competition;

/**
 *
 * @author Sriram
 */
//import static java.lang.System.*;
import java.util.*;
import java.io.*;

public class RecycledNumbers_GCJ_2012{

    public static void main(String args[]) throws Exception
    {
      Scanner in = new Scanner(new java.io.FileInputStream("f:\\input.in"));
      PrintStream out = new PrintStream(new FileOutputStream("f:\\output.out"));
      int T = in.nextInt();
      for(int zz=0; zz<T;zz++)
      {
          int A,B;
          A = in.nextInt();
          B = in.nextInt();
          HashSet<Integer> s = new HashSet<Integer>();
          int count = 0;
          for(int i=A;i<(B+1);i++)
          {
              int[] perm = rotations(i);
              for(int e: perm)
              {
                  if((e>=A)&&(e<=B)&&(e>i))
                  {
                     s.add(e);
                     count++;
                  }
              }
          }
          out.printf("Case #%d: %d\n",(zz+1),count);
      }
    }

    static int[] rotations(int n) {
        // Max places for small set: 4
        // Max places for large set: 7
        if((n/1000000)>0)
        {
            int a,b,c,d,e,f,g;
            g = n%10;
            n/=10;
            f = n%10;
            n/=10;
            e = n%10;
            n/=10;
            d = n%10;
            n/=10;
            c = n%10;
            n/=10;
            b = n%10;
            n/=10;
            a = n%10;
            return new int[]{(a*1000000+b*100000+c*10000+d*1000+e*100+f*10+g),(b*1000000+c*100000+d*10000+e*1000+f*100+g*10+a),(c*1000000+d*100000+e*10000+f*1000+g*100+a*10+b),(d*1000000+e*100000+f*10000+g*1000+a*100+b*10+c),(e*1000000+f*100000+g*10000+a*1000+b*100+c*10+d),(f*1000000+g*100000+a*10000+b*1000+c*100+d*10+e),(g*1000000+a*100000+b*10000+c*1000+d*100+e*10+f)};
        }
        if((n/100000)>0)
        {
            int a,b,c,d,e,f;
            f = n%10;
            n/=10;
            e = n%10;
            n/=10;
            d = n%10;
            n/=10;
            c = n%10;
            n/=10;
            b = n%10;
            n/=10;
            a = n%10;
            return new int[]{(a*100000+b*10000+c*1000+d*100+e*10+f),(b*100000+c*10000+d*1000+e*100+f*10+a),(c*100000+d*10000+e*1000+f*100+a*10+b),(d*100000+e*10000+f*1000+a*100+b*10+c),(e*100000+f*10000+a*1000+b*100+c*10+d),(f*100000+a*10000+b*1000+c*100+d*10+e)};
        }
        if(n/10000>0)
        {
          int a,b,c,d,e;
          e = n%10;
          n/=10;
          d = n%10;
          n/=10;
          c = n%10;
          n/=10;
          b = n%10;
          n/=10;
          a = n%10;
          return new int[]{(a*10000+b*1000+c*100+d*10+e),(b*10000+c*1000+d*100+e*10+a),(c*10000+d*1000+e*100+a*10+b),(d*10000+e*1000+a*100+b*10+c),(e*10000+a*1000+b*100+c*10+d)};
        }
        if((n/1000>0))
        {
        if (n == 1000) {
            return new int[]{1000, 100, 10, 1};
        }
        int a,b,c,d;
        d = n%10;
        a = n/1000;
        b = (n-(a*1000))/100;
        c = ((n%100)-d)/10;
        return new int[]{(a*1000+b*100+c*10+d),(b*1000+c*100+d*10+a),(c*1000+d*100+a*10+b),(d*1000+a*100+b*10+c)};
        }
        if ((n / 100) > 0) {
            if (n == 100) {
                return new int[]{100, 10, 1};
            }
            int x, y, z;
            z = n%10;
            x = n/100;
            y = (n-(x*100)-z)/10;
            return new int[]{(x * 100) +(y * 10) + z, (y * 100 + z * 10 + x), (z * 100 + x * 10 + y)};
        }
        if((n/10)>0)
        {
            if(n == 10)
                return new int[]{10,1};
            int x,y;
            x = n/10;
            y = n%10;
            return new int[]{x*10+y,y*10+x};
        }
        return new int[]{n};

    }
}
