#include <bits/stdc++.h>
using namespace std;
char chats[110][200];
char names[110][20];
int owner[110];
set<int> mayOwner[110];
int messageIndex[110];
int main() {
  int t;
  int n, m;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%d", &n);
    for (int j = 0; j < n; j++) {
      scanf("%s", names[j]);
    }
    scanf("%d", &m);
    getchar();
    for (int j = 0; j < m; j++) {
      gets(chats[j]);
      mayOwner[j].clear();
      owner[j] = -1;
      for (int k = 0; k < 15; k++) {
        if (chats[j][k] == ':') {
          messageIndex[j] = k;
        }
      }
      for (int k = 0; k < n; k++) {
        char *p = strstr(chats[j], names[k]);
        if (p == NULL) {
          mayOwner[j].insert(k);
        } else {
          if (p == chats[j] && messageIndex[j] == strlen(names[k])) {
            owner[j] = k;
            break;
          } else {
            bool finded = false;
            do {
              if ((p - 1 < chats[j] || !isalnum(*(p - 1))) &&
                  (p + strlen(names[k]) >= chats[j] + strlen(chats[j]) ||
                   !isalnum(*(p + strlen(names[k]))))) {
                finded = true;
                break;
              }
            } while ((p = strstr(p + 1, names[k])) != NULL);
            if (!finded) {
              mayOwner[j].insert(k);
            }
          }
        }
      }
    }
    bool updated = true;
    do {
      while (updated) {
        updated = false;
        for (int j = 0; j < m; j++) {
          if (owner[j] >= 0) {
          } else {
            if (j - 1 >= 0) {
              mayOwner[j].erase(owner[j - 1]);
            }
            if (j + 1 < m) {
              mayOwner[j].erase(owner[j + 1]);
            }
            if (mayOwner[j].size() == 1) {
              owner[j] = *mayOwner[j].begin();
              updated = true;
            }
          }
        }
      }
      for (int j = 0; j < m; j++) {
        if (owner[j] < 0) {
          if (mayOwner[j].size() == 0) {
            updated = false;
            break;
          } else {
            owner[j] = *mayOwner[j].begin();
            updated = true;
            break;
          }
        }
      }
    } while (updated);
    bool possiable = true;
    for (int j = 0; j < m; j++) {
      if (owner[j] < 0) {
        possiable = false;
        break;
      }
    }
    if (!possiable) {
      printf("Impossible\n");
    } else {
      for (int j = 0; j < m; j++) {
        printf("%s%s\n", names[owner[j]], chats[j] + messageIndex[j]);
      }
    }
  }
  return 0;
}
