def f(X,Y): assert len(X)==len(Y) A=0 for x,y in zip(X,Y): if x!=y:A+=1 return A #================================================ import sys input=sys.stdin.readline T=int(input()) for _ in range(T): S=input()[:-1] N=len(S) X=float("inf") for i in range(N-3): A=S[i:i+4] U=f(A,"good") for j in range(i+4,N-6): B=S[j:j+7] X=min(X,U+f(B,"problem")) print(X)