#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <set>
inline void getstr(char s[],int&ls)
{
register char c=0;
while(!(c>='a' && c<='z'))
c=getchar();
ls=0;
while(c>='a' && c<='z')
s[++ls]=c,c=getchar();
s[ls+1]=0;
return;
}
int gcnt,ghead[360505],gnext[750505],gnode[750505];
inline void insertLine(register int s,register int t)
{
gnext[++gcnt]=ghead[s],ghead[s]=gcnt,gnode[gcnt]=t;
gnext[++gcnt]=ghead[t],ghead[t]=gcnt,gnode[gcnt]=s;
}
int cnt,parent[360505],mxval[360505],ch[360505][26];
inline int CreateNode(register int val=0)
{
parent[++cnt]=0,mxval[cnt]=val;
for(register int j=0;j<26;j++)
ch[cnt][j]=0;
return cnt;
}
int root,last;
inline int ExtendNode(register short c)
{
register int nw=CreateNode(mxval[last]+1),now=last;
for(; now && !ch[now][c]; now=parent[now])
ch[now][c]=nw;
if(!now)
return parent[nw]=root,last=nw;
register int q=ch[now][c];
if(mxval[q] == mxval[now]+1)
return parent[nw]=q,last=nw;
register int b=CreateNode(mxval[now]+1);
parent[b]=parent[q],parent[nw]=parent[q]=b;
for(register int j=0;j<26;j++)
ch[b][j]=ch[q][j];
for(; now && ch[now][c]==q; now=parent[now])
ch[now][c]=b;
return last=nw;
}
template<class T>
inline void Merge(T&a,T&b)
{
if(a.size() < b.size())
a.swap(b);
std::set<int>::iterator it=b.begin();
while(it != b.end())
a.insert(*it),it++;
b.clear();
}
int ans[360505];
std::set<int>apr[360001];
void DFS(register int o,register int fa)
{
for(register int j=ghead[o],t;j;j=gnext[j])
if((t=gnode[j]) != fa)
{
DFS(t,o);
Merge(apr[o],apr[t]);
}
ans[o]=apr[o].size();
}
char s[360505];
int main()
{
int N,Q;scanf("%d%d",&N,&Q);
root=last=CreateNode();
for(register int lp=1;lp<=N;lp++)
{
int ls;getstr(s,ls);
last=root;
for(register int i=1;i<=ls;i++)
{
ExtendNode(s[i]-'a');
apr[last].insert(lp);
}
}
for(register int i=2;i<=cnt;i++)
insertLine(i,parent[i]);
DFS(root,0);
for(register int i=1;i<=Q;i++)
{
int ls;getstr(s,ls);
register int now=root;
for(register int i=1;i<=ls && now;i++)
now=ch[now][s[i]-'a'];
printf("%d\n",ans[now]);
}
return 0;
}