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 for tail in range(len1, n-len2 + 1): dp1[tail] = min(dp1[tail - 1], len([1 for c1, c2 in zip(string1, S[tail - len1:tail]) if c1 != c2])) if not dp1[tail]: break for head in range(n-len2, len1 - 1, -1): dp2[head] = min(dp2[head + 1], len([1 for c1, c2 in zip(string2, S[head:head + len2]) if c1 != c2])) return min(dp1[i] + dp2[i] for i in range(len1, n - len2 + 1)) T = int(input()) for t in range(T): print(solve(input(), 'good', 'problem'))