Under construction

Still being built. Things may change

Another FizzBuzz Task

Time limit2sMemory limit512 MB

Summary
Given a string L of digits and Fizz/Buzz letters, find the 1-based position where L first appears in the infinite FizzBuzz string, or report -1.
Level

Medium7 of 10

Topics
String matching, Number theory, Math
Solved
No attempts yet

Statement

Transform the sequence of positive integers 1,2,3,…1, 2, 3, \ldots as follows:

  • If an integer is divisible by 15, replace it with "FizzBuzz".
  • If an integer is divisible by 3 and has not been replaced yet, replace it with "Fizz".
  • If an integer is divisible by 5 and has not been replaced yet, replace it with "Buzz".
  • Otherwise, leave the integer unchanged.

The beginning of the sequence is as follows:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz ...

Let FF be the infinite string obtained by writing this sequence without spaces. Given a string LL, determine whether LL appears as a substring of FF. If it does, find the 1-based index of its first appearance.

Input

The first line contains one integer NN, the number of test cases (1≤N≤201 \le N \le 20). Each of the next NN lines contains one non-empty string LL made of digits and the letters "F", "B", "i", "u", and "z". The length of LL does not exceed 15.

Output

For each test case, print −1-1 if LL does not appear in FF. Otherwise, print the smallest 1-based index of the first element of LL in FF.

Examples1

  1. Example 1

    Input
    6
    1
    78Fizz
    68FizzBuzz71
    FizzBu
    uzzBuz
    987654321
    
    Expected output
    1
    16
    208
    18
    -1
    8358189296