#include <bits/stdc++.h>
using namespace std;
const int N = 105;
map<string, int> ls;
string op[N];
vector<string> fans;
bool temp[N][N];
int kn[N], dp[N][N];
bool fin = false, imp = false;
int q, n;
int calc(int in, int prev) {
  if (in == q) {
    dp[in][prev] = 1;
    fin = true;
    return 1;
  }
  int &ret = dp[in][prev];
  if (ret != -1) return ret;
  ret = 0;
  if (kn[in] == prev && in) {
    return 0;
  }
  if (kn[in])
    ret = calc(in + 1, kn[in]);
  else
    for (int x = 1; x <= n; x++) {
      if (!temp[in][x] && x != prev) {
        ret |= calc(in + 1, x);
      }
    }
  return ret;
}
queue<string> d;
void print(int in, int prev) {
  if (in == q) {
    return;
  }
  if (kn[in]) {
    print(in + 1, kn[in]);
    return;
  }
  for (int x = 1; x <= n; x++) {
    if (!temp[in][x] && x != prev && dp[in + 1][x] > 0) {
      d.push(op[x]);
      print(in + 1, x);
      break;
    }
  }
}
int split(string s, int in) {
  string t = "";
  int at = 0;
  for (int i = 0; i < s.length(); i++) {
    if (isupper(s[i]) || islower(s[i]) || isdigit(s[i])) {
      t += s[i];
    } else {
      if (t.size()) {
        if (ls[t]) {
          if (at == 0) at = ls[t];
          temp[in][ls[t]] = 1;
        }
      }
      t = "";
    }
  }
  if (t.size()) {
    if (ls[t]) {
      if (at == 0) at = ls[t];
      temp[in][ls[t]] = 1;
    }
  }
  return at;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    fin = false;
    imp = false;
    scanf("%d", &n);
    ls.clear();
    for (int i = 0; i < 105; i++) {
      op[i] = "";
    }
    fans.clear();
    string s;
    memset(kn, 0, sizeof kn);
    for (int i = 1; i <= n; i++) {
      cin >> s;
      op[i] = s;
      ls[s] = i;
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
      scanf(" ");
      getline(cin, s);
      fans.push_back(s);
    }
    memset(temp, 0, sizeof temp);
    for (int i = 0; i < q; i++) {
      s = fans[i];
      int f = split(s, i);
      if (s[0] != '?') {
        kn[i] = f;
        if (op[f] == "") {
          imp = true;
        }
      }
    }
    int ans = 0;
    if (!imp) {
      memset(dp, -1, sizeof dp);
      ans = calc(0, 0);
    }
    if (imp || !fin) {
      puts("Impossible");
    } else {
      print(0, 0);
      for (int i = 0; i < fans.size(); i++) {
        s = fans[i];
        if (s[0] != '?') {
          puts(s.c_str());
        } else {
          printf("%s", d.front().c_str());
          d.pop();
          int x = s.find(':');
          puts(s.substr(x).c_str());
        }
      }
    }
  }
  return 0;
}
