# your code goes here
import math
for _ in range(int(input())):
s=input()
c=0
x=10**9
n=len(s)
for i in range(0,10):
for j in range(0,10):
c=0
f=0
for k in range(0,n):
if s[k]==str(i):
f=1
if s[k]==str(j) and f==1 and i!=j:
f=0
c+=2
t=n-c
x=min(x,t)
for i in range(0,10):
c=0
for j in range(n):
if s[j]==str(i):
c+=1
x=min(x,(n-c))
print(x)
IyB5b3VyIGNvZGUgZ29lcyBoZXJlCmltcG9ydCBtYXRoCmZvciBfIGluIHJhbmdlKGludChpbnB1dCgpKSk6CiAgICBzPWlucHV0KCkKICAgIGM9MAogICAgeD0xMCoqOQogICAgbj1sZW4ocykKICAgIGZvciBpIGluIHJhbmdlKDAsMTApOgogICAgICAgIGZvciBqIGluIHJhbmdlKDAsMTApOgogICAgICAgICAgICBjPTAKICAgICAgICAgICAgZj0wCiAgICAgICAgICAgIGZvciBrIGluIHJhbmdlKDAsbik6CiAgICAgICAgICAgICAgICBpZiBzW2tdPT1zdHIoaSk6CiAgICAgICAgICAgICAgICAgICAgZj0xCiAgICAgICAgICAgICAgICBpZiBzW2tdPT1zdHIoaikgYW5kIGY9PTEgYW5kIGkhPWo6CiAgICAgICAgICAgICAgICAgICAgZj0wCiAgICAgICAgICAgICAgICAgICAgYys9MgogICAgICAgICAgICB0PW4tYwogICAgICAgICAgICB4PW1pbih4LHQpCiAgICBmb3IgaSBpbiByYW5nZSgwLDEwKToKICAgICAgICAgICAgIGM9MAogICAgICAgICAgICAgZm9yIGogaW4gcmFuZ2Uobik6CiAgICAgICAgICAgICAgICAgICAgICBpZiBzW2pdPT1zdHIoaSk6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGMrPTEKICAgICAgICAgICAgIHg9bWluKHgsKG4tYykpCiAgICBwcmludCh4KQ==