import java.util.*; public class Main { static final char[] GOOD = "good".toCharArray(); static final char[] PROBLEM = "problem".toCharArray(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int k = 0; k < t; k++) { char[] arr = sc.next().toCharArray(); int length = arr.length; int size = length - 11 + 1; int[] gArr = new int[size]; int[] pArr = new int[size]; for (int i = 0; i < size; i++) { for (int j = 0; j < GOOD.length; j++) { if (arr[i + j] != GOOD[j]) { gArr[i]++; } } if (i != 0) { gArr[i] = Math.min(gArr[i - 1], gArr[i]); } } for (int i = size - 1; i >= 0; i--) { for (int j = 0; j < PROBLEM.length; j++) { if (arr[i + GOOD.length + j] != PROBLEM[j]) { pArr[i]++; } } if (i != size - 1) { pArr[i] = Math.min(pArr[i + 1], pArr[i]); } } int min = Integer.MAX_VALUE; for (int i = 0; i < size; i++) { min = Math.min(min, gArr[i] + pArr[i]); } sb.append(min).append("\n"); } System.out.print(sb); } }