import sys from bisect import bisect_left def solve(): input = sys.stdin.read().split() T = int(input[0]) cases = input[1:T+1] for S in cases: r_count = S.count('R') g_count = S.count('G') w_count = S.count('W') # Check if number of R and G are equal if r_count != g_count: print("impossible") continue # Check if there are enough W for each pair if w_count < r_count: print("impossible") continue # Collect positions of G and R g_positions = [] for i, c in enumerate(S): if c == 'G': g_positions.append(i) r_positions = [i for i, c in enumerate(S) if c == 'R'] # Check if each R can be paired with a G that comes before it possible = True used = [False] * len(g_positions) # Process R in reverse order (from last to first) for r in reversed(r_positions): # Find the largest G position less than r idx = bisect_left(g_positions, r) - 1 # Find the first unused G before r while idx >= 0 and used[idx]: idx -= 1 if idx < 0: possible = False break used[idx] = True print("possible" if possible else "impossible") if __name__ == "__main__": solve()