#include <bits/stdc++.h>
using namespace std;
char str[100], str1[100];
char str2[100], str3[100];
char str4[1010][100], str5[100];
int n, m;
int main() {
  map<string, int> mp;
  scanf("%d%d", &n, &m);
  int k = 0;
  for (int i = 0; i < n; i++) {
    scanf("%s%s", str, str1);
    mp[str1] = k;
    for (int j = 0; j < strlen(str); j++) str4[k][j] = str[j];
    k++;
  }
  while (m--) {
    scanf("%s%s", str2, str3);
    int flag = 0;
    for (int i = 0; i < strlen(str3); i++) {
      if (str3[i] < '0' || str3[i] > '9') {
        if (str3[i] != '.' && str3[i] != ';') {
          flag = 1;
          break;
        }
      }
    }
    if (flag)
      printf("%s %s\n", str2, str3);
    else {
      for (int i = 0; i < strlen(str3) - 1; i++) str5[i] = str3[i];
      printf("%s %s #%s\n", str2, str3, str4[mp[str5]]);
      memset(str5, 0, sizeof(str5));
    }
  }
  return 0;
}
