#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s[n], ss[n], z[m], zz[m];
  map<string, string> mp;
  for (int i = 0; i < n; i++) cin >> s[i] >> ss[i];
  for (int i = 0; i < m; i++) cin >> z[i] >> zz[i];
  for (int i = 0; i < n; i++) mp[ss[i]] = s[i];
  for (int i = 0; i < m; i++) {
    cout << z[i] << ' ' << zz[i] << ' ' << '#';
    zz[i].erase(zz[i].size() - 1);
    cout << mp[zz[i]] << endl;
  }
}
