T = int(input()) for _ in range(T): s = input().strip() possible = True # Check first character is not R if s[0] == 'R': print("impossible") continue # Check last character is not G if s[-1] == 'G': print("impossible") continue # Precompute has_w_before array n = len(s) has_w_before = [False] * (n + 1) for i in range(n): has_w_before[i+1] = has_w_before[i] or (s[i] == 'W') # Check each G has at least one W before it for i in range(n): if s[i] == 'G' and not has_w_before[i]: possible = False break if not possible: print("impossible") continue # Check count of G and R are equal count_g = s.count('G') count_r = s.count('R') if count_g != count_r: print("impossible") continue # Check balance during processing balance = 0 for c in s: if c == 'G': balance += 1 elif c == 'R': balance -= 1 if balance < 0: possible = False break if possible and balance == 0: print("possible") else: print("impossible")