public class CopyOfFairAndSquare {
public static long [] array = new long[10000010];
public static void main(String [] args){
fillArray();
try{
// System.out.println(Double.MAX_VALUE);
// System.exit(0);
FileInputStream fstream = new FileInputStream("C-small-practice.in");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
strLine = br.readLine();
int T = Integer.valueOf(strLine);
// System.out.println("T: "+T);
int i=1;
while ((strLine = br.readLine()) != null) {
// System.out.println (strLine);
String [] str = strLine.split(" ");
double x = Double.valueOf(str[0].trim() );
double y = Double.valueOf(str[1].trim() );
//int count= getFairSqur(x,y);
long count=0;
int xr = (int)Math.sqrt(x);
int yr = (int)Math.sqrt(y);
count = array[yr]-array[xr];
if(xr*xr == (int)x){
if(checkPalindrom(xr) && checkPalindrom(xr*xr)){
count++;
}
}
System.out.println ("Case #"+i++ +": "+count);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void fillArray(){
long count = 0;
for(int i=1;i<array.length;i++){
long j = (long)i;
if(!checkPalindrom(j))
array[i]= count;
else if(!checkPalindrom(j*j))
array[i]= count;
else{
count++;
array[i]= count;
}
}
}
public static Boolean checkPalindrom(long n)
{
String normal = String.valueOf(n);
//reverse the letters in string
int length = normal.length(); //length of string
StringBuffer result = new StringBuffer(length);
int i;
for(i = length - 1; i>=0; i--)
{
result.append(normal.charAt(i));
}
long r=0;
try{
r= Long.valueOf(result.toString());
}catch(Exception e){
e.printStackTrace();
System.out.println("i:"+i+"\tr:"+r+"\tn:"+n);
}
if(r==n)
return true;
return false;
}
}