From Codechef:
A string is considered balanced if and only if all the characters occur in it equal number of times.
You are given a strin
if __name__ == "__main__":
for j in range(int(input())):
S = str(input())
N = len(S)
A = [0]*27
for c in S:
A[ord(c) - 65] = A[ord(c) - 65] + 1
A = sorted(A,reverse=True)
minSwap = N + 1
for i in range(1,27):
if N%i == 0:
temp = N//i
tempSwap = 0
for f in range(i):
if temp > A[f]:
tempSwap = tempSwap + temp - A[f]
if tempSwap <= minSwap:
minSwap = tempSwap
if minSwap == N+1:
minSwap = 0
print(minSwap)