Under construction

Still being built. Things may change

Hair Tying

Time limit1sMemory limit1024 MB

Summary
Given values 0 to 3, pick subarrays and overwrite each with its XOR so no 3 remains, then print the minimum number of operations or -1.
Level

Medium6 of 10

Topics
Array, Prefix sum, Dynamic programming
Solved
No attempts yet

Problem

Sihyeon likes changing and tying up people's hairstyles. A hairstyle is one of 00 (straight hair), 11 (ponytail), 22 (pigtails), or 33 (three-strand braid).

Given hairstyles A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, Sihyeon can pick ll and rr and set the hairstyles of people ll through rr all at once to Al⊕Al+1⊕⋯⊕ArA_l \oplus A_{l+1} \oplus \cdots \oplus A_r. ⊕\oplus is the bitwise XOR operator.

Sihyeon dislikes the oddly shaped three-strand braid, so Sihyeon wants to make sure nobody has a three-strand braid. Find the minimum number of hairstyle changes.

Input

The input has several test cases. The first line contains the number of test cases TT. (1≤T≤20 0001 \le T \le 20\,000)

The first line of each test case gives NN. (1≤N≤1061 \leq N \leq 10^6)

The second line gives the NN hairstyles A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces. (0≤Ai≤30 \leq A_i \leq 3)

The sum of NN over all test cases does not exceed 10610^6.

Output

For each test case, print on one line the minimum number of hairstyle changes needed so that no three-strand braid remains. If a three-strand braid remains no matter how you change the hairstyles, print −1-1.

Examples1

  1. Example 1

    Input
    5
    4
    3 0 1 2
    4
    1 3 3 3
    4
    3 1 2 3
    3
    3 3 3
    1
    3
    
    Expected output
    1
    1
    2
    3
    -1