def is_possible(S): n = len(S) count_G = S.count('G') count_R = S.count('R') count_W = S.count('W') if count_G != count_R: return False if count_W < count_G: return False # Precompute next_G and next_R next_G = [None] * n next_R = [None] * n last_G = None last_R = None for i in range(n-1, -1, -1): if S[i] == 'G': last_G = i next_G[i] = last_G if S[i] == 'R': last_R = i next_R[i] = last_R # Check condition3: each G has R after it for i in range(n): if S[i] == 'G': if i+1 >= n: return False if next_R[i+1] is None: return False # Check condition4: each W has G and R after it for i in range(n): if S[i] == 'W': if i+1 >= n: return False g_pos = next_G[i+1] if g_pos is None: return False if g_pos + 1 >= n: return False r_pos = next_R[g_pos + 1] if r_pos is None: return False return True T = int(input()) for _ in range(T): S = input().strip() if is_possible(S): print("possible") else: print("impossible")