#include <iostream> #include <vector> #include <string> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> // require sort next_permutation count __gcd reverse etc. #include <cstdlib> // require abs exit atof atoi #include <cstdio> // require scanf printf #include <functional> #include <numeric> // require accumulate #include <cmath> // require fabs #include <climits> #include <limits> #include <cfloat> #include <iomanip> // require setw #include <sstream> // require stringstream #include <cstring> // require memset #include <cctype> // require tolower, toupper #include <fstream> // require freopen #include <ctime> // require srand #define rep(i,n) for(int i=0;i<(n);i++) #define ALL(A) A.begin(), A.end() #define INF 1<<10 using namespace std; typedef long long ll; typedef pair<int, int> P; int first_char (string S, char c ){ for (int i = 0; i < S.length(); i++ ){ if (S[i] == c ) return i; } // end for return INF; } int last_char (string S, char c ){ for (int i = S.length() - 1; i >= 0; i-- ){ if (S[i] == c ) return i; } // end for return -1; } bool is_order (string S, char c1, char c2, bool flag ){ stack<char> st; rep (i, S.length() ){ if (S[i] == c1 ) st.push (S[i] ); else if (S[i] == c2 ){ if (st.empty() ){ return false; }else{ st.pop(); } // end if } // end if } // end rep if (flag && !st.empty() ) return false;; return true; } int main() { ios_base::sync_with_stdio(0); int T; cin >> T; while (T-- ){ string S = ""; cin >> S; int w = (int)count (ALL (S ), 'W' ); int g = (int)count (ALL (S ), 'G' ); int r = (int)count (ALL (S ), 'R' ); bool ok = true; ok &= (S[0] == 'W' ); ok &= (S[S.length()-1] == 'R' ); ok &= (w > 0 && g > 0 && r > 0 && g == r && w >= g ); ok &= is_order (S, 'W', 'G', false ); ok &= is_order (S, 'G', 'R', true ); int firstW = first_char (S, 'W' ); int firstG = first_char (S, 'G' ); int firstR = first_char (S, 'R' ); ok &= (firstW < firstG && firstG < firstR ); int lastW = last_char(S, 'W' ); int lastG = last_char(S, 'G' ); int lastR = last_char(S, 'R' ); ok &= (lastW < lastG && lastG < lastR ); cout << (ok ? "possible" : "impossible" ) << endl; } // end while return 0; }