#include <bits/stdc++.h>
using namespace std;
int n,m;
int p[100010];
string wd[100010];
int main(){
freopen("engzam.in","r",stdin);
freopen("engzam.out","w",stdout);
cin.tie(0);cout.tie(0);
string b;
int l,res;
cin>>n>>m;
for (int i=1;i<=n;i++){
cin>>b>>p[i];
b=" "+b;
wd[i]=b;
}
for (int x=1;x<=m;x++){
cin>>b>>l;
b=" "+b;
res=0;
for (int i=1;i<=n;i++){
if (p[i]<l) continue;
for (int j=1;j<b.length();j++){
if (b[j]!=wd[i][j]) break;
if (j==b.length()-1) res++;
}
}
cout<<res<<'\n';
}
return 0;
}