#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  map<string, string> map1;
  for (int i = 0; i < n; ++i) {
    string t1, t2;
    cin >> t1 >> t2;
    map1[t2] = t1;
  }
  for (int j = 0; j < m; ++j) {
    string t1, t2, t3;
    cin >> t1 >> t2;
    for (int i = 0; i < t2.size() - 1; ++i) {
      t3 += t2[i];
    }
    cout << t1 << " " << t2 << " #" << map1[t3] << endl;
  }
}
