#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long a, long long n, long long m) {
  if (n == 0) return 1LL;
  if (n == 1) return a;
  long long x = bigmod(a, n / 2, m);
  if (n % 2) return (((a * x) % m) * x) % m;
  return (x * x) % m;
}
string names[102], msg[102];
map<string, int> ch;
pair<string, string> dt[102];
int main() {
  ios_base::sync_with_stdio(0);
  int i, j, x, n, cnt = 0, m, y, k, g, flg, t;
  cin >> t;
  string s;
  while (t--) {
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> names[i];
      ch[names[i]] = 0;
    }
    cin >> m;
    y = 105;
    flg = 0;
    char c;
    for (i = 1; i <= m; i++) {
      cin >> c;
      getline(cin, msg[i]);
      dt[i].first = c;
      for (j = 0; j <= msg[i].size() - 1; j++) {
        if (msg[i][j] == ':') break;
        dt[i].first += msg[i][j];
      }
      dt[i].second = msg[i][j + 1];
      for (j = j + 2; j <= msg[i].size() - 1; j++) {
        dt[i].second += msg[i][j];
      }
    }
    while (y--) {
      for (i = 1; i <= m; i++) {
        if (dt[i].first != "?") continue;
        for (j = 1; j <= n; j++) ch[names[j]] = 0;
        s.clear();
        for (j = 0; j <= dt[i].second.size() - 1; j++) {
          if ((dt[i].second[j] >= 'a' && dt[i].second[j] <= 'z') ||
              (dt[i].second[j] >= 'A' && dt[i].second[j] <= 'Z') ||
              (dt[i].second[j] >= '0' && dt[i].second[j] <= '9')) {
            s += dt[i].second[j];
          } else {
            if (s.size()) {
              ch[s] = 1;
            }
            s.clear();
          }
        }
        if (s.size()) {
          ch[s] = 1;
        }
        if (i > 1) {
          if (dt[i - 1].first != "?") {
            ch[dt[i - 1].first] = 1;
          }
        }
        if (i < m) {
          if (dt[i + 1].first != "?") {
            ch[dt[i + 1].first] = 1;
          }
        }
        cnt = 0;
        for (j = 1; j <= n; j++) {
          if (!ch[names[j]]) {
            cnt++;
            x = j;
          }
        }
        if (cnt == 1) {
          dt[i].first = names[x];
        } else if (cnt == 0) {
          cout << "Impossible" << endl;
          flg = 1;
          break;
        }
      }
      if (flg) break;
    }
    if (flg) continue;
    for (i = 1; i <= m; i++) {
      if (dt[i].first != "?") continue;
      for (j = 1; j <= n; j++) ch[names[j]] = 0;
      s.clear();
      for (j = 0; j <= dt[i].second.size() - 1; j++) {
        if ((dt[i].second[j] >= 'a' && dt[i].second[j] <= 'z') ||
            (dt[i].second[j] >= 'A' && dt[i].second[j] <= 'Z') ||
            (dt[i].second[j] >= '0' && dt[i].second[j] <= '9')) {
          s += dt[i].second[j];
        } else {
          if (s.size()) {
            ch[s] = 1;
          }
          s.clear();
        }
      }
      if (s.size()) {
        ch[s] = 1;
      }
      if (i > 1) {
        if (dt[i - 1].first != "?") {
          ch[dt[i - 1].first] = 1;
        }
      }
      if (i < m) {
        if (dt[i + 1].first != "?") {
          ch[dt[i + 1].first] = 1;
        }
      }
      cnt = 0;
      for (j = 1; j <= n; j++) {
        if (!ch[names[j]]) {
          dt[i].first = names[j];
          break;
        }
      }
    }
    for (i = 1; i <= m; i++) {
      cout << dt[i].first << ":" << dt[i].second << endl;
    }
  }
}
