#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  unordered_map<string, string> mp;
  while (n--) {
    string s1, s2;
    cin >> s1 >> s2;
    mp[s2] = s1;
  }
  vector<string> op;
  while (m--) {
    string s1, s2, s = "";
    cin >> s1 >> s2;
    s += s1;
    s += " ";
    s += s2;
    s += " #";
    s += mp[s2.substr(0, s2.length() - 1)];
    op.push_back(s);
  }
  for (int i = 0; i < op.size(); i++) cout << op[i] << endl;
}
