#include <bits/stdc++.h>
#define N 100000
using namespace std;
int t, a;
vector<string> robot;
string tans;
int solve(int idx, string ans, set<int> skip) {
set<char> ch;
//tried all idx > 500 , < 500 , > 1000 etc
if (idx >= 500) {
return 0;
}
for (int i = 0; i < a; i++) {
if(skip.find(i) != skip.end())
continue;
if (idx < robot[i].length()) {
ch.insert(robot[i][idx]);
} else {
ch.insert(robot[i][idx % robot[i].length()]);
}
}
//also tried ch.size() == 0 return 0
if (ch.size() == 0)
return 1;
if (ch.size() == 3)
return 0;
if (ch.size() == 1) {
switch (*ch.begin()) {
case 'R':
ans += "P";
break;
case 'P':
ans += "S";
break;
case 'S':
ans += "R";
}
// cout << "ans:" << ans << "\n";
tans = ans;
return 1;
}
char curr[501];
int i = 0;
for (auto xx : ch) {
curr[i++] = xx;
}
string cur(curr);
if(cur.compare("PR") == 0){
for (int i = 0; i < a; i++) {
if (robot[i][idx % robot[i].length()] == 'R') {
skip.insert(i);
}
}
return solve(idx + 1, ans + "P", skip);
}
if (cur.compare("RS") == 0){
for (int i = 0; i < a; i++) {
if (robot[i][idx % robot[i].length()] == 'S') {
skip.insert(i);
}
}
return solve(idx + 1, ans + "R", skip);
}
if (cur.compare("PS") == 0){
for (int i = 0; i < a; i++) {
if (robot[i][idx % robot[i].length()] == 'P') {
skip.insert(i);
}
}
return solve(idx + 1, ans + "S", skip);
}
return 0;
}
int main() {
// your code goes here
int tc = 1;
string s;
string ans;
cin >> t;
while (t--) {
cin >> a;
tans = ans = "";
for (int i = 0; i < a; i++) {
cin >> s;
robot.push_back(s);
}
set<int> st;
int x = solve(0, ans, st);
if (x) {
cout << "Case #" << tc++ << ": " << tans << "\n";
} else {
cout << "Case #" << tc++ << ": IMPOSSIBLE"
<< "\n";
}
robot.clear();
st.clear();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIE4gMTAwMDAwCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHQsIGE7CnZlY3Rv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gICBjdXJyW2krK10gPSB4eDsKICB9CgogIHN0cmluZyBjdXIoY3Vycik7CgogIGlmKGN1ci5jb21wYXJlKCJQUiIpID09IDApewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBhOyBpKyspIHsKCiAgICAgIGlmIChyb2JvdFtpXVtpZHggJSByb2JvdFtpXS5sZW5ndGgoKV0gPT0gJ1InKSB7CiAgICAgICAgc2tpcC5pbnNlcnQoaSk7CiAgICAgIH0KICAgIH0KICAgIHJldHVybiBzb2x2ZShpZHggKyAxLCBhbnMgKyAiUCIsIHNraXApOwogIH0KICAgIAoKICBpZiAoY3VyLmNvbXBhcmUoIlJTIikgPT0gMCl7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGE7IGkrKykgewoKICAgICAgaWYgKHJvYm90W2ldW2lkeCAlIHJvYm90W2ldLmxlbmd0aCgpXSA9PSAnUycpIHsKICAgICAgICBza2lwLmluc2VydChpKTsKICAgICAgfQogICAgfQogICAgcmV0dXJuIHNvbHZlKGlkeCArIDEsIGFucyArICJSIiwgc2tpcCk7CiAgfQogICAgCgogIGlmIChjdXIuY29tcGFyZSgiUFMiKSA9PSAwKXsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgYTsgaSsrKSB7CgogICAgICBpZiAocm9ib3RbaV1baWR4ICUgcm9ib3RbaV0ubGVuZ3RoKCldID09ICdQJykgewogICAgICAgIHNraXAuaW5zZXJ0KGkpOwogICAgICB9CiAgICB9CiAgICByZXR1cm4gc29sdmUoaWR4ICsgMSwgYW5zICsgIlMiLCBza2lwKTsKICB9CgogICAgcmV0dXJuIDA7Cn0KCmludCBtYWluKCkgewogIC8vIHlvdXIgY29kZSBnb2VzIGhlcmUKICBpbnQgdGMgPSAxOwogIHN0cmluZyBzOwogIHN0cmluZyBhbnM7CgogIGNpbiA+PiB0OwoKICB3aGlsZSAodC0tKSB7CiAgICBjaW4gPj4gYTsKICAgIHRhbnMgPSBhbnMgPSAiIjsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgYTsgaSsrKSB7CiAgICAgIGNpbiA+PiBzOwogICAgICByb2JvdC5wdXNoX2JhY2socyk7CiAgICB9CiAgICBzZXQ8aW50PiBzdDsKICAgIGludCB4ID0gc29sdmUoMCwgYW5zLCBzdCk7CgogICAgaWYgKHgpIHsKICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCB0YysrIDw8ICI6ICIgPDwgdGFucyA8PCAiXG4iOwogICAgfSBlbHNlIHsKICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCB0YysrIDw8ICI6IElNUE9TU0lCTEUiCiAgICAgICAgICAgPDwgIlxuIjsKICAgIH0KICAgIHJvYm90LmNsZWFyKCk7CiAgICBzdC5jbGVhcigpOwogIH0KCiAgcmV0dXJuIDA7Cn0K