#include #include #include #include #include #include #include #include #include // require sort next_permutation count __gcd reverse etc. #include // require abs exit atof atoi #include // require scanf printf #include #include // require accumulate #include // require fabs #include #include #include #include // require setw #include // require stringstream #include // require memset #include // require tolower, toupper #include // require freopen #include // 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 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 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; }