#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int iinf = INT_MAX / 2;
const long long linf = LONG_MAX / 2;
const int MOD = 1e9 + 7;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
map<string, string> a;
int n, m;
string s, p;
int main() {
  cin >> n >> m;
  while (n--) {
    cin >> s >> p;
    a[p] = s;
  }
  while (m--) {
    cin >> s >> p;
    cout << s << " " << p << " #" << a[p.substr(0, p.length() - 1)] << '\n';
  }
}
