Qizz Quzz (Easy)
Time limit1sMemory limit1024 MB
Given label sequences, find the longest prefix that a Fizz Buzz variant with at most two rules, each string of length 4, could have produced.
- Level
Medium4 of 10
- Topics
- Implementation, Brute force, String
- Solved
- No attempts yet
Problem
Generalized Fizz Buzz works like the original Fizz Buzz. The program prints positive integers starting from 1, but some special numbers and their multiples are replaced by strings. If a number is divisible by several special numbers, it is replaced by the concatenation of all their strings.
Your task is not to implement Generalized Fizz Buzz. You are given a sequence and must decide whether it can be the output of a Generalized Fizz Buzz program.
A particular instance is defined by these parameters: a positive integer , a nonnegative integer , distinct positive integers with , and strings of lowercase English letters.
The number is the length of the output sequence. The number is the number of replacement rules. Each rule says: instead of multiples of , print . If several rules apply, the label is the concatenation of the matching , ordered from the smallest to the largest divisor.
For example, the original Fizz Buzz has , , and . For the output is 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17. Instead of 15, the program prints fizzbuzz because 15 is divisible by both 3 and 5. The label cannot be buzzfizz, because the order of the cannot change.
Samko wrote his own implementation. It handles only inputs where and every string has length exactly 4.
Samko showed you a sequence of labels . Find the largest such that the prefix can be the output of Samko's program.
Input
The first line contains an integer , the number of test cases. Each test case is preceded by a blank line. Each test case has two lines. The first line contains a positive integer , the number of labels. The second line contains non-empty tokens separated by single spaces. Each token contains only digits and lowercase English letters.
Output
For each test case, output one line with the integer .
Hints
In the first example, the first six tokens are the output of a Generalized Fizz Buzz program with , , and . Both Samko's and Monika's programs can handle this input, so the answer is 6 for each. In the second example, 2 must be replaced with qizz and 3 with quzz. The first five tokens match that sequence. The next token should be qizzquzz instead of quzzqizz, and the last one should be 7 instead of 007. In the third example, hunter2 is wrong because it contains both letters and digits. In the fourth example, and . The strings do not have to be distinct. In the fifth example, bat is already wrong because Samko's program cannot handle a string of length 3.