#include <bits/stdc++.h>
using namespace std;
struct Msg {
  int index;
  string name;
  set<string> avail;
};
int t, n, m;
set<string> nameset;
vector<string> names;
vector<string> lines;
vector<Msg> sol;
bool alphanum(char c) {
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
         (c >= '0' && c <= '9');
}
string next(const string &s, int &i) {
  while (i < s.size() && !alphanum(s[i])) ++i;
  int x = i;
  while (i < s.size() && alphanum(s[i])) ++i;
  return s.substr(x, i - x);
}
void process(int idx, const string &s) {
  int i = 0;
  if (s[i] == '?') {
    set<string> p = nameset;
    i = 2;
    string name;
    while ((name = next(s, i)) != "") p.erase(name);
    sol.push_back({idx, "", p});
  } else {
    while (s[i] != ':') ++i;
    set<string> p = {s.substr(0, i)};
    sol.push_back({idx, "", p});
  }
}
bool solve() {
  queue<int> que;
  for (int i = 0; i < m; ++i) {
    if (sol[i].avail.empty())
      return false;
    else if (sol[i].avail.size() == 1)
      que.push(i);
  }
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    string name = *sol[i].avail.begin();
    sol[i].name = name;
    if (i - 1 >= 0 && sol[i - 1].name == "") {
      sol[i - 1].avail.erase(name);
      if (sol[i - 1].avail.empty())
        return false;
      else if (sol[i - 1].avail.size() == 1)
        que.push(i - 1);
    }
    if (i + 1 < m && sol[i + 1].name == "") {
      sol[i + 1].avail.erase(name);
      if (sol[i + 1].avail.empty())
        return false;
      else if (sol[i + 1].avail.size() == 1)
        que.push(i + 1);
    }
  }
  return true;
}
void solve2() {
  for (int i = 0; i < m; ++i) {
    if (sol[i].name == "") {
      sol[i].name = *sol[i].avail.begin();
      if (i < m - 1) sol[i + 1].avail.erase(sol[i].name);
    }
  }
}
void printline(int idx) {
  int i = 0;
  while (lines[idx][i] != ':') ++i;
  string text = lines[idx].substr(i);
  cout << sol[idx].name << text << endl;
}
int main() {
  cin >> t;
  while (t--) {
    names.clear();
    nameset.clear();
    lines.clear();
    sol.clear();
    cin >> n;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      names.push_back(s);
      nameset.insert(s);
    }
    cin >> m;
    string empty;
    getline(cin, empty);
    for (int i = 0; i < m; ++i) {
      string s;
      getline(cin, s);
      process(i, s);
      lines.push_back(s);
    }
    if (solve()) {
      solve2();
      for (int i = 0; i < m; ++i) printline(i);
    } else
      cout << "Impossible" << endl;
  }
  return 0;
}
