gets.to_i.times{ s = gets.chomp s.reverse =~ /R(.*?)G/ tmp = s.count('G') if s[-1] != 'R' || s[0] != 'W' || $1.to_s.include?('W') || tmp != s.count('R') puts "impossible" else flag = 0 w = [] g = [] r = [] s.length.times{|i| case s[i] when 'W' w.push(i) when 'G' g.push(i) when 'R' r.push(i) end } tmp.times{|i| if g[i] > r[i] || w[i] > g[i] flag = 1 end } puts ["possible","impossible"][flag] end }