#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
char A[MX], B[MX];
int n, q, p[MX], r[MX], sz[MX], ans[30], query[MX][2], res[MX];
void init() {
for(int i = 0; i < MX; p[i] = i, r[i] = 0, sz[i] = 1, i++);
memset(ans, 0, sizeof(ans));
}
int find_par(int x) {
return (p[x] == x) ? x : p[x] = find_par(p[x]);
}
int union_set(int i, int j) {
int x = find_par(i), y = find_par(j);
if(x != y) {
if(r[x] < r[y])
swap(x, y);
p[y] = x;
sz[x] += sz[y];
if(r[x] == r[y]) r[x]++;
return sz[x];
}
return 0;
}
int main() {
int t;
char ch;
scanf("%d", &t);
for(int i = 1; i <= t; i++) {
scanf("%c", &ch);
scanf("%s %d", A, &q);
strcpy(B, A);
for(int j = 0; j < q; j++) {
scanf("%d %d", &query[j][0], &query[j][1]);
if(query[j][0] == 2) A[query[j][1]] = '#';
}
int m = strlen(A); init();
for(int j = 0; j < m; j++) {
if(A[j] == '#') continue;
for(; (j < m) && (A[j] == A[j + 1]); j++) {
int val = union_set(j, j + 1);
ans[A[j] - 'A'] = max(val, ans[A[j] - 'A']);
}
ans[A[j] - 'A'] = max(1, ans[A[j] - 'A']);
}
int id = 0;
for(int j = q - 1; j >= 0; j--) {
if(query[j][0] == 1) {
res[id++] = ans[B[query[j][1]] - 'A'];
} else {
A[query[j][1]] = B[query[j][1]];
if(query[j][1] - 1 >= 0) {
if(A[query[j][1]] == A[query[j][1] - 1]) {
int val = union_set(query[j][1], query[j][1] - 1);
ans[A[query[j][1]] - 'A'] = max(val, ans[A[query[j][1]] - 'A']);
}
}
if(query[j][1] + 1 < m) {
if(A[query[j][1]] == A[query[j][1] + 1]) {
int val = union_set(query[j][1], query[j][1] + 1);
ans[A[query[j][1]] - 'A'] = max(val, ans[A[query[j][1]] - 'A']);
}
}
ans[A[query[j][1]] - 'A'] = max(1, ans[A[query[j][1]] - 'A']);
}
}
printf("Case %d:\n", i);
for(int j = id - 1; j >= 0; j--) {
printf("%d\n", res[j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