from sys import stdin
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
s=input()
if len(s.replace('A','').replace('T','').replace('G','').replace('C',''))!=0:
print(0)
continue
s1=s[::-1]
cnt=0
for i in range(n):
if (s[i]=='A' and s1[i]=='T') or (s[i]=='T' and s1[i]=='A') or (s[i]=='G' and s1[i]=='C') or (s[i]=='C' and s1[i]=='G'):
cnt+=(i+1)
print(cnt)