#include <bits/stdc++.h>
using namespace std;
int t, n, m;
bool ok(char c) { return !isdigit(c) && !isalpha(c); }
int main() {
  cin >> t;
  while (t--) {
    vector<string> user;
    pair<string, string> chat[101];
    map<int, vector<string> > M;
    bool f = false;
    int unknown = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      user.insert(user.end(), s);
    }
    cin >> m;
    char c;
    for (int i = 0; i < m; i++) {
      while (cin >> c) {
        chat[i].first.push_back(c);
        if (c == ':') break;
      }
      getline(cin, chat[i].second);
      if (chat[i].first == "?:") {
        unknown++;
        for (int j = 0; j < n; j++)
          for (int l = 0; l + user[j].length() <= chat[i].second.length(); l++)
            if (l == 0 || ok(chat[i].second[l - 1]))
              if (user[j].length() + l == chat[i].second.length() ||
                  ok(chat[i].second[user[j].length() + l]))
                if (chat[i].second.substr(l, user[j].length()) == user[j])
                  if (find(M[i].begin(), M[i].end(), user[j]) == M[i].end())
                    M[i].push_back(user[j]);
      }
    }
    for (int i = 0; i < m; i++) {
      if (chat[i].first != "?:") {
        if (i) {
          string t = chat[i].first;
          t.erase(t.end() - 1);
          bool r = false;
          for (int j = 0; j < M[i - 1].size(); j++)
            if (M[i - 1][j] == t) r = true;
          if (!r) M[i - 1].push_back(t);
        }
        if (i < m - 1) {
          string t = chat[i].first;
          t.erase(t.end() - 1);
          bool r = false;
          for (int j = 0; j < M[i + 1].size(); j++)
            if (M[i + 1][j] == t) r = true;
          if (!r) M[i + 1].push_back(t);
        }
      }
    }
    while (unknown) {
      bool yra1 = false;
      for (int i = 0; i < m; i++) {
        if (chat[i].first == "?:") {
          if (M[i].size() == n) {
            cout << "Impossible" << endl;
            f = true;
            break;
          } else if (M[i].size() == n - 1) {
            unknown--;
            yra1 = true;
            for (int j = 0; j < n; j++) {
              bool r = false;
              for (int k = 0; k < M[i].size(); k++) {
                if (M[i][k] == user[j]) {
                  r = true;
                  break;
                }
              }
              if (!r) {
                chat[i].first = user[j];
                chat[i].first.push_back(':');
                M[i].insert(M[i].end(), user[j]);
                break;
              }
            }
            if (i) {
              string t = chat[i].first;
              t.erase(t.end() - 1);
              bool r = false;
              for (int j = 0; j < M[i - 1].size(); j++)
                if (M[i - 1][j] == t) r = true;
              if (!r) M[i - 1].push_back(t);
            }
            if (i < m - 1) {
              string t = chat[i].first;
              t.erase(t.end() - 1);
              bool r = false;
              for (int j = 0; j < M[i + 1].size(); j++)
                if (M[i + 1][j] == t) r = true;
              if (!r) M[i + 1].push_back(t);
            }
          }
        }
      }
      if (!yra1) {
        for (int i = 0; i < m; i++) {
          if (chat[i].first == "?:") {
            unknown--;
            for (int j = 0; j < n; j++) {
              bool r = false;
              for (int k = 0; k < M[i].size(); k++) {
                if (M[i][k] == user[j]) {
                  r = true;
                  break;
                }
              }
              if (!r) {
                chat[i].first = user[j];
                chat[i].first.push_back(':');
                M[i].insert(M[i].end(), user[j]);
                break;
              }
            }
            if (i) {
              string t = chat[i].first;
              t.erase(t.end() - 1);
              bool r = false;
              for (int j = 0; j < M[i - 1].size(); j++)
                if (M[i - 1][j] == t) r = true;
              if (!r) M[i - 1].push_back(t);
            }
            if (i < m - 1) {
              string t = chat[i].first;
              t.erase(t.end() - 1);
              bool r = false;
              for (int j = 0; j < M[i + 1].size(); j++)
                if (M[i + 1][j] == t) r = true;
              if (!r) M[i + 1].push_back(t);
            }
          }
        }
      }
      if (f) break;
    }
    if (!f)
      for (int i = 0; i < m; i++)
        cout << chat[i].first << chat[i].second << endl;
  }
  return 0;
}
