#include <bits/stdc++.h>
using namespace std;
long long n, m;
map<string, string> mapp;
vector<pair<string, string> > v;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  while (n--) {
    string a, b;
    cin >> a >> b;
    mapp[b] = a;
  }
  while (m--) {
    string a, b;
    cin >> a >> b;
    string ip = "";
    for (int i = 0; i < b.size() - 1; i++) ip += b[i];
    cout << a << " " << b << " "
         << "#" << mapp[ip] << endl;
  }
}
