def dist(l,word): return sum([1 for p in zip(l, word) if p[0] != p[1]]) def solver(_s): global GL if len(_s) == GL: return dist(_s, "goodproblem") s = list(_s) min_dist = len(s) min_idx = len(s)-7 for i in range(len(s)-7, 3, -1): d = dist(s[i:i+7], "problem") if min_dist > d: min_dist = d min_idx = i d = min_dist min_dist = len(s) for i in range(min_idx-4+1): min_dist = min(min_dist, dist(s[i:i+4], "good")) return d + min_dist GL = len("goodproblem") T = int(input()) S = [input() for i in range(T)] ans = [solver(i) for i in S] for i in ans: print(i)