#include<bits/stdc++.h>
using namespace std;
const int N = 1e6;
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e7+10;
int Pos[MAXN];
struct Trie
{
int Next[26];
int end;
void Init()
{
end = 0;
memset(Next, 0, sizeof(Next));
}
}trie[MAXN];
int n, m, cnt;
void Insert(string word)
{
int pos = 0;
int len = word.length();
for (int i = 0;i < len;i++)
{
if (trie[pos].Next[word[i]-'a'] == 0)
{
trie[pos].Next[word[i]-'a'] = ++cnt;
trie[cnt].Init();
}
pos = trie[pos].Next[word[i]-'a'];
}
trie[pos].end = 1;
}
int Count(string sen)
{
int ans = 0, pos = 0;
memset(Pos, 0, sizeof(Pos));
int len = sen.length();
for (int i = 0;i < len;i++)
{
if (trie[pos].Next[sen[i]-'a'] == 0)
break;
pos = trie[pos].Next[sen[i]-'a'];
if (trie[pos].end == 1)
Pos[i] = 1;
}
for (int i = 0;i < len;i++)
{
if (Pos[i] == 0)
continue;
else
ans = i+1;
pos = 0;
for (int j = i+1;j < len;j++)
{
if (trie[pos].Next[sen[j]-'a'] == 0)
break;
pos = trie[pos].Next[sen[j]-'a'];
if (trie[pos].end == 1)
Pos[j] = 1;
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
trie[0].Init();
for (int i = 1;i <= n;i++)
{
string tmp;
cin >> tmp;
Insert(tmp);
}
for (int i = 1;i <= m;i++)
{
string tmp;
cin >> tmp;
Count(tmp);
cout << Count(tmp) << endl;
}
return 0;
}