#include <bits/stdc++.h>
#pragma comment(linker, "/stack:256000000")
using namespace std;
const int INF = 1000000000;
const long long LINF = 4000000000000000000LL;
const double eps = 1e-9;
void prepare() {}
struct Chat {
  string name, text;
  unsigned int hname;
  set<unsigned int> words;
  vector<int> possibles;
};
int n, m;
vector<Chat> chats;
vector<string> names;
vector<int> res;
vector<unsigned int> hnames;
int p[100500];
unsigned int getHash(string s) {
  unsigned int h = 0;
  for (int i = (0); i < (((int)(s).size())); i++) h = h * 3137 + s[i];
  return h;
}
Chat readChat() {
  Chat chat;
  string s;
  getline(cin, s);
  int ix = s.find(':');
  chat.name = s.substr(0, ix);
  chat.hname = getHash(chat.name);
  chat.text = s.substr(ix + 1);
  s = chat.text;
  for (int i = (0); i < (((int)(s).size())); i++) {
    if (!isalnum(s[i])) s[i] = ' ';
  }
  stringstream ss(s);
  while (ss >> s) {
    chat.words.insert(getHash(s));
  }
  return chat;
}
void read() {
  names.clear();
  hnames.clear();
  chats.clear();
  string s;
  scanf("%d", &n);
  getline(cin, s);
  getline(cin, s);
  stringstream ss(s);
  while (ss >> s) {
    names.push_back(s);
    hnames.push_back(getHash(s));
  }
  scanf("%d", &m);
  getline(cin, s);
  for (int i = (0); i < ((m)); i++) {
    chats.push_back(readChat());
  }
}
bool rec(int cc) {
  if (cc >= m) return true;
  int id = p[cc];
  for (int i = (0); i < (((int)(chats[id].possibles).size())); i++) {
    int prid = chats[id].possibles[i];
    unsigned int pr = hnames[prid];
    if ((id == 0 || chats[id - 1].hname != pr) &&
        (id + 1 >= m || chats[id + 1].hname != pr)) {
      unsigned int ph = chats[id].hname;
      chats[id].hname = pr;
      res[id] = prid;
      if (rec(cc + 1)) return true;
      chats[id].hname = ph;
    }
  }
  return false;
}
bool cmp(const int &a, const int &b) {
  return chats[a].possibles.size() < chats[b].possibles.size();
}
bool solve() {
  res.clear();
  res.resize(m);
  for (int i = (0); i < ((m)); i++) {
    if (chats[i].name != "?") {
      int id =
          find((names).begin(), (names).end(), chats[i].name) - names.begin();
      chats[i].possibles.push_back(id);
      continue;
    }
    for (int j = (0); j < ((n)); j++) {
      if (!chats[i].words.count(hnames[j])) chats[i].possibles.push_back(j);
    }
  }
  for (int i = (0); i < ((m)); i++) p[i] = i;
  sort(p, p + m, cmp);
  if (rec(0)) {
    for (int i = (0); i < ((m)); i++) {
      printf("%s:%s\n", names[res[i]].c_str(), chats[i].text.c_str());
    }
  } else {
    printf("Impossible\n");
  }
  return false;
}
int main() {
  prepare();
  int t;
  scanf("%d", &t);
  for (int i = (0); i < ((t)); i++) {
    read();
    while (solve())
      ;
  }
  return 0;
}
