Under construction

Still being built. Things may change

Qizz Quzz (Easy)

Time limit1sMemory limit1024 MB

Summary
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 nn, a nonnegative integer kk, distinct positive integers d1,d2,...,dkd_1, d_2, ..., d_k with d1<d2<...<dkd_1 < d_2 < ... < d_k, and strings s1,...,sks_1, ..., s_k of lowercase English letters.

The number nn is the length of the output sequence. The number kk is the number of replacement rules. Each rule says: instead of multiples of did_i, print sis_i. If several rules apply, the label is the concatenation of the matching sis_i, ordered from the smallest to the largest divisor.

For example, the original Fizz Buzz has k=2k = 2, d=(3,5)d = (3, 5), and s=(fizz,buzz)s = (fizz, buzz). For n=17n = 17 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 sis_i cannot change.

Samko wrote his own implementation. It handles only inputs where k≤2k \le 2 and every string sis_i has length exactly 4.

Samko showed you a sequence of labels ℓ1,...,ℓn\ell_1, ..., \ell_n. Find the largest qq such that the prefix ℓ1,...,ℓq\ell_1, ..., \ell_q can be the output of Samko's program.

Input

The first line contains an integer tt, 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 n≤1000n \le 1000, the number of labels. The second line contains nn non-empty tokens ℓ1,...,ℓn\ell_1, ..., \ell_n separated by single spaces. Each token contains only digits and lowercase English letters.

Output

For each test case, output one line with the integer qq.

Hints

In the first example, the first six tokens are the output of a Generalized Fizz Buzz program with k=1k = 1, d1=1d_1 = 1, and s1=mlems_1 = mlem. 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, d=(1,2)d = (1, 2) and s=(haha,haha)s = (haha, haha). The strings sis_i 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.

Examples1

  1. Example 1

    Input
    5
    
    7
    mlem mlem mlem mlem mlem mlem fail
    
    7
    1 qizz quzz qizz 5 quzzqizz 007
    
    8
    1 2 3 4 hunter2 is my password
    
    2
    haha hahahaha
    
    9
    1 bat 3 batman robin batbat 7 batmanman 9
    
    Expected output
    6
    5
    4
    2
    1