def min a, b return a < b ? a : b end t = readline().to_i for u in 1..t do s = readline() prevprob = Array.new(s.length, 0) for i in 6..(s.length-1) do flg = true for j in 0..6 do if s[i - 6 + j] != 'problem'[j] flg = false break end end prevprob[i] = prevprob[i - 1] prevprob[i] += 1 if flg end prob = Array.new(s.length, 100) for i in 0..(s.length-8) do prob[i] = 0 for j in 0..6 do prob[i] += 1 if s[i + j] != 'problem'[j] end end for i in 2..(s.length) do prob[s.length - i] = min prob[s.length - i + 1], prob[s.length - i] end ans = 100 for i in 0..(s.length - 11) do sub = prob[i + 4] for j in 0..3 do sub += 1 if s[i + j] != 'good'[j] end sub += prevprob[i - 1] if i > 0 ans = min ans, sub end puts ans end