#include <bits/stdc++.h>
using namespace std;
struct p {
  string na, ip;
};
struct h {
  string na, ip;
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<p> a(n);
  vector<h> b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i].na >> a[i].ip;
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].na >> b[i].ip;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int fl = 1;
      for (int s = 0; s < a[j].ip.size(); s++) {
        if (b[i].ip[s] != a[j].ip[s]) {
          fl = 0;
          break;
        }
      }
      if (fl == 1) {
        cout << b[i].na << " " << b[i].ip << " #" << a[j].na << "\n";
        break;
      }
    }
  }
  return 0;
}
