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 cand = [] for i in range(len(s)-7, 3, -1): d = dist(s[i:i+7], "problem") cand.append([i,d]) d = min_dist min_dist = len(s) for i in cand: for j in range(i[0]-4+1): min_dist = min(min_dist, i[1] + dist(s[j:j+4], "good")) return 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)