#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<string, string> > v1, v2;
  for (long long i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    pair<string, string> p = make_pair(s1, s2);
    v1.push_back(p);
  }
  for (long long i = 0; i < m; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    pair<string, string> p = make_pair(s1, s2);
    v2.push_back(p);
  }
  for (long long i = 0; i < m; i++) {
    for (long long j = 0; j < n; j++) {
      string a = v1[j].second;
      string b = v2[i].second;
      b.erase(b.end() - 1);
      if (a == b)
        cout << v2[i].first << " " << v2[i].second << " #" << v1[j].first
             << endl;
    }
  }
  return 0;
}
