310279: CF1809A. Garland

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A. Garlandtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You have a garland consisting of $4$ colored light bulbs, the color of the $i$-th light bulb is $s_i$.

Initially, all the light bulbs are turned off. Your task is to turn all the light bulbs on. You can perform the following operation any number of times: select a light bulb and switch its state (turn it on if it was off, and turn it off if it was on). The only restriction on the above operation is that you can apply the operation to a light bulb only if the previous operation was applied to a light bulb of a different color (the first operation can be applied to any light bulb).

Calculate the minimum number of operations to turn all the light bulbs on, or report that this is impossible.

Input

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The single line of each test case contains $s$ — a sequence of $4$ characters, where each character is a decimal digit. The $i$-th character denotes the color of the $i$-th light bulb.

Output

For each test case, print one integer — the minimum number of operations to turn all the light bulbs on. If it is impossible to turn all the bulbs on, print -1.

ExampleInput
3
9546
0000
3313
Output
4
-1
6
Note

In the first example, all the colors are different, so you can just turn all the bulbs on in $4$ operations.

In the second example, it is impossible to turn all the bulbs on, because after you switch one light bulb, it is impossible to turn the others on.

In the third example, you can proceed as follows: turn the first light bulb on, turn the third light bulb on, turn the fourth light bulb on, turn the third light bulb off, turn the second light bulb on, turn the third light bulb on.

Input

题意翻译

#### 题意 有 $4$ 只灯泡,第 $i$ 只灯泡的颜色为 $s_i$ 。 一开始,所有灯泡都是关着的,你需要把它们都打开。你可以进行数次操作,每次操作改变一个灯泡的状态,即打开原本关着的灯泡或关上原本亮着的灯泡。第一次操作可选择任何灯泡,此后每一次被操作的灯泡的颜色都不得与上次操作的灯泡颜色相同。现求最少需要的操作数,如果无法打开所有灯泡输出 $-1$ 。 #### 输入 输入有多组测试数据,第一行是数据组数 $T$ $(1\le T \le 10^4)$ ,此后每行每组数据都有 $4$ 个**不用空格隔开**的整数 $s_i$ $(0\le s_i \le 9)$ ,表示灯泡的颜色。 #### 输出 对于每组数据,如果不可能打开所有灯泡,输出 $-1$ ,否则输出打开所有灯泡最少需要的操作数目。

Output

题目大意:
你有一个由4个彩色灯泡组成的灯串,第i个灯泡的颜色是s_i。最初,所有的灯泡都是关闭的。你的任务是打开所有的灯泡。你可以执行以下操作任意次:选择一个灯泡并切换其状态(如果它是关闭的则打开它,如果它是打开的则关闭它)。上述操作的唯一限制是,你只能在上一操作应用于不同颜色的灯泡时应用该操作(第一次操作可以应用于任何灯泡)。计算打开所有灯泡所需的最小操作数,或者报告这是不可能的。

输入数据格式:
第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。
每个测试用例的单行包含s——一个由4个字符组成的序列,每个字符都是十进制数字。第i个字符表示第i个灯泡的颜色。

输出数据格式:
对于每个测试用例,打印一个整数——打开所有灯泡所需的最小操作数。如果无法打开所有灯泡,打印-1。

示例:
输入:
3
9546
0000
3313
输出:
4
-1
6

注意:
在第一个示例中,所有颜色都不同,因此你可以直接在4个操作中打开所有灯泡。
在第二个示例中,无法打开所有灯泡,因为你切换一个灯泡后,无法打开其他灯泡。
在第三个示例中,你可以按以下步骤操作:打开第一个灯泡,打开第三个灯泡,打开第四个灯泡,关闭第三个灯泡,打开第二个灯泡,打开第三个灯泡。题目大意: 你有一个由4个彩色灯泡组成的灯串,第i个灯泡的颜色是s_i。最初,所有的灯泡都是关闭的。你的任务是打开所有的灯泡。你可以执行以下操作任意次:选择一个灯泡并切换其状态(如果它是关闭的则打开它,如果它是打开的则关闭它)。上述操作的唯一限制是,你只能在上一操作应用于不同颜色的灯泡时应用该操作(第一次操作可以应用于任何灯泡)。计算打开所有灯泡所需的最小操作数,或者报告这是不可能的。 输入数据格式: 第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。 每个测试用例的单行包含s——一个由4个字符组成的序列,每个字符都是十进制数字。第i个字符表示第i个灯泡的颜色。 输出数据格式: 对于每个测试用例,打印一个整数——打开所有灯泡所需的最小操作数。如果无法打开所有灯泡,打印-1。 示例: 输入: 3 9546 0000 3313 输出: 4 -1 6 注意: 在第一个示例中,所有颜色都不同,因此你可以直接在4个操作中打开所有灯泡。 在第二个示例中,无法打开所有灯泡,因为你切换一个灯泡后,无法打开其他灯泡。 在第三个示例中,你可以按以下步骤操作:打开第一个灯泡,打开第三个灯泡,打开第四个灯泡,关闭第三个灯泡,打开第二个灯泡,打开第三个灯泡。

加入题单

算法标签: