def solve(s) last_w = 0 last_g = 0 counter = {W: 0, G: 0} s.each_char.with_index do |c, i| if c == 'W' last_w = i counter[:W] += 1 elsif c == 'G' if counter[:W] == 0 return false else last_g = i counter[:W] -= 1 counter[:G] += 1 end else if counter[:G] == 0 return false else counter[:G] -= 1 end end end if last_w > last_g or counter[:G] != 0 or s[-1] != 'R' return false end return true end n = gets.to_i n.times do puts solve(gets.chomp) ? 'possible' : 'impossible' end