#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,m;	cin >> n >> m;
	string s,c,i;
	map<string,string> ser;
	for(int j=0;j<n;j++){
		cin >> s >> i;
		ser.insert(make_pair(i,s));
	}
	for(int j=0;j<m;j++){
		cin >> c >> i;
		cout << c << " " << i << " #";
		auto it = ser.find(i);
		cout << it->second << endl;
		// cout << c << " " << i << " #" << it->second << endl;
	}
	return 0;
}