Hide

Problem G
Virus Synthesis

Viruses are usually bad for your health. How about fighting them with…other viruses? In this problem, you need to find out how to synthesize such good viruses.

We have prepared for you a set of strings of the letters A, G, T and C. They correspond to the DNA nucleotide sequences of viruses that we want to synthesize, using the following operations:

  • Adding a nucleotide either to the beginning or the end of the existing sequence.

  • Replicating the sequence, reversing the copied piece, and gluing it either to the beginning or to the end of the original (so that e.g., AGTC can become AGTCCTGA or CTGAAGTC).

We’re concerned about efficiency, since we have very many such sequences, some of them very long. Find a way to synthesize them in a minimum number of operations.

Input

The first line of input contains the number of test cases $T$ ($1 \leq T \leq 200\, 000$). The descriptions of the test cases follow:

Each test case consists of a single line containing a non-empty string. The string uses only the capital letters $A$, $C$, $G$ and $T$ and is not longer than $100\, 000$ characters.

The total number of characters in all test cases is at most $2\, 600\, 000$.

Output

For each test case, output a single line containing the minimum total number of operations necessary to construct the given sequence.

Sample Input 1 Sample Output 1
4
AAAA
AGCTTGCA
AAGGGGAAGGGGAA
AAACAGTCCTGACAAAAAAAAAAAAC
3
8
6
18

Please log in to submit a solution to this problem

Log in