#include<bits/stdc++.h>
using namespace std;
int main(){
	freopen("replace.in","r",stdin);
	freopen("replace.out","w",stdout);
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		string y,e;
		cin>>y>>e;
	}
	while(q--){
		string fr,se;
		cin>>fr>>se;
		cout<<fr.size();
		cout<<"\n";
	}
	return 0;
} 
