#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &cout, vector<T> &a) {
  for (unsigned long long i = 0; i < a.size(); ++i) cout << a[i] << " ";
  return cout;
}
template <typename T>
ostream &operator<<(ostream &cout, vector<vector<T> > &a) {
  for (unsigned long long i = 0; i < a.size(); ++i) cout << a[i] << endl;
  return cout;
}
string names[100];
string s[100];
bool authors[100][100];
int named[100];
int n;
string author(string &r) {
  int i = 0;
  while (r[i] != ':') ++i;
  string b = r;
  r.erase(0, i);
  return b.erase(i, b.size() - 1);
}
void delName(string name, int i) {
  auto d = lower_bound(names, names + n, name);
  if (d != names + n && (*d).compare(name) == 0) {
    authors[i][d - names] = 0;
  }
}
void exAuthors(int i) {
  string name = "";
  for (int j = 0; j < s[i].size(); ++j) {
    if (isalnum(s[i][j])) {
      name.push_back(s[i][j]);
    } else {
      if (!name.empty()) {
        delName(name, i);
        name = "";
      }
    }
  }
  if (!name.empty()) delName(name, i);
}
int main() {
  int T;
  cin >> T;
  int m;
  string a;
  int an;
  while (T--) {
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < 100; ++j) {
        authors[i][j] = 0;
      }
    }
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> names[i];
    sort(names, names + n);
    cin >> m;
    scanf("\n");
    for (int i = 0; i < m; ++i) {
      getline(cin, s[i]);
    }
    for (int i = 0; i < m; ++i) {
      a = author(s[i]);
      if (a[0] != '?') {
        an = lower_bound(names, names + n, a) - names;
        authors[i][an] = 1;
      } else {
        for (int j = 0; j < n; ++j) {
          authors[i][j] = 1;
        }
        exAuthors(i);
      }
    }
    int k = 0, d;
    for (int i = 1; i < m; ++i) {
      k = 0;
      d = -1;
      for (int j = 0; j < n; ++j) {
        if (authors[i - 1][j]) {
          ++k;
          d = j;
        }
      }
      if (k == 1) {
        authors[i][d] = 0;
      } else if (k == 0) {
        cout << "Impossible" << endl;
        k = -1;
        break;
      }
    }
    if (k == -1) continue;
    for (int i = m - 2; i >= 0; --i) {
      k = 0;
      d = -1;
      for (int j = 0; j < n; ++j) {
        if (authors[i + 1][j]) {
          ++k;
          d = j;
        }
      }
      if (k == 1) {
        authors[i][d] = 0;
      } else if (k == 0) {
        cout << "Impossible" << endl;
        k = -1;
        break;
      }
    }
    if (k == -1) continue;
    k = 0;
    for (int i = 0; i < n; ++i) {
      if (authors[0][i]) {
        ++k;
      }
    }
    if (k == 0) {
      cout << "Impossible" << endl;
      continue;
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (authors[i][j]) {
          if (i + 1 < m) authors[i + 1][j] = 0;
          cout << names[j] << s[i] << endl;
          break;
        }
      }
    }
  }
  return 0;
}
