#include <bits/stdc++.h>
using namespace std;
const int e = 1e3 + 10;
int n, m, cnt = 0;
string name[e], name2[e], ip[e], ip2[e];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> name[i] >> ip[i];
  for (int i = 0; i < m; i++) {
    cnt = 0;
    cin >> name2[i] >> ip2[i];
    while (ip[cnt] + ";" != ip2[i]) cnt++;
    cout << name2[i] << " " << ip2[i] << " #" << name[cnt] << endl;
  }
  return 0;
}
