#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s1[n], s2[n];
  for (int i = 0; i < n; i++) {
    cin >> s1[i] >> s2[i];
  }
  for (int i = 0; i < m; i++) {
    string a, b;
    cin >> a >> b;
    cout << a << " " << b << " ";
    string t(b.begin(), b.end() - 1);
    for (int j = 0; j < n; j++) {
      if (t == s2[j]) {
        cout << "#" << s1[j] << endl;
        break;
      }
    }
  }
  return 0;
}
