#include <bits/stdc++.h>
#define maxn 2000100
#define maxm 5010
#define endl '\n'
using namespace std;
struct Trie{
int nxt[maxm][30], cnt;
bool exist[maxn];
void ins(char *s, int l){
int p = 0;
for(int i = 0; i < l; i++){
int c = s[i] - 'a';
if(!nxt[p][c]) nxt[p][c] = ++cnt;
p = nxt[p][c];
}
exist[p] = true;
}
int find(char *s,int pos,int l){
int p = 0;
int ans = pos, i;
if(pos == l) return pos;
for(i = pos; i <= l; i++){
int c = s[i] - 'a';
if(!nxt[p][c] && p == 0 && i == pos){
return pos - 1;
}
if(!nxt[p][c] && p != 0){
if(exist[p] && i != l) {
ans = i;
break;
}
if(exist[p] && i == l){
return l;
}
ans = max(ans, find(s, i, l));
return pos - 1;
}
p = nxt[p][c];
}
return max(ans, find(s, i, l));
}
}trie;
int n, m;
char ch[maxn];
int main(){
freopen("1.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> ch;
trie.ins(ch, strlen(ch));
}
for(int i = 1; i <= m; i++){
cin >> ch;
int num = trie.find(ch, 0, strlen(ch));
if(num == -1) cout << 0 << endl;
else cout << num << endl;
}
return 0;
}