def haming(str1, str2): return len([1 for c1, c2 in zip(str1, str2) if c1 != c2]) def solve(S, string1, string2): n = len(S) len1 = len(string1) len2 = len(string2) dp1 = [len1] * n # good dp2 = [len2] * n # problem for tail in range(len1, n-len2 + 1): dp1[tail] = min(dp1[tail - 1], haming(string1, S[tail - len1:tail])) for head in range(n-len2, len1 - 1, -1): dp2[head] = min(dp2[head + 1], haming(string2, S[head:head + len2])) return min(dp1[i] + dp2[i] for i in range(len1, n - len2 + 1)) T = int(input()) for t in range(T): S = input() print(solve(S, 'good', 'problem'))