#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
map<string, string> ma;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    ma[s2] = s1;
  }
  for (int i = 1; i <= m; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    cout << s1 << ' ' << s2 << " #" << ma[s2.substr(0, s2.length() - 1)]
         << '\n';
  }
  return 0;
}
