local mmi, mma = math.min, math.max local good = "good" local problem = "problem" local g = {} for i = 1, 4 do g[i] = good:sub(i, i) end local p = {} for i = 1, 7 do p[i] = problem:sub(i, i) end local function solve() local s = io.read() local n = #s local t = {} for i = 1, n - 6 do t[i] = 0 end t[n - 5] = 1000000007 for i = n - 6, 1, -1 do local c = 0 for j = 0, 6 do if p[1 + j] ~= s:sub(i + j, i + j) then c = c + 1 end end t[i] = mmi(c, t[i + 1]) end local ans = 1000000007 local u = 1000000007 local bias = 0 for i = 1, n - 10 do if 7 < i then local f = true for j = i - 7, i - 1 do if p[j - i + 8] ~= s:sub(j, j) then f = false break end end if f then bias = bias + 1 end end local c = bias for j = 0, 3 do if g[1 + j] ~= s:sub(i + j, i + j) then c = c + 1 end end u = mmi(u, c) ans = mmi(ans, u + t[i + 4]) end print(ans) end local q = io.read("*n", "*l") for iq = 1, q do solve() end