#include <bits/stdc++.h>
using namespace std;
struct Node {
  char str[15];
  char ip[35];
  int len;
} node[1100];
struct Que {
  char name[15];
  char ip2[35];
} que[1100];
int n, m;
int slove(char ss[]) {
  int ll = strlen(ss) - 1;
  for (int i = 0; i < n; i++) {
    if (ll == node[i].len) {
      bool flag = 1;
      for (int j = 0; j < ll; j++) {
        if (ss[j] != node[i].ip[j]) {
          flag = 0;
          break;
        }
      }
      if (flag == 1) return i;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s%s", node[i].str, node[i].ip);
    node[i].len = strlen(node[i].ip);
  }
  for (int i = 0; i < m; i++) {
    scanf("%s%s", que[i].name, que[i].ip2);
    int pp = slove(que[i].ip2);
    printf("%s %s #%s\n", que[i].name, que[i].ip2, node[pp].str);
  }
}
