T = int(input()) for i in range(T): S = input() pre = [1 << 30] * 110 post = [1 << 30] * 110 for y in range(3, len(S)): cost = 0 if (S[y - 3] != 'g'): cost += 1 if (S[y - 2] != 'o'): cost += 1 if (S[y - 1] != 'o'): cost += 1 if (S[y] != 'd'): cost += 1 pre[y] = min(pre[y - 1], cost) for y in range(len(S) - 7, 0, -1): cost = 0 for index,s in enumerate("problem"): if (S[y + index] != s): cost += 1 post[y] = min(post[y + 1], cost) ans = 1 << 30 for i in range(101): ans = min(ans, pre[i] + post[i + 1]) print (ans)