import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt(); int []ans = new int[T]; char []c1 = {'g', 'o', 'o', 'd'}; char []c2 = {'p', 'r', 'o', 'b', 'l', 'e', 'm'}; int min1 = 4; int min2 = 6; for(int i = 0; i < T; i++) { String s = scan.next(); int index1 = s.indexOf("good"); int index2 = s.indexOf("problem"); if(index1 != -1 && index1 < index2) { ans[i] = 0; }else { int []cost1 = new int[s.length()]; int []cost2 = new int[s.length()]; int min = 7; //System.out.println(s.length()); for(int j = s.length() - 7; j >= 4; j--) { int cnt2 = 0; for(int k = 0; k <= 6; k++) { char t1 = s.charAt(j + k); if(t1 != c2[k]) { cnt2++; } } if(min > cnt2) { min = cnt2; } //System.out.println(min); cost2[j] = min; } for(int j = 0; j < s.length() - 10; j++) { int cnt1 = 0; for(int k = 0; k < 4; k++) { char t1 = s.charAt(j + k); if(t1 != c1[k]) { cnt1++; } } cost1[j] = cnt1; } int cost = 11; for(int j = 0; j < s.length() - 10; j++) { int t = cost1[j] + cost2[j + 4]; if(t < cost) { cost = t; } } ans[i] = cost; } } for(int i = 0; i < T; i++) { System.out.println(ans[i]); } } }