#include <bits/stdc++.h>
using namespace std;
int II() {
  int n;
  scanf("%d", &n);
  return n;
}
int table[100][100];
char message[100][200];
string names[100];
void recprint(int m, int s) {
  if (m < 0) return;
  recprint(m - 1, table[m][s]);
  cout << names[s];
  printf(":%s\n", message[m]);
}
int main() {
  int T = II();
  while (T--) {
    set<std::string> usernames;
    int N = II();
    for (int x = 0; x < N; x++) {
      string s;
      cin >> s;
      usernames.insert(s);
    }
    for (auto it = usernames.begin(); it != usernames.end(); it++)
      names[distance(usernames.begin(), it)] = *it;
    int M = II();
    int sa = N, sb = N;
    for (int m = 0; m < M; m++) {
      for (int x = 0; x < N; x++) table[m][x] = N;
      string user;
      user.clear();
      char c = getchar();
      while (c < 32) c = getchar();
      while (c != ':') {
        user += c;
        c = getchar();
      }
      int it = distance(usernames.begin(), usernames.find(user));
      if (it < N)
        for (int x = 0; x < N; x++)
          if (x != it) table[m][x] = -1;
      user.clear();
      char *msg = message[m];
      c = getchar();
      while (true) {
        bool ended = false;
        if (c == '!' || c == '?' || c == '.' || c == ',' || c == ' ' ||
            c == 10) {
          int it = distance(usernames.begin(), usernames.find(user));
          if (it < N) table[m][it] = -1;
          ended = true;
        }
        if (c == 10) break;
        *msg = c;
        msg++;
        user += c;
        if (ended) user.clear();
        c = getchar();
      }
      *msg = 0;
      for (int x = 0; x < N; x++)
        if (table[m][x] != -1) table[m][x] = (x == sa ? sb : sa);
      sa = sb = -1;
      for (int x = 0; x < N; x++)
        if (table[m][x] != -1) sa = x;
      for (int x = 0; x < N; x++)
        if (table[m][x] != -1)
          if (x != sa) sb = x;
    }
    if (sa == -1)
      printf("Impossible\n");
    else
      recprint(M - 1, sa);
  }
}
