#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long MOD = 998244353;
double eps = 1e-12;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<pair<string, string>> v1;
  vector<pair<string, string>> v2;
  for (long long i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    v1.push_back(make_pair(a, b));
  }
  for (long long i = 0; i < m; i++) {
    string a, b;
    cin >> a >> b;
    b.erase(b.length() - 1);
    v2.push_back(make_pair(a, b));
  }
  for (long long i = 0; i < m; i++) {
    for (long long j = 0; j < n; j++) {
      if (v2[i].second == v1[j].second) {
        cout << v2[i].first << " " << v2[i].second << ";"
             << " "
             << "#" << v1[j].first << "\n";
        break;
      }
    }
  }
  return 0;
}
