#include <bits/stdc++.h>
using namespace std;
map<string, string> mp;
string cmd, ip;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  while (n--) {
    cin >> cmd >> ip;
    mp[ip] = cmd;
  }
  while (m--) {
    cin >> cmd >> ip;
    cout << cmd << ' ' << ip << " #";
    ip.pop_back();
    cout << mp[ip] << '\n';
  }
}
