#include <bits/stdc++.h>
using namespace std;
map<string, string> st;
char a[1000];
char b[1000];
char br[1000];
char k;
int main() {
  long long int n, m, i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    st[b] = a;
  }
  for (i = 0; i < m; i++) {
    cin >> a;
    cin >> b;
    for (j = 0; b[j] != ';'; j++) {
      br[j] = b[j];
    }
    cout << a << " " << b << " #" << st[br] << endl;
    for (j = 0; b[j] != ';'; j++) br[j] = '\0';
  }
  return 0;
}
