#include<iostream>
#include<algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
int m;
cin >> s >> m;
while (m--) {
char op;
cin >> op;
switch (op) {
case 'A': {
char c;
cin >> c;
s.push_back(c);
break;
}
case 'I': {
int index;
cin >> index;
char c;
cin >> c;
s.insert(s.begin() + index - 1, c);
break;
}
case 'Q':
char c;
cin >> c;
cout << count(s.begin(), s.end(), c) << endl;
break;
case 'P':
for_each(s.begin(), s.end(), [](const auto &c) { cout << c << ' '; });
cout << endl;
break;
default:
break;
}
}
return 0;
}