This page is still under construction.

Parts of this page are still being built. What you see may change.

Fair and Square (Large2)

Time limit5sMemory limit512 MB

Summary
Count numbers in each interval [A, B] that read the same forwards and backwards and equal the square of such a number.
Level

Hard8 of 10

Topics
Math, String, Binary search
Solved
No attempts yet

Problem

Little John likes palindromes and calls them fair. A palindrome is an integer that reads the same forwards and backwards, so 6, 11 and 121 are palindromes while 10, 12, 223 and 2244 are not. Numbers are written without leading zeros, so 010 is the same number as 10 and does not count as a palindrome.

John also became interested in squares, and defined a fair and square number this way: a number is fair and square if it is a palindrome and it is the square of a palindrome. For example 1, 9 and 121 are fair and square, because each one is a palindrome and they are the squares of 1, 3 and 11. On the other hand 16, 22 and 676 are not fair and square: 16 is not a palindrome, 22 is not a square, and 676 is both a palindrome and a square but it is the square of 26, which is not a palindrome.

Given one interval, count the fair and square numbers inside it.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains two integers AA and BB, the endpoints of one interval, separated by a space.

Limits

  • 1≤T≤10001 \le T \le 1000
  • 1≤A≤B≤101001 \le A \le B \le 10^{100}

Output

For each test case, print one line of the form Case #x: y, where x is the test case number starting from 1 and y is the number of fair and square numbers that are greater than or equal to AA and less than or equal to BB.

Examples2

  1. Example 1

    Input
    3
    1 4
    10 120
    100 1000
    
    Expected output
    Case #1: 2
    Case #2: 0
    Case #3: 2
  2. Example 2

    Input
    4
    1 1
    5 5
    676 676
    484 484
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: 0
    Case #4: 1