#include <bits/stdc++.h>
using namespace std;
mt19937 ran(time(0));
int mod = 1e9 + 7;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long Bpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int m;
    cin >> m;
    vector<map<string, int>> ma(m);
    vector<int> k(m, n);
    vector<string> ans(m);
    vector<pair<string, int>> p;
    vector<string> mes;
    for (int i = 0; i < m; i++) {
      string str;
      do {
        string c;
        cin >> c;
        str += c + " ";
      } while (cin.peek() != '\n');
      mes.push_back(str);
      if (str[0] == '?') {
        p.push_back({str, i});
      } else {
        string name;
        for (int j = 0; str[j] != ':'; j++) {
          name.push_back(str[j]);
        }
        ans[i] = name;
      }
    }
    set<pair<int, int>> st;
    for (int i = 0; i < p.size(); i++) {
      if (p[i].second && ans[p[i].second - 1] != "") {
        k[p[i].second]--;
        ma[p[i].second][ans[p[i].second - 1]]++;
      }
      if (p[i].second < m - 1 && ans[p[i].second + 1] != "" &&
          !ma[p[i].second][ans[p[i].second + 1]]) {
        k[p[i].second]--;
        ma[p[i].second][ans[p[i].second + 1]]++;
      }
      string str = p[i].first;
      if (i == 3) i = i;
      for (int j = 2; j < str.size(); j++) {
        for (int x = 0; x < n; x++) {
          bool f1 = false, f2 = false;
          for (char i = '0'; i < '9'; i++) {
            f1 |= str[j - 1] == i;
            f2 |= (j + s[x].size() < str.size() && str[j + s[x].size()] == i);
          }
          for (char i = 'a'; i < 'z' + 1; i++) {
            f1 |= str[j - 1] == i;
            f2 |= (j + s[x].size() < str.size() && str[j + s[x].size()] == i);
          }
          for (char i = 'A'; i < 'Z' + 1; i++) {
            f1 |= str[j - 1] == i;
            f2 |= (j + s[x].size() < str.size() && str[j + s[x].size()] == i);
          }
          if (!f1 && !f2 && j + s[x].size() - 1 < str.size() &&
              str.substr(j, s[x].size()) == s[x] && !ma[p[i].second][s[x]]) {
            k[p[i].second]--;
            ma[p[i].second][s[x]]++;
          }
        }
      }
      st.insert({k[p[i].second], p[i].second});
    }
    bool f = false;
    while (!st.empty()) {
      int num = st.begin()->second;
      st.erase(st.begin());
      if (k[num] == 0) {
        f = true;
        break;
      }
      if (k[num] != 2) {
        for (int i = 0; i < n; i++) {
          if (ma[num][s[i]] == 0) {
            ans[num] = s[i];
            if (num && ma[num - 1][s[i]] == 0 && ans[num - 1] == "") {
              k[num - 1]--;
              ma[num - 1][s[i]]++;
              st.erase({k[num - 1] + 1, num - 1});
              st.insert({k[num - 1], num - 1});
            }
            if (num + 1 < m && ma[num + 1][s[i]] == 0 && ans[num + 1] == "") {
              k[num + 1]--;
              ma[num + 1][s[i]]++;
              st.erase({k[num + 1] + 1, num + 1});
              st.insert({k[num + 1], num + 1});
            }
            break;
          }
        }
      } else {
        pair<int, int> v = {2, 2};
        for (int i = 0; i < n; i++) {
          if (ma[num][s[i]] == 0) {
            if (num + 1 < m && ma[num + 1][s[i]] == 0 && ans[num + 1] == "") {
              v = min(v, {1, i});
            } else {
              v = min(v, {0, i});
            }
          }
        }
        ans[num] = s[v.second];
        if (num + 1 < m && ma[num + 1][s[v.second]] == 0 &&
            ans[num + 1] == "") {
          k[num + 1]--;
          ma[num + 1][s[v.second]]++;
          st.erase({k[num + 1] + 1, num + 1});
          st.insert({k[num + 1], num + 1});
        }
        if (num && ma[num - 1][s[v.second]] == 0 && ans[num - 1] == "") {
          k[num - 1]--;
          ma[num - 1][s[v.second]]++;
          st.erase({k[num - 1] + 1, num - 1});
          st.insert({k[num - 1], num - 1});
        }
      }
    }
    if (f) {
      cout << "Impossible\n";
      continue;
    }
    for (int i = 0; i < m; i++) {
      if (mes[i][0] == '?') {
        cout << ans[i];
        for (int j = 1; j < mes[i].size() - 1; j++) {
          cout << mes[i][j];
        }
        cout << "\n";
      } else {
        for (int j = 0; j < mes[i].size() - 1; j++) {
          cout << mes[i][j];
        }
        cout << "\n";
      }
    }
  }
}
