#include<cstdio>
#include<cstring>
#include<cstdlib>
int i,n,b[101];
char s[1000005],a[101][10005];
typedef struct
{char *astring,*stringy;
int length,number;}AC_PATTERN_t;
typedef struct
{AC_PATTERN_t *patterns;
int position,match_num;}AC_MATCH_t;
typedef int (*MATCH_CALBACK_f)(AC_MATCH_t*);
struct edge
{char alpha;
struct node *next;};
typedef struct node
{int final,depth;
struct node *failure_node;
AC_PATTERN_t *matched_patterns;
int matched_patterns_num,matched_patterns_max,outgoing_degree,outgoing_max;
struct edge * outgoing;}AC_NODE_t;
typedef struct
{AC_NODE_t *root,**all_nodes,*current_node;
int all_nodes_num,all_nodes_max,automata_open,base_position,total_patterns;
AC_MATCH_t match;
MATCH_CALBACK_f match_callback;}AC_AUTOMATA_t;
void node_init(AC_NODE_t *thiz)
{memset(thiz,0,sizeof(AC_NODE_t));
thiz->outgoing_max=thiz->matched_patterns_max=8;
thiz->outgoing=new edge[8];
thiz->matched_patterns=new AC_PATTERN_t[8];}
struct node *node_create()
{AC_NODE_t *thiz;
thiz=new AC_NODE_t;
node_init(thiz);
return thiz;}
void node_release(AC_NODE_t *thiz)
{free(thiz->matched_patterns);
free(thiz->outgoing);
free(thiz);}
AC_NODE_t *node_find_next(AC_NODE_t * thiz,char alpha)
{int i;
for(i=0; i < thiz->outgoing_degree; i++)
if(thiz->outgoing[i].alpha == alpha)
return (thiz->outgoing[i].next);
return NULL;}
AC_NODE_t * node_findbs_next(AC_NODE_t * thiz,char alpha)
{int min=0,max=thiz->outgoing_degree-1,mid;
char amid;
while(min<=max)
{mid=(min+max)>>1;
amid=thiz->outgoing[mid].alpha;
if(alpha>amid)
min=mid+1;
else
if(alpha<amid)
max=mid-1;
else
return (thiz->outgoing[mid].next);}
return NULL;}
int node_has_matchstr (AC_NODE_t *thiz, AC_PATTERN_t *newstr)
{int i,j;
AC_PATTERN_t * str;
for(i=0;i<thiz->matched_patterns_num;i++)
{str=&thiz->matched_patterns[i];
if(str->length!=newstr->length)
continue;
for(j=0;j<str->length;j++)
if(str->astring[j]!=newstr->astring[j])
continue;
if(j==str->length)
return 1;}
return 0;}
void node_register_outgoing(AC_NODE_t * thiz, AC_NODE_t *next,char alpha)
{if(thiz->outgoing_degree>=thiz->outgoing_max)
{thiz->outgoing_max+=8;
thiz->outgoing=(struct edge*)realloc(thiz->outgoing,thiz->outgoing_max*sizeof(struct edge));}
thiz->outgoing[thiz->outgoing_degree].alpha=alpha;
thiz->outgoing[thiz->outgoing_degree++].next=next;}
AC_NODE_t *node_create_next(AC_NODE_t *thiz,char alpha)
{AC_NODE_t * next;
next=node_find_next(thiz, alpha);
if(next)
return NULL;
next=node_create();
node_register_outgoing(thiz,next,alpha);
return next;}
void node_register_matchstr(AC_NODE_t *thiz,AC_PATTERN_t *str)
{if(node_has_matchstr(thiz,str))
return;
if(thiz->matched_patterns_num>=thiz->matched_patterns_max)
{thiz->matched_patterns_max+=8;
thiz->matched_patterns=(AC_PATTERN_t*)realloc(thiz->matched_patterns,thiz->matched_patterns_max*sizeof(AC_PATTERN_t));}
thiz->matched_patterns[thiz->matched_patterns_num].astring=str->astring;
thiz->matched_patterns[thiz->matched_patterns_num].length=str->length;
thiz->matched_patterns[thiz->matched_patterns_num].stringy=str->stringy;
thiz->matched_patterns[thiz->matched_patterns_num++].number=str->number;}
int comp(const void *l,const void *r)
{return ((struct edge*)l)->alpha>=((struct edge*)r)->alpha?1:-1;}
void register_nodeptr(AC_AUTOMATA_t *thiz, AC_NODE_t *node)
{if(thiz->all_nodes_num>=thiz->all_nodes_max)
{thiz->all_nodes_max+=200;
thiz->all_nodes=(AC_NODE_t**)realloc(thiz->all_nodes,thiz->all_nodes_max*sizeof(AC_NODE_t*));}
thiz->all_nodes[thiz->all_nodes_num++]=node;}
AC_AUTOMATA_t *init(MATCH_CALBACK_f mc)
{AC_AUTOMATA_t *thiz=new AC_AUTOMATA_t;
memset(thiz,0,sizeof(AC_AUTOMATA_t));
thiz->root=node_create();
thiz->all_nodes_max=200;
thiz->all_nodes=new AC_NODE_t*[200];
thiz->match_callback=mc;
register_nodeptr(thiz,thiz->root);
thiz->current_node=thiz->root;
thiz->base_position=thiz->total_patterns=0;
thiz->automata_open=1;
return thiz;}
void add(AC_AUTOMATA_t *thiz,AC_PATTERN_t *patt)
{AC_NODE_t *n=thiz->root,*next;
char alpha;
for(int i=0;i<patt->length;i++)
{alpha=patt->astring[i];
if((next=node_find_next(n,alpha)))
{n=next;
continue;}
else
{next=node_create_next(n,alpha);
next->depth=n->depth+1,n=next;
register_nodeptr(thiz,n);}}
n->final=1,node_register_matchstr(n,patt);
thiz->total_patterns++;}
void set_failure(AC_AUTOMATA_t *thiz,AC_NODE_t *node,char *alphas)
{int i,j;
AC_NODE_t *m;
for(i=1;i<node->depth;i++)
{m=thiz->root;
for(j=i;j<node->depth&&m;j++)
m=node_find_next(m,alphas[j]);
if(m)
{node->failure_node=m;
break;}}
if(!node->failure_node)
node->failure_node=thiz->root;}
void traverse_setfailure(AC_AUTOMATA_t *thiz, AC_NODE_t *node,char *alphas)
{int i;
AC_NODE_t *next;
for(i=0;i<node->outgoing_degree;i++)
{alphas[node->depth]=node->outgoing[i].alpha;
next=node->outgoing[i].next;
set_failure(thiz,next,alphas);
traverse_setfailure(thiz,next,alphas);}}
void union_matchstrs(AC_NODE_t *node)
{int i;
AC_NODE_t *m=node;
while((m=m->failure_node))
{for(i=0;i<m->matched_patterns_num;i++)
node_register_matchstr(node,&(m->matched_patterns[i]));
if(m->final)
node->final=1;}}
void finalize(AC_AUTOMATA_t *thiz)
{char alphas[1024];
AC_NODE_t *node;
traverse_setfailure(thiz,thiz->root,alphas);
for(int i=0;i<thiz->all_nodes_num;i++)
{node=thiz->all_nodes[i];
union_matchstrs(node),qsort((void*)node->outgoing,node->outgoing_degree,sizeof(struct edge),comp);}
thiz->automata_open=0;}
int search(AC_AUTOMATA_t *thiz,AC_PATTERN_t *txt,void *param)
{int position=0;
AC_NODE_t *current,*next;
if(thiz->automata_open)
return -1;
current=thiz->current_node;
while(position<txt->length)
{if(!(next=node_findbs_next(current,txt->astring[position])))
if(current->failure_node)
current = current->failure_node;
else
position++;
else
current=next,position++;
if(current->final&&next)
{thiz->match.position=position+thiz->base_position;
thiz->match.match_num=current->matched_patterns_num;
thiz->match.patterns=current->matched_patterns;
if(thiz->match_callback(&thiz->match))
return 1;}}
thiz->current_node=current;
thiz->base_position+=position;
return 0;}
int match_handler(AC_MATCH_t *m)
{for(int j=0;j<m->match_num;j++)
for(int i=0;i<n;i++)
if(!strcmp(m->patterns[j].astring,a[i]))
b[i]++;
return 0;}
int main()
{freopen("ahocorasick.in","r",stdin),freopen("ahocorasick.out","w",stdout);
gets(s),scanf("%d\n",&n);
AC_AUTOMATA_t *acap=init(match_handler);
AC_PATTERN_t tmp_patt,tmp_text;
for(i=0;i<n;i++)
{gets(a[i]),tmp_patt.astring=a[i];
tmp_patt.length=strlen(a[i]),add(acap,&tmp_patt);}
finalize(acap),tmp_text.astring=s;
tmp_text.length=strlen(s);
search(acap,&tmp_text,0);
for(i=0;i<n;i++)
printf("%d\n",b[i]);
return 0;}