#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<pair<string, string> > v, final;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    v.push_back(make_pair(s1, s2));
  }
  for (int i = 0; i < m; i++) {
    string s1, s2, nxt;
    cin >> s1 >> s2;
    for (int j = 0; j < s2.length() - 1; j++) {
      nxt.push_back(s2[j]);
    }
    final.push_back(make_pair(s1, nxt));
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (final[i].second == v[j].second) {
        cout << final[i].first << " " << final[i].second << ";"
             << " "
             << "#" << v[j].first << endl;
      }
    }
  }
  return 0;
}
