#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<string, string> a, pair<string, string> b) {
  return a.second < b.second;
}
int binarysearch(pair<string, string> servers[], string ip, int l, int r) {
  if (r < l) return -1;
  int mid = (l + r) / 2;
  if (servers[mid].second.compare(ip) == 0)
    return mid;
  else if (servers[mid].second.compare(ip) > 0)
    return binarysearch(servers, ip, l, mid - 1);
  else
    return binarysearch(servers, ip, mid + 1, r);
}
int main() {
  int n, m;
  cin >> n >> m;
  pair<string, string> servers[n];
  pair<string, string> commands[m];
  for (int i = 0; i < n; i++) {
    cin >> servers[i].first >> servers[i].second;
    servers[i].second = servers[i].second + ";";
  }
  for (int i = 0; i < m; i++) cin >> commands[i].first >> commands[i].second;
  sort(servers, servers + n, cmp);
  string id = commands[0].second;
  string ip = servers[0].second;
  for (int i = 0; i < m; i++) {
    int id = binarysearch(servers, commands[i].second, 0, n - 1);
    cout << commands[i].first << " " << commands[i].second << " #"
         << servers[id].first << endl;
  }
  return 0;
}
