def isinvalid (str) # bus = 0 # while str =~ /[GR]/ # isvalid = str =~ /^(W*?)WG([WG]*?)R(.*)$/ # return true unless isvalid # str = "#{$1}#{$2}#{$3}" # end # false # # bus = {} bus['W'] = bus['G'] = bus['R'] = 0 str = str.split('') for c in str do bus[c] += 1 return true if bus['W'] < bus['G'] || bus['G'] < bus['R'] end bus['G'] != bus['R'] end def main () flandre = gets.chomp.to_i(10) for i in 0...flandre do str = gets.chomp x = "" x << "im" if isinvalid(str) puts x << "possible" end end main()