#include #include #include using namespace std; bool judge(string); int main(int argc, char *argv[]) { int T; string pattern; cin >> T; for (int i = 0; i < T; i++) { cin >> pattern; cout << (judge(pattern) ? "possible" : "impossible") << endl; } return 0; } bool judge(string s) { vector count(3); vector final(3); int len = s.length(); int ch; int r; for (int i = 0; i < len; i++) { ch = s[i]; switch (ch) { case 'W': r = 0; break; case 'G': r = 1; break; case 'R': r = 2; break; } if (count[1] > count[0]) return false; if (count[2] > count[1]) return false; count[r]++; final[r] = i; } if (count[2] == 0) return false; if (count[2] != count[1]) return false; if ((final[0] > final[1]) || (final[0] > final[2])) return false; return true; }