#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smax(T &x, T y) {
  x = max((x), (y));
}
template <class T>
inline void smin(T &x, T y) {
  x = min((x), (y));
}
inline void sc(int &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f) x = -x;
  return;
}
void sc(char &x) { scanf("%c", &x); }
void sc(int &x, int &y) {
  sc(x);
  return sc(y);
}
void sc(int &x, int &y, int &z) {
  sc(x);
  sc(y);
  return sc(z);
}
const double eps = 1e-7;
int t, n, k, i, j, m, x;
int inp[2000005], A[2000005], B[2000005];
char str[2000005];
map<string, int> mp;
int check(string &from, string &to) {
  int x = 0;
  if (from == to) return 1;
  while (from.find(to, x) != -1) {
    int pos = from.find(to, x);
    if (pos == 0 && !isalpha(from[to.size()]) && !isdigit(from[to.size()]))
      return 1;
    if (pos == from.size() - to.size() && !isalpha(from[pos - 1]) &&
        !isdigit(from[pos - 1]))
      return 1;
    if (!isalpha(from[pos - 1]) && !isdigit(from[pos - 1]) &&
        !isalpha(from[pos + int((to).size())]) &&
        !isdigit(from[pos + int((to).size())]))
      return 1;
    x++;
  }
  return 0;
}
int correct(string &str) {
  return 0;
  int x = 0;
  while (x < str.size() && str[x] == ' ') x++;
  str = str.substr(x);
}
int calc(vector<vector<int>> &dp) {
  for (int i = 0; i < dp.size() - 1; i++) {
    for (int j = 0; j < dp[0].size(); j++) {
      int flag = 0;
      for (long long kk = 0; kk < dp[0].size(); kk++) {
        if (j != kk && dp[i + 1][j] && dp[i][kk]) {
          flag = kk + 1;
          break;
        }
      }
      dp[i + 1][j] = flag;
    }
  }
  return accumulate((dp.back()).begin(), (dp.back()).end(), 0);
}
int main() {
  std::ios::sync_with_stdio(false);
  string str;
  cin >> t;
  while (t--) {
    int cntttt = 0;
    int user, mess;
    cin >> user;
    vector<string> vs;
    for (long long i = 0; i < user; i++) {
      cin >> str;
      vs.push_back(str);
      mp[str] = i;
    }
    cin >> mess;
    vector<pair<string, string>> vm;
    vector<vector<int>> dp(mess, vector<int>(user, 1));
    std::cin.ignore();
    for (long long i = 0; i < mess; i++) {
      getline(cin, str);
      int pos = find((str).begin(), (str).end(), ':') - str.begin();
      vm.push_back({str.substr(0, pos), str.substr(pos + 1)});
      correct(vm.back().second);
    }
    int flag = 1;
    for (long long i = 0; i < mess; i++) {
      if (vm[i].first.size() == 1 && vm[i].first[0] == '?') {
      } else {
        if (check(vm[i].second, vm[i].first)) {
          cout << "Impossible\n";
          cntttt++;
          break;
        }
        for (long long k = 0; k < user; k++) dp[i][k] = 0;
        dp[i][mp[vm[i].first]] = 1;
      }
      for (long long j = 0; j < user; j++) {
        if (check(vm[i].second, vs[j])) {
          dp[i][j] = 0;
        }
      }
    }
    if (cntttt) continue;
    int res = calc(dp);
    if (!res) {
      cout << "Impossible\n";
      continue;
    };
    int last;
    for (i = 0; i < dp[0].size(); i++) {
      if (dp.back()[i]) {
        last = dp.back()[i];
        break;
      }
    }
    vm.back().first = vs[i];
    i = mess - 2;
    while (i >= 0) {
      vm[i].first = vs[last - 1];
      last = dp[i][last - 1];
      i--;
    }
    for (long long i = 0; i < mess; i++) {
      cout << vm[i].first << ":" << vm[i].second << endl;
    }
  }
  return 0;
}
