#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int who[550];
string message[500];
int par[500][500];
bool can[500][500];
inline void solve() {
  int n;
  cin >> n;
  map<string, int> NAME;
  map<int, string> rev;
  for (int i = 1; i <= n; i++) {
    string name;
    cin >> name;
    NAME[name] = i;
    rev[i] = name;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      can[i][j] = 0;
    }
  }
  string tmp;
  getline(cin, tmp);
  for (int i = 1; i <= m; i++) {
    getline(cin, tmp);
    message[i] = tmp;
    int j = 0;
    string sender;
    while (tmp[j] != ':') {
      sender += tmp[j];
      j++;
    }
    j++;
    string names;
    tmp += " ";
    if (sender != "?") {
      can[i][NAME[sender]] = 1;
      continue;
    }
    for (int ii = 1; ii <= n; ii++) {
      can[i][ii] = true;
    }
    while (j < tmp.size()) {
      if (tmp[j] == '.' || tmp[j] == ',' || tmp[j] == '!' || tmp[j] == '?' ||
          tmp[j] == ' ') {
        if (names != "") {
          if (NAME.count(names)) {
            can[i][NAME[names]] = 0;
          }
          names = "";
        }
      } else {
        names += tmp[j];
      }
      j++;
    }
  }
  for (int mess = 2; mess <= m; mess++) {
    for (int i = 1; i <= n; i++) {
      if (can[mess][i]) {
        bool boo = false;
        for (int j = 1; j <= n; j++) {
          if (i == j) {
            continue;
          }
          if (can[mess - 1][j]) {
            boo = true;
            par[mess][i] = j;
          }
        }
        if (boo == false) {
          can[mess][i] = false;
        }
      }
    }
  }
  int last = -1;
  for (int i = 1; i <= n; i++) {
    if (can[m][i]) {
      last = i;
    }
  }
  if (last == -1) {
    cout << "Impossible";
  } else {
    who[m] = last;
    for (int i = m - 1; i >= 1; i--) {
      last = par[i + 1][last];
      who[i] = last;
    }
    for (int i = 1; i <= m; i++) {
      int j = 0;
      while (message[i][j] != ':') {
        j++;
      }
      cout << rev[who[i]];
      while (j < message[i].size()) {
        cout << message[i][j];
        j++;
      }
      if (m != i) cout << "\n";
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int test = 1; test <= t; test++) {
    solve();
    if (test != t) {
      cout << "\n";
    }
  }
  return 0;
}
