#include <bits/stdc++.h>
const int N = 1e5 + 1;
using namespace std;
int n, m;
bool was[N];
int a[N], k;
pair<string, string> p[N];
string s, ss;
string s1, ss1, ss2;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> s >> ss;
    p[i] = make_pair(s, ss);
  }
  for (int i = 1; i <= k; i++) {
    cin >> s1 >> ss1;
    ss2 = "";
    cout << s1 << " " << ss1 << " ";
    for (int x = 0; x < ss1.size() - 1; x++) {
      ss2 += ss1[x];
    }
    for (int j = 1; j <= n; j++) {
      if (ss2 == p[j].second) {
        cout << '#' << p[j].first << endl;
      }
    }
  }
  return 0;
}
