#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
vector<pair<int, int> > genFactors(int n) {
  vector<pair<int, int> > v;
  for (int d = 2; d <= n; ++d) {
    if (n % d == 0) {
      int k = 0;
      while (n % d == 0) {
        ++k;
        n /= d;
      }
      v.push_back(make_pair(d, k));
    }
  }
  return v;
}
long long modPow2(long long base, long long p, long long MOD) {
  long long ans = 1, exp = p % (MOD - 1);
  while (exp > 0) {
    if (exp & 1) ans = (ans * base) % MOD;
    base = (base * base) % MOD;
    exp >>= 1;
  }
  return ans;
}
void solve();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
int n, m;
map<string, string> ma;
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string s;
    string ip;
    cin >> s >> ip;
    for (int j = 0; j < s.length(); ++j) s[j] = tolower(s[j]);
    ma[ip + ";"] = s;
  }
  for (int i = 0; i < m; ++i) {
    string s;
    string ip;
    cin >> s >> ip;
    cout << s << " " << ip << " #" << ma[ip] << endl;
  }
}
