#include <bits/stdc++.h>
using namespace std;
const int MAX = 1.2e7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  while (tc--) {
    int n, m;
    cin >> n >> m;
    map<string, string> name;
    for (int i = 0; i < n; i++) {
      string ip, nam;
      cin >> nam >> ip;
      name[ip] = nam;
    }
    while (m--) {
      string a, b;
      cin >> a >> b;
      cout << a << ' ' << b << ' ';
      b.pop_back();
      cout << '#' << name[b] << "\n";
    }
  }
  return 0;
}
