#include <iostream>
#include <string.h>
using namespace std;
struct tree
{
tree *next[26], *fail;
int flag, len, ;
};
tree *root, *p;
tree arr[500001];
int indexx;
char letter = 'a';
void newn()
{
arr[indexx].fail = NULL;
arr[indexx].flag = 0;
arr[indexx].len = -1;
for(int i = 0; i < 26; i ++) arr[indexx].next[i] = 0;
}
void insert(char ch[], int w)
{
p = root;
int tmp, i = 0;
while(ch[i])
{
tmp = ch[i] - letter;
if(p->next[tmp] == 0)
{
newn();
p->next[tmp] = &arr[indexx ++];
p->next[tmp]->len = p->len + 1;
}
p = p->next[tmp];
i ++;
}
if(!p->flag)
p->flag = w;
}
tree *que[500005];
void get_fail()
{
p=root;
p->fail = root;
int open = 0, close = -1, i;
que[0] = p;
while(close < open)
{
p = que[++ close];
for(i = 0; i < 26; i ++)
{
if(p->next[i] == 0)
{
if(p == root) p->next[i] = root;
else p->next[i] = p->fail->next[i];
}
else
{
if(p == root) p->next[i]->fail = root;
else p->next[i]->fail = p->fail->next[i];
que[++open] = p->next[i];
}
}
}
}
int ans[2][100005];
int pos[100005];
void query(char ch[])
{
p = root;
tree *q;
int t, i = 0;
while(ch[i])
{
t = ch[i] - letter;
p = p->next[t];
q = p;
while(q != root && q->fail )
{
if(q->flag)
{
ans[0][q->flag] ++;
if(i - pos[q->flag] > q->len)
{
ans[1][q->flag] ++;
pos[q->flag] = i;
}
}
q = q->fail;
}
i ++;
}
}
int search(char ch[])
{
p = root;
int tmp, i = 0;
while(ch[i])
{
tmp = ch[i] - letter;
p = p->next[tmp];
i ++;
}
return p->flag;
}
void init()
{
indexx = 0;
newn();
root = &arr[indexx ++];
memset(ans, 0, sizeof(ans));
memset(pos, -1, sizeof(pos));
}
char str[100005];
char dic[100005][7];
int typed[100005];
int main()
{
int test = 0;
while(scanf("%s", str) != EOF)
{
int n, i;
init();
scanf("%d", &n);
for(i = 1; i <= n; i ++)
{
scanf("%d %s", &typed[i], dic[i]);
insert(dic[i], i);
}
get_fail();
query(str);
printf("Case %d\n",++test);
for(i = 1; i <= n; i ++)
{
int id = search(dic[i]);
printf("%d\n", ans[typed[i]][id]);
}
puts("");
}
}