#include #include #include // when size is too small, the table will overflow. #define SIZE 100023 #define IDLEN 19 #define P 13 typedef struct hashtable { int m; int p; char **key; int *answer; } hashTable; hashTable *newHash() { hashTable *h = malloc(sizeof(hashTable)); h->m = SIZE; h->p = P; h->key = malloc(sizeof(char *) * SIZE); for (int i = 0; i < SIZE; i++) { h->key[i] = malloc(sizeof(char) * IDLEN); h->key[i][0] = '#'; h->key[i][1] = '\0'; } h->answer = malloc(sizeof(int) * SIZE); for (int i = 0; i < SIZE; i++) { h->answer[i] = 0; } return h; } int hash(hashTable *h, char *str) { int m = h->m; int p = h->p; int ans = 0; int p_pow = 1; for (int i = 0; i < IDLEN - 1; i++) { if (str[i] == 'x') { ans = (ans + 11 * p_pow) % m; } else { ans = (ans + (str[i] - '0' + 1) * p_pow) % m; } p_pow = (p_pow * p) % m; } int quad = 1; while (h->key[ans][0] != '#' && strcmp(h->key[ans], str) != 0) { // collision ans = (ans + quad * quad) % m; quad++; } return ans; } int max(int l, int r) { return (l > r) ? l : r; } int main(void) { int n, k; scanf("%d%d", &n, &k); char buf[IDLEN]; int tmp; hashTable *h = newHash(); int strHash; for (int i = 0; i < n; i++) { scanf("%s%d", buf, &tmp); strHash = hash(h, buf); strcpy(h->key[strHash], buf); h->answer[strHash] += max(k, tmp); } scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", buf); strHash = hash(h, buf); if (h->key[strHash][0] == '#') { printf("No Info\n"); } else { printf("%d\n", h->answer[strHash]); } } return 0; }