#include <bits/stdc++.h>
volatile bool isLocalTestEnabled = 0;
bool g_isLocalPrintEnabled = (bool)(0);
template <typename T>
void UpdateMin(T& a, const T b) {
  a = std::min(a, b);
}
template <typename T>
void UpdateMax(T& a, const T b) {
  a = std::max(a, b);
}
const long double Pi = std::atan(1.0L) * 4.0L;
static const long double Eps = 1.0e-09;
template <typename T>
bool IsEqual(const T a, const T b) {
  return std::abs(a - b) < Eps;
}
template <typename T>
bool IsGreater(const T a, const T b) {
  return a > b + Eps;
}
template <typename T>
bool IsLess(const T a, const T b) {
  return a + Eps < b;
}
template <typename T>
bool IsGreaterEqual(const T a, const T b) {
  return !IsLess(a, b);
}
template <typename T>
bool IsLessEqual(const T a, const T b) {
  return !IsGreater(a, b);
}
template <typename T>
std::string ToStr(const T& val) {
  std::ostringstream ostr;
  ostr << val;
  return ostr.str();
}
template <typename T>
bool FromStr(const std::string& str, T& val) {
  std::istringstream istr(str);
  istr >> val;
  return !!istr;
}
template <typename T>
std::istream& operator>>(std::istream& ist, std::vector<T>& data) {
  ;
  for (size_t i = 0; i < data.size(); i++) {
    ist >> data[i];
  }
  return ist;
}
template <typename T>
T Read(std::istream& ist) {
  ;
  T val;
  ist >> val;
  return val;
}
template <typename T>
std::ostream& operator<<(std::ostream& ost, const std::vector<T>& data) {
  for (size_t i = 0; i < data.size(); i++) {
    if (i != 0) {
      ost << ' ';
    }
    ost << data[i];
  }
  return ost;
}
template <size_t id>
class StopWatch {};
bool IsPart(const char c) {
  if (c >= '0' && c <= '9') return true;
  if (c >= 'a' && c <= 'z') return true;
  if (c >= 'A' && c <= 'Z') return true;
  return false;
}
bool IsMentioned(const std::string& line, const std::string& user) {
  size_t p = 0;
  while (p + user.size() <= line.size()) {
    if (p != 0 && IsPart(line[p - 1])) {
      p += 1;
      continue;
    }
    if ((p + user.size() < line.size()) && IsPart(line[p + user.size()])) {
      p += 1;
      continue;
    }
    if (line.substr(p, user.size()) != user) {
      p += 1;
      continue;
    }
    return true;
  }
  return false;
}
std::vector<std::string> GetAns(const std::vector<std::string>& users,
                                const std::vector<std::string>& chat) {
  const size_t n = users.size();
  const size_t m = chat.size();
  std::vector<std::string> text(m);
  std::vector<std::vector<size_t> > isAllowed(m, std::vector<size_t>(n));
  for (size_t i = 0; i < m; i++) {
    const std::string& line = chat[i];
    const size_t dotPos = line.find(':');
    ;
    const std::string userName = line.substr(0, dotPos);
    text[i] = line.substr(dotPos + 1);
    if (userName == "?") {
      for (size_t j = 0; j < n; j++) {
        if (!IsMentioned(text[i], users[j])) {
          isAllowed[i][j] = 1;
        }
      }
    } else {
      const size_t userId =
          std::find(users.begin(), users.end(), userName) - users.begin();
      ;
      isAllowed[i][userId] = 1;
    }
  }
  std::vector<std::vector<size_t> > dp(m + 1, std::vector<size_t>(n, n));
  for (size_t j = 0; j < n; j++) {
    dp[0][j] = 0;
  }
  for (size_t j = 0; j < n; j++) {
    if (!isAllowed[0][j]) continue;
    dp[1][j] = 0;
  }
  for (size_t i = 1; i < m; i++) {
    if (!g_isLocalPrintEnabled) {
    } else
      std::cerr << "";
    for (size_t j = 0; j < n; j++) {
      if (!isAllowed[i][j]) {
        dp[i + 1][j] = n;
        continue;
      }
      for (size_t k = 0; k < n; k++) {
        if (j == k) {
          continue;
        }
        if (dp[i][k] >= n) {
          continue;
        }
        dp[i + 1][j] = k;
        break;
      }
    }
  }
  bool hasAns = false;
  size_t current = n;
  for (size_t i = 0; i < n; i++) {
    if (dp[m][i] < n) {
      hasAns = true;
      current = i;
      break;
    }
  }
  if (!hasAns) {
    std::vector<std::string> imp;
    imp.push_back("Impossible");
    return imp;
  }
  std::vector<size_t> assignedUsers(m, n);
  for (size_t ri = m; ri > 0; ri--) {
    ;
    assignedUsers[ri - 1] = current;
    current = dp[ri][current];
  }
  std::vector<std::string> ans(m);
  for (size_t i = 0; i < m; i++) {
    ans[i] = users[assignedUsers[i]] + ":" + text[i];
  }
  return ans;
}
bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) {
  StopWatch<1> sw;
  (void)sw;
  size_t t;
  ist >> t;
  if (multipleTestMode && !ist) return false;
  if (!g_isLocalPrintEnabled) {
  } else
    std::cerr << std::endl << "Next test" << std::endl;
  for (size_t tt = 0; tt < t; tt++) {
    size_t n;
    ist >> n;
    std::vector<std::string> users(n);
    ist >> users;
    size_t m;
    ist >> m;
    std::string s;
    std::getline(ist, s);
    std::vector<std::string> chat;
    for (size_t i = 0; i < m; i++) {
      std::getline(ist, s);
      chat.push_back(s);
    }
    if (!g_isLocalPrintEnabled) {
    } else
      std::cerr << "Chat #" << (tt + 1) << " of " << t << std::endl;
    if (!g_isLocalPrintEnabled) {
    } else
      std::cerr << "-----" << std::endl;
    if (!g_isLocalPrintEnabled) {
    } else
      std::cerr << users << std::endl;
    for (size_t i = 0; i < chat.size(); i++) {
      if (!g_isLocalPrintEnabled) {
      } else
        std::cerr << chat[i] << std::endl;
    }
    if (!g_isLocalPrintEnabled) {
    } else
      std::cerr << "-----" << std::endl;
    std::vector<std::string> ans = GetAns(users, chat);
    for (size_t i = 0; i < ans.size(); i++) {
      ost << ans[i] << std::endl;
    }
  }
  return multipleTestMode;
}
void RunSolve(std::istream& ist, std::ostream& ost) { Solve(ist, ost, false); }
int main(int argc, const char* argv[]) {
  std::ios_base::sync_with_stdio(false);
  if (argc > 1) {
    std::ifstream ifs(argv[1]);
    if (!ifs) {
      std::cout << "File not found: " << argv[1] << std::endl;
      return 1;
    }
    RunSolve(ifs, std::cout);
    return 0;
  }
  RunSolve(std::cin, std::cout);
}
