T = int(input()) S = [input() for _ in range(T)] for s in S: cc = len(s) g = [] p = [] for gi in range(0, cc-3): gcount = 0 for j, k in zip(s[gi:gi+4], 'good'): if j != k: gcount += 1 g.append([gcount, gi+3]) for pi in range(0, cc-6): pcount = 0 for n, m in zip(s[pi:pi+7], 'problem'): if n != m: pcount += 1 p.append([pcount, pi]) ans = set() for cg in g: for cp in p: if cg[1] < cp[1]: ans.add(cg[0] + cp[0]) print(min(ans))