Under construction

Still being built. Things may change

Fair and Square (Large 1)

Time limit5sMemory limit512 MB

Summary
Count the integers in each interval [A, B] that are palindromes and the square of a palindrome.
Level

Medium6 of 10

Topics
Math, Brute force, 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. Leading zeroes do not count, so writing 010 = 10 does not make 10 a palindrome.

Little John also became interested in squares, so he defined a fair and square number: a number that is a palindrome and at the same time the square of a palindrome. For example 1, 9 and 121 are fair and square, because each one is a palindrome and each one is the square of 1, 3 and 11 respectively. 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 a palindrome and a square, but its square root 26 is not a palindrome.

Given an interval, count how many fair and square numbers lie 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 separated by a space, the endpoints of one interval Little John is looking at.

Limits

  • 1≤T≤100001 \le T \le 10000
  • 1≤A≤B≤10141 \le A \le B \le 10^{14}

Output

For each test case, print one line in the form "Case #x: y", where xx is the test case number starting from 1 and yy is the number of fair and square numbers nn with A≤n≤BA \le n \le B.

Examples3

  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
    1
    1 100000000000000
    
    Expected output
    Case #1: 39
    
  3. Example 3

    Input
    12
    1 1
    2 2
    2 3
    4 4
    5 8
    9 9
    9 120
    10 120
    121 121
    122 483
    484 484
    485 10200
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: 0
    Case #4: 1
    Case #5: 0
    Case #6: 1
    Case #7: 1
    Case #8: 0
    Case #9: 1
    Case #10: 0
    Case #11: 1
    Case #12: 0