#include <bits/stdc++.h>
using namespace std;
struct IP {
  string nm;
  int a, b, c, d;
};
int n, m;
int test(IP a[], IP x) {
  for (int i = 0; i < n; i++) {
    if (a[i].a == x.a && a[i].b == x.b && a[i].c == x.c && a[i].d == x.d)
      return i;
  }
  return -1;
}
IP a[1000], b[1000];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i].nm >> a[i].a;
    getchar();
    cin >> a[i].b;
    getchar();
    cin >> a[i].c;
    getchar();
    cin >> a[i].d;
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].nm >> b[i].a;
    getchar();
    cin >> b[i].b;
    getchar();
    cin >> b[i].c;
    getchar();
    cin >> b[i].d;
    getchar();
  }
  for (int i = 0; i < m; i++) {
    cout << b[i].nm << " ";
    printf("%d.%d.%d.%d;", b[i].a, b[i].b, b[i].c, b[i].d);
    int k = test(a, b[i]);
    if (k != -1) cout << " #" << a[k].nm;
    cout << endl;
  }
  return 0;
}
