#include <bits/stdc++.h>
using namespace std;
int n, m;
map<string, string> m_ip;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string name, ip;
    cin >> name >> ip;
    m_ip[ip] = name;
  }
  for (int j = 0; j < m; ++j) {
    string name, ip;
    cin >> name >> ip;
    cout << name << " " << ip << " #" << m_ip[ip.substr(0, ip.size() - 1)]
         << endl;
  }
  return 0;
}
