def solve(S, string1, string2): n = len(S) len1 = len(string1) len2 = len(string2) dp1 = [0] * n # good dp2 = [0] * n # problem dp1[len1-1] = len1 dp2[n-len2+1] = len2 mindist = len1 for tail in range(len1, n-len2 + 1): dist = len([1 for c1, c2 in zip(string1, S[tail - len1:tail]) if c1 != c2]) if dist < mindist: mindist = dist dp1[tail] = mindist if not mindist: break mindist = len2 for head in range(n-len2, len1 - 1, -1): dist = len([1 for c1, c2 in zip(string2, S[head:head + len2]) if c1 != c2]) if dist < mindist: mindist = dist dp2[head] = mindist if not mindist: break dp3 = [0] * n # number of string2 in S[:i-len1] count = 0 for i in range(len1 + len2, n - len2 + 1): if string2 == S[i-len1-len2:i-len1]: count += 1 dp3[i] = count return min(dp1[i] + dp2[i] + dp3[i] for i in range(len1, n - len2 + 1)) T = int(input()) for t in range(T): print(solve(input(), 'good', 'problem'))