#include
using namespace std;
#define _l (long long int)
struct node { int le, sz; node *tr[27], *pt; node() { memset(tr, 0, sizeof(tr)); pt = 0; sz = 0; } };
const int maxn = 1200009; const int maxl = 4000000;
char g[maxl]; int n, mask; node nbuf_arr[maxn], *nbuf(nbuf_arr), *srt, *scur;
void decodeStr(char* a, int mask) { int l(strlen(a)); for (int i = 0; i < l; ++ i) { mask = (_l mask * 131 + i) % l; swap(a[i], a[mask]); } }
void samIns(int w) { node *np(nbuf ++), *p; np-> le = scur-> le + 1; for (p = scur; p && !p-> tr[w]; p = p-> pt) p-> tr[w] = np; if (p) { node q(p-> tr[w]); if (q-> le != p-> le + 1) { node nq(nbuf ++); memcpy(nq, q, sizeof(node)); nq-> le = p-> le + 1; q-> pt = np-> pt = nq; for (; p && p-> tr[w] == q; p = p-> pt) p-> tr[w] = nq; } else np-> pt = q; } else np-> pt = srt; scur = np; for (p = scur; p; p = p-> pt) ++ p-> sz; }
int samQry(char* it) { node* p; for (p = srt; *it; ++ it) { int d(*it - 65); if (p-> tr[d]) p = p-> tr[d]; else return 0; } return p-> sz; }
int main() { #ifndef ONLINE_JUDGE freopen(".in", “r”, stdin); #endif
scanf("%d%s", &n, g);
mask = 0;
srt = scur = nbuf ++;
scur-> le = 0;
for (char* i = g; *i; ++ i)
samIns(*i - 65);
while (n --) {
char opt[7];
scanf("%s%s", opt, g);
decodeStr(g, mask);
if (opt[0] == 'A') {
for (char* i = g; *i; ++ i)
samIns(*i - 65);
}
else {
int ans(samQry(g));
mask ^= ans;
printf("%d\n", ans);
}
}
}