#include #include #include #include #include #include #include #include using namespace std; void possible() { cout << "possible" << endl; } void impossible() { cout << "impossible" << endl; } void resolve(const string& s) { vector index_list(s.length()); string::size_type indexR = string::npos; string::size_type indexG = string::npos; string::size_type indexW = string::npos; int countR = 0; int countG = 0; int countW = 0; for (string::size_type i = 0; i < s.length(); i++) { char ch = s[i]; switch (ch) { case 'R': countR++; index_list[i] = indexR; indexR = i; break; case 'G': countG++; index_list[i] = indexG; indexG = i; break; case 'W': countW++; index_list[i] = indexW; indexW = i; break; } } if (countR == 0 || countR != countG || countG > countW) { impossible(); return; } while (countR > 0) { if (countG > countW) { impossible(); return; } if (indexR < indexG || indexG < indexW) { impossible(); return; } indexR = index_list[indexR]; countR--; indexG = index_list[indexG]; countG--; do { if (indexW == string::npos) { impossible(); return; } indexW = index_list[indexW]; countW--; } while (indexW > indexG); } possible(); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; resolve(s); } return 0; }