#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  map<string, string> mp;
  while (n--) {
    string a, b;
    cin >> a >> b;
    mp[b] = a;
  }
  while (m--) {
    string a, b;
    cin >> a >> b;
    string c;
    c.assign(b);
    c.pop_back();
    cout << a << " " << b << " #" << mp[c] << "\n";
  }
  return 0;
}
