import java.io.BufferedInputStream; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListSet; public class Main { public static final int C = 1000000007; static final int CY = 1000000000; //static boolean MAP[][]; static int T; static int N; static double P; //static String S; static int M; static int MAX = -1; //static int tic[][]; static String S[]; static int Y[]; static double W[]; //static int max; static long DP[]; static int ST[][]; //static int p[]; //static ArrayList> al; static ConcurrentSkipListSet TS; static ArrayList AL; //static int a[][]; //static char[][] ch; //static ArrayList> al; //static String a[]; //static String str; public static void main(String[] args) { StringBuilder sb = new StringBuilder(); BufferedInputStream bs = new BufferedInputStream(System.in); Scanner sc = new Scanner(bs); T = sc.nextInt(); S = new String[T]; for (int i=0; i < T; i++) { S[i] = sc.next(); } long[] ans = new long[T]; for (int t=0; t < T; t++) { int[] good = new int[S[t].length()]; int[] problem = new int[S[t].length()]; for (int i=0; i < S[t].length(); i++) { if (S[t].length() - i < 11) { good[i] = -1; }else { int num = 0; if (S[t].charAt(i) == 'g') { num++; } if (S[t].charAt(i+1) == 'o') { num++; } if (S[t].charAt(i+2) == 'o') { num++; } if (S[t].charAt(i+3) == 'd') { num++; } good[i] = num; } if (i < 4|| S[t].length() - i < 7) { problem[i] = -1; }else { int num = 0; if (S[t].charAt(i) == 'p') { num++; } if (S[t].charAt(i+1) == 'r') { num++; } if (S[t].charAt(i+2) == 'o') { num++; } if (S[t].charAt(i+3) == 'b') { num++; } if (S[t].charAt(i+4) == 'l') { num++; } if (S[t].charAt(i+5) == 'e') { num++; } if (S[t].charAt(i+6) == 'm') { num++; } problem[i] = num; } } for (int i=0; i < S[t].length(); i++) { System.out.print(good[i] + ", "); } System.out.println(); for (int i=0; i < S[t].length(); i++) { System.out.print(problem[i] + ", "); } System.out.println(); int min = 10000; for (int i=0; i < S[t].length(); i++) { if (good[i] == -1)break; int num = 4 - good[i]; int max = 0; for (int v=i+4; v < S[t].length(); v++) { if (problem[v] == -1)continue; if (max < problem[v]) { max = problem[v]; } } num += 7 - max; if (min > num) { min = num; } } ans[t] = min; } for (int i=0; i < T; i++) { sb.append(ans[i] + "\n"); } System.out.print(sb); } }