#include <bits/stdc++.h>
using namespace std;
char basicname[1000][20];
char basicid[1000][20];
char name[1000][20];
char id[1000][20];
int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    memset(basicname, 0, sizeof(basicname));
    memset(basicid, 0, sizeof(basicid));
    memset(name, 0, sizeof(name));
    memset(id, 0, sizeof(id));
    for (int i = 0; i < n; i++) {
      scanf("%s", basicname[i]);
      scanf("%s", basicid[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%s", name[i]);
      scanf("%s", id[i]);
      id[i][strlen(id[i]) - 1] = '\0';
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (!strcmp(id[i], basicid[j])) {
          printf("%s ", name[i]);
          printf("%s; ", id[i]);
          printf("#%s\n", basicname[j]);
          break;
        }
      }
    }
  }
  return 0;
}
