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') || s.count('W') < tmp puts "impossible" else flag = 0 s.delete!('W') g = [] r = [] (tmp * 2).times{|i| case s[i] when 'G' g.push(i) when 'R' r.push(i) end } tmp.times{|i| if g[i] > r[i] flag = 1 end } puts ["possible","impossible"][flag] end }