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

public class C {

    void solve() throws IOException {
        in("C-large.in"); out("C-large.out");

        long tm = System.currentTimeMillis();
        boolean[] mask = new boolean[2000000];
        int[] r = new int[10];
        int t = readInt();
        for (int cs = 1; cs <= t; ++cs) {
            int a = readInt();
            int b = readInt();
            int ans = 0;
            for (int m = a + 1; m <= b; ++m) {
                char[] d = String.valueOf(m).toCharArray();
                int c = 0;
                for (int i = 1; i < d.length; ++i) {
                    if (d[i] != '0' && d[i] <= d[0]) {
                        int n = 0;
                        for (int j = 0; j < d.length; ++j) {
                            int jj = i + j;
                            if (jj >= d.length) jj -= d.length;
                            n = n * 10 + (d[jj] - '0');
                        }
                        if (n >= a && n < m && !mask[n]) {
                            ++ans;
                            mask[n] = true;
                            r[c++] = n;
                        }
                    }
                }
                for (int i = 0; i < c; ++i) {
                    mask[r[i]] = false;
                }
            }
            println("Case #" + cs + ": " + ans);
            //System.out.println(cs);
        }
        System.out.println("time: " + (System.currentTimeMillis() - tm));
        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new C().solve();
    }
}


/**
 * Write a description of class solver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class solver
{
    public int aise(int a[],int k,int j)
    {
        int i;
        int temp=0;
        for(i=0;i<j;i++)
        {
            temp=temp*10+a[(i+k)%j];
        }
        return temp;
    }

    public int[] reverse(int a[],int j)
    {
        int i,temp;
        for(i=0;i<j/2;i++)
        {
            temp=a[i];
            a[i]=a[j-i-1];
            a[j-i-1]=temp;
        }
        return a;
    }

  public boolean greater(int a[],int i,int n)
  {
      int j=0;
      for(j=0;j<n;j++)
      {
          if(a[(i+j)%n]<a[j])
          return false;
          else if(a[(i+j)%n]>a[j])
          return true;
        }
        return false;
    }
    public boolean lesser(int a[],int b[],int k,int j,int n1)
    {
        if(j<n1)
        return true;
        else
        {
            int i=0;
            for(i=0;i<n1;i++)
            {
                if(a[(k+i)%j]>b[i])
                return false;
                else if(a[(k+i)%j]<b[i])
                return true;
            }
            return true;
        }
    }


    public int solve(int a,int b)
  {
      int i,k,s;
      int n1=0,temp,temp1=b,j;
      int[] arr=new int[10];
      int count=0;
      int[] arr2=new int[10];
      int[] comp=new int[10];
      while(temp1>0)
          {
              arr2[n1++]=temp1%10;
              temp1=temp1/10;
           }
    
      arr2=reverse(arr2,n1);
      for(i=a;i<b;i++)
      { 
          temp=i;
          j=0;
          while(temp>0)
          {
              arr[j++]=temp%10;
              temp=temp/10;
           }
           arr=reverse(arr,j);
           s=0;
           for(k=j-1;k>0;k--)
           {
               if(arr[k]!=0)
               {
                   if(greater(arr,k,j) && lesser(arr,arr2,k,j,n1))
                   {
                   count++;
                   comp[s++]=aise(arr,k,j);
                }
                }
            }
            for(k=0;k<s;k++)
            {
                for(j=k+1;j<s;j++)
                    {
                           if(comp[k]==comp[j])
                           {
                               count--;
                            }
                        }
                    }
                
            }
          return count;
        }
      
      
    }
    

