def solve(): import sys input = sys.stdin.read data = input().split() T = int(data[0]) cases = data[1:T+1] for S in cases: # Step 1: Check if number of Gs equals number of Rs g_positions = [] r_positions = [] for i, c in enumerate(S): if c == 'G': g_positions.append(i) elif c == 'R': r_positions.append(i) if len(g_positions) != len(r_positions): print("impossible") continue # Step 2: Check each R has at least one G before it valid = True for r in r_positions: has_g = False for g in g_positions: if g < r: has_g = True break if not has_g: print("impossible") valid = False break if not valid: continue # Step 3: Check that at any point, number of Gs <= Ws w_count = 0 g_count = 0 valid_step3 = True for c in S: if c == 'W': w_count += 1 elif c == 'G': g_count += 1 if g_count > w_count: valid_step3 = False break if not valid_step3: print("impossible") continue # Step 4: Pair each R with a G using greedy approach # Sort positions g_sorted = sorted(g_positions) r_sorted = sorted(r_positions) g_ptr = len(g_sorted) - 1 possible = True for r in reversed(r_sorted): # Find the latest G that is before r while g_ptr >= 0 and g_sorted[g_ptr] >= r: g_ptr -= 1 if g_ptr < 0: possible = False break g_ptr -= 1 # Mark this G as used if possible: print("possible") else: print("impossible") if __name__ == "__main__": solve()