def hd(x,y): cnt = 0 for i in range(len(x)): if x[i]!=y[i]: cnt += 1 return cnt T = int(input()) for _ in range(T): S = input().strip() dmin = 10**6 ans = 10**6 for i in range(len(S)-11,-1,-1): d = hd(S[i:i+4],"good") dmin = min(dmin,hd(S[i+4:i+11],"problem")) ans = min(ans,d+dmin) print(ans)