Revenge of Fibonacci

Time limit5sMemory limit128 MB

Summary
For each of up to 50,000 queries, find the smallest Fibonacci index below 100,000 whose decimal representation starts with the given digit string, or print -1.
Level

Medium7 of 10

Topics
Math, Binary search, Implementation, Sorting
Solved
No attempts yet

Problem

The Fibonacci sequence is defined as follows.

  • F(0)=F(1)=1F(0) = F(1) = 1
  • F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2) (for n≥2n \ge 2)

Here nn is called the index of the Fibonacci number F(n)F(n).

The Fibonacci sequence has been studied for a very long time, and countless properties are known today. Seonyeong loves studying Fibonacci numbers even more than coding. After reading many papers on them, she came to believe there was no new property left to discover.

That night, Fibonacci appeared in her dream and said: "There is still an important property left unknown. For example, the Fibonacci number 347746739..."

Seonyeong woke up and tried to recall the rest of the digits, but she could not. So she decides to write a program to figure out what that number is.

Given the leading digits of some Fibonacci number, write a program that finds the smallest index among the Fibonacci numbers that start with those digits.

Input

The first line contains the number of test cases TT (T≤50,000T \le 50{,}000).

Each test case consists of a single line containing the leading digits of some Fibonacci number. This number has at most 40 digits and has no unnecessary leading zeros.

Output

For each test case, print one line in the format Case #x: y, where xx is the test case number (starting from 1) and yy is the smallest index among the Fibonacci numbers that start with the given digits.

If no Fibonacci number with an index smaller than 100,000 starts with the given digits, print −1-1 in place of yy.

Examples2

  1. Example 1

    Input
    15
    1
    12
    123
    1234
    12345
    9
    98
    987
    9876
    98765
    89
    32
    51075176167176176176
    347746739
    5610
    
    Expected output
    Case #1: 0
    Case #2: 25
    Case #3: 226
    Case #4: 1628
    Case #5: 49516
    Case #6: 15
    Case #7: 15
    Case #8: 15
    Case #9: 43764
    Case #10: 49750
    Case #11: 10
    Case #12: 51
    Case #13: -1
    Case #14: 1233
    Case #15: 22374
    
  2. Example 2

    Input
    3
    1
    2
    3
    
    Expected output
    Case #1: 0
    Case #2: 2
    Case #3: 3