import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); char[] GOOD = "good".toCharArray(); char[] PROBLEM = "problem".toCharArray(); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (n-- > 0) { char[] values = sc.next().toCharArray(); int[] goods = getDPLeft(GOOD, values); int[] problems = getDPRight(PROBLEM, values); int min = Integer.MAX_VALUE; for (int i = 0; i + 4 < values.length; i++) { min = Math.min(min, goods[i] + problems[i + 4]); } sb.append(min).append("\n"); } System.out.print(sb); } static int[] getDPLeft(char[] target, char[] base) { int[] dp = getDP(target, base); for (int i = 1; i < dp.length; i++) { dp[i] = Math.min(dp[i], dp[i - 1]); } return dp; } static int[] getDPRight(char[] target, char[] base) { int[] dp = getDP(target, base); for (int i = dp.length - 1; i > 0; i--) { dp[i - 1] = Math.min(dp[i - 1], dp[i]); } return dp; } static int[] getDP(char[] target, char[] base) { int[] dp = new int[base.length]; Arrays.fill(dp, Integer.MAX_VALUE / 2); for (int i = 0; i + target.length <= base.length; i++) { dp[i] = 0; for (int j = 0; j < target.length; j++) { if (target[j] != base[i + j]) { dp[i]++; } } } return dp; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }