#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t = 1;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    map<string, string> mp;
    for (int i = 1; i <= n; i++) {
      string s, s2;
      cin >> s >> s2;
      mp[s2] = s;
    }
    for (int i = 1; i <= m; i++) {
      string s, s2;
      cin >> s >> s2;
      cout << s << " " << s2 << " #";
      s2.erase(s2.size() - 1, 1);
      cout << mp[s2] << endl;
    }
  }
}
