//50
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n" 
int n,q;
string s[200005][3];
string q1,q2;
int ans;
signed main(){
	freopen("replace.in","r",stdin);
	freopen("replace.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> q;
	for(int i=1;i<=n;i++){
		cin >> s[i][1] >> s[i][2];
	}
	while(q --){
		ans = 0;
		cin >> q1 >> q2;
		for(int i=1;i<=n;i++){
			int pos = q1.find(s[i][1]);
			if(pos == -1) continue;
			string tmp = q1.substr(0,pos);
			tmp += s[i][2];
			tmp += q1.substr(pos + s[i][1].size());
			ans += (tmp == q2);
		}
		cout << ans << endl;
	}
	return 0;
} 
//long long(unsigned long long?) 
//��ʼ��(lazy=0/1,init()��������n����)
//����t[x].val = val[id[l]] 
//���Լ��n+1
//���鼯init(0,n+1?)
//�ռ価���ܿ�8��,�Ƿ���Ҫ˫���?���Լ���ཨ��������,��0���ɵı߱�ȨΪ�� 
//__gcd() __int128 __builtin_popcount()
//�Լ�����freopen 
//���� 
