package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class mainclass {
	
	public static void main(String args[]) throws IOException {
		
		int cases;
		int a;
		int b;
		int flag;
		int counter;
		int[] data;
		int temp = 0;
		int count = 0;
		int mc;

		FileWriter fstream = new FileWriter("out.txt");
    	BufferedWriter out = new BufferedWriter(fstream);
		
		FileReader isr = new FileReader(args[0]);
		Scanner src = new Scanner(isr);
    
		cases = src.nextInt();
		data = new int[2000000];
		for(int i=0;i<2000000;i++)
			data[i] = 0;
		
		for(int j=0;j<cases;j++)
		{
			a=src.nextInt();
			b=src.nextInt();
			counter = 0;
			
			for(int i=0;i<2000000;i++)
				data[i] = 0;
			
			flag = 0;
			
			for(int i=a;i<=b;i++)
			{
				count = 0;
				System.out.print(i);
				if(data[i]==0)
				{
					count++;
					mc = numdig(i);
					temp = recnum(i,mc);
					data[i] = 1;
					for(int p=0;p<mc;p++)
					{
						if(temp>=a&&temp<=b&&data[temp]==0&&mc==numdig(temp))
						{
							System.out.print(" "+temp);
							data[temp] = 1;
							count++;
						}
						temp = recnum(temp,mc);
					}	
					flag = flag+count;
				}		
				System.out.println(" "+count);
				if(count>1)
					counter = counter + (count*(count-1))/2;
			}
			
			System.out.println(" "+flag);
			
			try{
				out.write("Case #"+(j+1)+": "+counter);
		    	out.write("\r\n");
		    	}catch (Exception e){
		    	System.err.println("Error: " + e.getMessage());
		    }
		}
		
		out.close();		
	}			
	
	static int numdig(int n)
	{
		int count = 1;
		while(n/10>0)
		{
			count++;
			n = n/10;
		}
		return count;
	}
	
	static int recnum(int n,int p)
	{
		int pow = 1;
		int num;
		num = p-1;
		for(int i=0;i<num;i++)
		{
			pow = pow*10;
		}
		return (n/10)+(pow*(n%10));
	}
}