#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string a[1000], b[1000];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    string cmd, ip, tip;
    cin >> cmd >> ip;
    for (int i = 0; i < ip.size() - 1; i++) tip += ip[i];
    for (int i = 0; i < n; i++) {
      if (tip.compare(b[i]) == 0)
        cout << cmd << " " << ip << " #" << a[i] << '\n';
    }
  }
}
