#B3891. Stable String Pairing

    ID: 11548 Type: Default 1000ms 256MiB

Stable String Pairing

Stable String Pairing

Given a string \( S \) of length \( n \) consisting only of uppercase letters, we want to determine its stability when paired with its reverse.

Define \( S' \) as the reverse of \( S \). For \( 1 \leq i \leq n \), the \( i \)th character of \( S \) is paired with the \( i \)th character of \( S' \).

The pairing rules are as follows:

  • \( A \) pairs with \( T \) and vice versa.
  • \( C \) pairs with \( G \) and vice versa.

If \( S_i \) and \( S'_i \) form a valid pair, the stability increases by \( i \) (i.e. the position index, using 1-indexing).

If any character in \( S \) (and hence in \( S' \)) is not one of \( A, T, C, G \), the entire string's stability is defined to be \( 0 \).

You are given \( T \) test cases. For each string \( S \), compute its stability after pairing with its reverse \( S' \).

inputFormat

The input begins with an integer ( T ) on the first line, which denotes the number of test cases. Each of the following ( T ) lines contains a string ( S ) consisting solely of uppercase letters.

outputFormat

For each test case, output a single integer that represents the stability of the string after pairing.

sample

1
ATCGT
6