#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)])
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            string tmp = "";
            bool flag = false;
            for(int j = 1; j < ro[i].size() ; j++){
                if(!flag){
                    if(ro[i][j] == ':')
                        flag = true;
                    continue;
                }
                if(isalpha(ro[i][j])||(isdigit(ro[i][j])&&tmp.size()))
                    tmp.push_back(ro[i][j]);
                else{
                    if(M[tmp]){
                        got[i].insert(M[tmp] - 1);
                    }
                    tmp.clear();
                }
                if(j+1==ro[i].size()){
                    if(M[tmp]){
                        got[i].insert(M[tmp] - 1);
                    }
                    tmp.clear();
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)]){
                        if((k+j<ro[i].size()) && isalpha(ro[i][k+j])||isdigit(ro[i][k+j]))
                            continue;
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                    }
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)]&&(k+j!=ro[i].size() && !isdigit(ro[i][k+j])))
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {

             if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.' || msg[idx][j-1] == '?') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'|| msg[idx][j+q.size()] == '?' || msg[idx][j+q.size()] == '.'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.' || msg[idx][j-1] == '?') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'|| msg[idx][j+q.size()] == '?' || msg[idx][j+q.size()] == '.'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) {cout<<"Impossible"<<endl; continue;}
       printans(0 , 0);
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
           if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' ||msg[idx][j-1] == '.' ))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' ||msg[idx][j-1] == '.' ))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       name[-1] = "?";
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) {cout<<"Impossible"<<endl; continue;}
       printans(0 , 0);
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) {cout<<"Impossible"<<endl; continue;}
       printans(0 , 0);
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {

            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' || msg[idx][j+q.size()]))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' || msg[idx][j+q.size()]))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) {cout<<"Impossible"<<endl; continue;}
       printans(0 , 0);
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {

            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' ||
              msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' || msg[idx][j+q.size()]=='.'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!' || msg[idx][j+q.size()] == '.'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) {cout<<"Impossible"<<endl; continue;}
       printans(0 , 0);
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int t , n , m;
string s;
map<string ,int>id;
map<int,string>name;
vector<string>msg;
vector<int>vec;
int dp[150][150];
bool solve(int idx ,int pre)
{
    if(idx==m) return 1;
    if(dp[idx][pre] != -1) return dp[idx][pre];
    int & ret = dp[idx][pre];
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x == pre) return ret = 0;
    if(x != -1) return ret = solve(idx + 1 , x);
    set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(notallow.find(vec[i]) != notallow.end()) continue;
        flag |= solve(idx+ 1 , vec[i]);
    }
    return ret = flag;
}
void printans(int idx, int pre)
{
    if(idx==m)  return ;
    string q = msg[idx].substr(0 , msg[idx].find(':'));
    int x = id[q];
    if(x!= -1)
            {
                cout<<msg[idx]<<endl;
                printans(idx+1 , x);
            }
    else
    {
       set<int>notallow;
    notallow.insert(x);
    notallow.insert(pre);
    bool flag = 0;
    for(int i = 0 ; i<vec.size() ; i++)
    {
        if(vec[i] == x || vec[i] == pre) continue;
        q = name[vec[i]];
        int j = msg[idx].find(q);
        while( j!= -1)
        {
            if( (msg[idx][j-1] == ' ' || msg[idx][j-1] == ':' ||  msg[idx][j-1] == ',' ||  msg[idx][j-1] == '!' ||msg[idx][j-1] == '.') &&
            (j+q.size() == msg[idx].size() || (msg[idx][j+q.size()] == ' ' || msg[idx][j+q.size()] == ':' || msg[idx][j+q.size()] == ',' || msg[idx][j+q.size()] == '!'))) notallow.insert(vec[i]);
            j = msg[idx].find(q , j+1);
        }
    }
    for(int i = 0 ; i<vec.size() ; i++)
    {
      if(notallow.find(vec[i]) != notallow.end()) continue;
       if(solve(idx+1 , vec[i]) == 1)
       {
           msg[idx].erase(0 , 1);
           msg[idx] = name[vec[i]] + msg[idx];
           cout<<msg[idx]<<endl;
           printans(idx+1 , vec[i]);
           return;
       }
    }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        id.clear();
        name.clear();
        vec.clear();
        msg.clear();
       for(int i = 1 ; i<=n ; i++)
       {
          string s;
          cin>>s;
          id[s] = i;
          name[i] = s;
          vec.push_back(i);
       }
       cin>>m;
        cin.ignore();

       for(int i = 0 ; i<m ; i++)
       {
           getline(cin , s);
           msg.push_back(s);
       }
       id["?"] = -1;
       memset(dp , -1 , sizeof dp);
       if(!solve(0 , 0)) cout<<"Impossible"<<endl;
       printans(0 , 0);
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        for (int j = 0; j < m - 1; j++) {
            choices[j].erase(senders[j + 1]);
        }
        forf(k, m) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                        if (j > 0) {
                            choices[j - 1].erase(senders[j]);
                        }
                        if (j < m - 1) {
                            choices[j + 1].erase(senders[j]);
                        }
                    } else if (choices[j].size() == 0) {
                        possible = false;
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    senders[j] = *choices[j].begin();
                    if (j < m - 1) {
                        choices[j + 1].erase(senders[j]);
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() - 1 || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, 100) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (j > 0) {
                        choices[j].erase(senders[j - 1]);
                    }
                    if (j < m - 1) {
                        choices[j].erase(senders[j + 1]);
                    }
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                    }
                }
            }
        }
        forf(j, m) {
            if (senders[j].compare("?") == 0) {
                possible = false;
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if (!isAlphanumeric(msgs[j][pos - 1]) && !isAlphanumeric(msgs[j][pos + users[i].length()])) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(j, m) {
            if (senders[j].compare("?") == 0) {
                if (j > 0) {
                    choices[j].erase(senders[j - 1]);
                }
                if (j < m - 1) {
                    choices[j].erase(senders[j + 1]);
                }
                if (choices[j].size() == 1) {
                    senders[j] = *choices[j].begin();
                } else {
                    possible = false;
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() - 1 || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, 100) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (j > 0) {
                        choices[j].erase(senders[j - 1]);
                    }
                    if (j < m - 1) {
                        choices[j].erase(senders[j + 1]);
                    }
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                    } else if (choices[j].size() == 0) {
                        possible = false;
                    }
                }
            }
        }
        forf(j, m) {
            if (senders[j].compare("?") == 0) {
                if (choices[j].size() == 0) {
                    possible = false;
                } else {
                    senders[j] = *choices[j].begin();
                    if (j < m - 1) {
                        choices[j + 1].erase(senders[j]);
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos > 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() < msgs[j].length() || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, 100) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (j > 0) {
                        choices[j].erase(senders[j - 1]);
                    }
                    if (j < m - 1) {
                        choices[j].erase(senders[j + 1]);
                    }
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                    }
                }
            }
        }
        forf(j, m) {
            if (senders[j].compare("?") == 0) {
                possible = false;
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if (!isAlphanumeric(msgs[j][pos - 1]) && !isAlphanumeric(msgs[j][pos + users[i].length()])) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, 100) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (j > 0) {
                        choices[j].erase(senders[j - 1]);
                    }
                    if (j < m - 1) {
                        choices[j].erase(senders[j + 1]);
                    }
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                    }
                }
            }
        }
        forf(j, m) {
            if (senders[j].compare("?") == 0) {
                possible = false;
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() - 1 || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, m) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                        if (j > 0) {
                            choices[j - 1].erase(senders[j]);
                        }
                        if (j < m - 1) {
                            choices[j + 1].erase(senders[j]);
                        }
                    } else if (choices[j].size() == 0) {
                        possible = false;
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    senders[j] = *choices[j].begin();
                    if (j < m - 1) {
                        choices[j + 1].erase(senders[j]);
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() - 1 || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        for (int j = 0; j < m - 1; j++) {
            choices[j].erase(senders[j + 1]);
        }
        forf(k, m) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                        if (j > 0) {
                            choices[j - 1].erase(senders[j]);
                        }
                        if (j < m - 1) {
                            choices[j + 1].erase(senders[j]);
                        }
                    } else if (choices[j].size() == 0) {
                        possible = false;
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    senders[j] = *choices[j].begin();
                    if (j < m - 1) {
                        choices[j + 1].erase(senders[j]);
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define mp make_pair
#define forf(i, n) for (int i = 0; i < n; i++)
#define forb(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vector<string> > vvs;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

bool isAlphanumeric(char c) {
    if (c >= 'A' && c <= 'Z') {
        return true;
    }
    if (c >= 'a' && c <= 'z') {
        return true;
    }
    if (c >= '0' && c <= '9') {
        return true;
    }
    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n;
        vs users(n);
        forf(i, n) {
            cin >> users[i];
        }
        cin >> m;
        cin.ignore(1, '\n');
        vs msgs(m);
        forf(j, m) {
            getline(cin, msgs[j]);
        }
        bool possible = true;
        vs senders(m);
        vector<set<string> > choices(m);
        forf(j, m) {
            senders[j] = msgs[j].substr(0, msgs[j].find(":", 0));
            if (senders[j].compare("?") == 0) {
                forf(i, n) {
                    if (senders[j].compare(users[i]) == 0) {
                        continue;
                    }
                    if (j > 0 && senders[j - 1].compare(users[i]) == 0) {
                        continue;
                    }
                    choices[j].insert(users[i]);
                    size_t pos = msgs[j].find(users[i], 0);
                    while (pos != string::npos) {
                        //cout << msgs[j][pos - 1] << ' ' << msgs[j][pos + users[i].length()] << '\n';
                        if ((pos == 0 || !isAlphanumeric(msgs[j][pos - 1])) && (pos + users[i].length() == msgs[j].length() - 1 || !isAlphanumeric(msgs[j][pos + users[i].length()]))) {
                            choices[j].erase(users[i]);
                            break;
                        }
                        pos = msgs[j].find(users[i], pos + 1);
                    }
                }
            }
        }
        forf(k, 100) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (j > 0) {
                        choices[j].erase(senders[j - 1]);
                    }
                    if (j < m - 1) {
                        choices[j].erase(senders[j + 1]);
                    }
                    if (choices[j].size() == 1) {
                        senders[j] = *choices[j].begin();
                        if (j > 0) {
                            choices[j - 1].erase(senders[j]);
                        }
                        if (j < m - 1) {
                            choices[j + 1].erase(senders[j]);
                        }
                    } else if (choices[j].size() == 0) {
                        possible = false;
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                if (senders[j].compare("?") == 0) {
                    if (choices[j].size() == 0) {
                        possible = false;
                    } else {
                        senders[j] = *choices[j].begin();
                        if (j < m - 1) {
                            choices[j + 1].erase(senders[j]);
                        }
                    }
                }
            }
        }
        if (possible) {
            forf(j, m) {
                cout << senders[j] << msgs[j].substr(msgs[j].find(":", 0)) << '\n';
            }
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
int dp[maxn][maxn];
int ds[maxn][maxn];
int main()
{     memset(dp,0,sizeof(dp));
        int t;
         cin>>t;
              int n;
           for(int i =0;i<t;i++)
           {
               scanf("%d",&n);
                        vector<string> K;
                         vector<string> B;
                    string w;
                for(int k =0;k<n;k++)
                {     cin>>w;
                     K.push_back(w);
                }
                   memset(dp,0,sizeof(dp));
                    memset(ds,-1,sizeof(ds));
                     for(int i =0;i<K.size();i++)
                     {
                         dp[0][i]=1;
                     }
                 int m;
                  scanf("%d",&m);
                    getchar();
                   for(int ks = 0;ks<m;ks++)
                   {
                       getline(cin,w);
                       // cout<<w<<"D";
                       B.push_back(w);
                   }
                      string kw;
                         int kd = 0;
                   for(int d = 1;d<=m;d++)
                   {
                         kw = B[d-1];
                          if(kw[0]!='?')
                          {
                             string q;
                           for(int ib =0;ib<kw.size();ib++)
                           {
                               if(kw[ib]==':')
                               {
                                   break;
                               }
                               else
                               {
                                   q=q+kw[ib];
                               }
                           }
                              int i;
                            for(i =0;i<K.size();i++)
                            {
                                if(K[i]==q)
                                    break;
                            }
                            dp[d][i]=1;
                              int fs=0;
                             for(int is=0;is<K.size();is++)
                             {
                                 if(dp[d-1][is]==1)
                                 {    if(i!=is)
                                     {ds[d][i]=is;
                                        fs=1;
                                     break;
                                     }
                                 }
                             }
                              if(!fs)
                              {
                                  cout<<"Impossible"<<endl;
                                   kd=1;
                                    break;
                              }
                          }
                          else
                          {
                              stringstream C(kw);
                                 string db;
                               while(C>>db)
                               {    if(db[db.size()-1]==','||db[db.size()-1]=='.'||db[db.size()-1]=='!'||db[db.size()-1]=='?')
                                       db = db.substr(0,db.size()-1);
                                      // cout<<db<<' '<<"D";
                                   for(int i =0;i<K.size();i++)
                                   {
                                       if(K[i]==db)
                                        dp[d][i]=-2;
                                   }
                                   // cout<<db<<"B";
                               }
                                 int found = 0;
                                for(int i =0;i<K.size();i++)
                                {
                                    if(dp[d][i]!=-2)
                                    {
                                        for(int ic = 0;ic<K.size();ic++)
                                        {
                                            if(dp[d-1][ic]==1&&ic!=i)
                                            {
                                                dp[d][i]=1;
                                                 ds[d][i]=ic;
                                                   found  = 1;
                                                 continue;
                                            }
                                        }
                                    }
                                }
                                 if(!found)
                                    {cout<<"Impossible"<<endl;
                                     kd=1;}
                          }
                           if(kd)
                            break;
                   }
                    if(kd)
                          continue;
                        else
                        {      int as = 0;
                            for(int i =0;i<K.size();i++)
                            {
                                if(ds[m][i]!=-1)
                                {
                                   as=i;
                                    break;
                                }
                            }
                              stack<int> M;
                               int w =m;
                            while(M.size()<m)
                            {
                                M.push(as);
                                 as=ds[w][as];
                                  w--;
                            }
                             for(int i = 0;i<B.size();i++)
                            {     int D = M.top();
                                   M.pop();
                                if(B[i][0]=='?')
                                 {
                                     cout<<K[D];
                                      for(int vd =1;vd<B[i].size();vd++)
                                      {
                                          cout<<B[i][vd];
                                      }
                                       cout<<endl;
                                 }
                                 else
                                 {
                                     cout<<B[i]<<endl;
                                 }
                             }
                        }
           }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
int dp[maxn][maxn];
int ds[maxn][maxn];
int main()
{     memset(dp,0,sizeof(dp));
        int t;
         cin>>t;
              int n;
           for(int i =0;i<t;i++)
           {
               scanf("%d",&n);
                        vector<string> K;
                         vector<string> B;
                    string w;
                for(int k =0;k<n;k++)
                {     cin>>w;
                     K.push_back(w);
                }
                   memset(dp,0,sizeof(dp));
                    memset(ds,-1,sizeof(ds));
                     for(int i =0;i<K.size();i++)
                     {
                         dp[0][i]=1;
                     }
                 int m;
                  scanf("%d",&m);
                    getchar();
                   for(int ks = 0;ks<m;ks++)
                   {
                       getline(cin,w);
                       // cout<<w<<"D";
                       B.push_back(w);
                   }
                      string kw;
                         int kd = 0;
                   for(int d = 1;d<=m;d++)
                   {
                         kw = B[d-1];
                          if(kw[0]!='?')
                          {
                             string q;
                           for(int ib =0;ib<kw.size();ib++)
                           {
                               if(kw[ib]==':')
                               {
                                   break;
                               }
                               else
                               {
                                   q=q+kw[ib];
                               }
                           }
                              int i;
                            for(i =0;i<K.size();i++)
                            {
                                if(K[i]==q)
                                    break;
                            }
                            dp[d][i]=1;
                              int fs=0;
                             for(int is=0;is<K.size();is++)
                             {
                                 if(dp[d-1][is]==1)
                                 {    if(i!=is)
                                     {ds[d][i]=is;
                                        fs=1;
                                     break;
                                     }
                                 }
                             }
                              if(!fs)
                              {
                                  cout<<"Impossible"<<endl;
                                   kd=1;
                                    break;
                              }
                          }
                          else
                          {
                               for(int i =0;i<kw.size();i++)
                               {
                                   if(kw[i]=='.'||kw[i]==','||kw[i]=='!'||kw[i]=='?')
                                   {
                                       kw[i]=' ';
                                   }
                               }
                              stringstream C(kw);
                                 string db;
                               while(C>>db)
                               {    if(db[db.size()-1]==','||db[db.size()-1]=='.'||db[db.size()-1]=='!'||db[db.size()-1]=='?')
                                       db = db.substr(0,db.size()-1);
                                      // cout<<db<<' '<<"D";
                                   for(int i =0;i<K.size();i++)
                                   {
                                       if(K[i]==db)
                                        dp[d][i]=-2;
                                   }
                                   // cout<<db<<"B";
                               }
                                 int found = 0;
                                for(int i =0;i<K.size();i++)
                                {
                                    if(dp[d][i]!=-2)
                                    {
                                        for(int ic = 0;ic<K.size();ic++)
                                        {
                                            if(dp[d-1][ic]==1&&ic!=i)
                                            {
                                                dp[d][i]=1;
                                                 ds[d][i]=ic;
                                                   found  = 1;
                                                 continue;
                                            }
                                        }
                                    }
                                }
                                 if(!found)
                                    {cout<<"Impossible"<<endl;
                                     kd=1;}
                          }
                           if(kd)
                            break;
                   }
                    if(kd)
                          continue;
                        else
                        {      int as = 0;
                            for(int i =0;i<K.size();i++)
                            {
                                if(ds[m][i]!=-1)
                                {
                                   as=i;
                                    break;
                                }
                            }
                              stack<int> M;
                               int w =m;
                            while(M.size()<m)
                            {
                                M.push(as);
                                 as=ds[w][as];
                                  w--;
                            }
                             for(int i = 0;i<B.size();i++)
                            {     int D = M.top();
                                   M.pop();
                                if(B[i][0]=='?')
                                 {
                                     cout<<K[D];
                                      for(int vd =1;vd<B[i].size();vd++)
                                      {
                                          cout<<B[i][vd];
                                      }
                                       cout<<endl;
                                 }
                                 else
                                 {
                                     cout<<B[i]<<endl;
                                 }
                             }
                        }
           }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
int dp[maxn][maxn];
int ds[maxn][maxn];
int main()
{     memset(dp,0,sizeof(dp));
        int t;
         cin>>t;
              int n;
           for(int i =0;i<t;i++)
           {
               scanf("%d",&n);
                        vector<string> K;
                         vector<string> B;
                    string w;
                for(int k =0;k<n;k++)
                {     cin>>w;
                     K.push_back(w);
                }
                   memset(dp,0,sizeof(dp));
                    memset(ds,-1,sizeof(ds));
                     for(int i =0;i<K.size();i++)
                     {
                         dp[0][i]=1;
                     }
                 int m;
                  scanf("%d",&m);
                    getchar();
                   for(int ks = 0;ks<m;ks++)
                   {
                       getline(cin,w);
                       // cout<<w<<"D";
                       B.push_back(w);
                   }
                      string kw;
                         int kd = 0;
                   for(int d = 1;d<=m;d++)
                   {
                         kw = B[d-1];
                          if(kw[0]!='?')
                          {
                             string q;
                           for(int ib =0;ib<kw.size();ib++)
                           {
                               if(kw[ib]==':')
                               {
                                   break;
                               }
                               else
                               {
                                   q=q+kw[ib];
                               }
                           }
                              int i;
                            for(i =0;i<K.size();i++)
                            {
                                if(K[i]==q)
                                    break;
                            }
                            dp[d][i]=1;
                              int fs=0;
                             for(int is=0;is<K.size();is++)
                             {
                                 if(dp[d-1][is]==1)
                                 {    if(i!=is||d==1)
                                     {ds[d][i]=is;
                                        fs=1;
                                     break;
                                     }
                                 }
                             }
                              if(!fs)
                              {
                                  cout<<"Impossible"<<endl;
                                   kd=1;
                                    break;
                              }
                          }
                          else
                          {
                               for(int i =0;i<kw.size();i++)
                               {
                                   if(kw[i]=='.'||kw[i]==','||kw[i]=='!'||kw[i]=='?'||kw[i]==':')
                                   {
                                       kw[i]=' ';
                                   }
                               }
                              stringstream C(kw);
                                 string db;
                               while(C>>db)
                               {    if(db[db.size()-1]==','||db[db.size()-1]=='.'||db[db.size()-1]=='!'||db[db.size()-1]=='?')
                                       db = db.substr(0,db.size()-1);
                                      // cout<<db<<' '<<"D";
                                   for(int i =0;i<K.size();i++)
                                   {
                                       if(K[i]==db)
                                        dp[d][i]=-2;
                                   }
                                   // cout<<db<<"B";
                               }
                                 int found = 0;
                                for(int i =0;i<K.size();i++)
                                {
                                    if(dp[d][i]!=-2)
                                    {
                                        for(int ic = 0;ic<K.size();ic++)
                                        {
                                            if(dp[d-1][ic]==1&&ic!=i)
                                            {
                                                dp[d][i]=1;
                                                 ds[d][i]=ic;
                                                   found  = 1;
                                                 continue;
                                            }
                                        }
                                    }
                                }
                                 if(!found)
                                    {cout<<"Impossible"<<endl;
                                     kd=1;}
                          }
                           if(kd)
                            break;
                   }
                    if(kd)
                          continue;
                        else
                        {      int as = 0;
                            for(int i =0;i<K.size();i++)
                            {
                                if(ds[m][i]!=-1)
                                {
                                   as=i;
                                    break;
                                }
                            }
                              stack<int> M;
                               int w =m;
                            while(M.size()<m)
                            {
                                M.push(as);
                                 as=ds[w][as];
                                  w--;
                            }
                             for(int i = 0;i<B.size();i++)
                            {     int D = M.top();
                                   M.pop();
                                if(B[i][0]=='?')
                                 {
                                     cout<<K[D];
                                      for(int vd =1;vd<B[i].size();vd++)
                                      {
                                          cout<<B[i][vd];
                                      }
                                       cout<<endl;
                                 }
                                 else
                                 {
                                     cout<<B[i]<<endl;
                                 }
                             }
                        }
           }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 120;
int dp[maxn][maxn];
int ds[maxn][maxn];
int main()
{     memset(dp,0,sizeof(dp));
        int t;
         cin>>t;
              int n;
           for(int i =0;i<t;i++)
           {
               scanf("%d",&n);
                        vector<string> K;
                         vector<string> B;
                    string w;
                for(int k =0;k<n;k++)
                {     cin>>w;
                     K.push_back(w);
                }
                   memset(dp,0,sizeof(dp));
                    memset(ds,-1,sizeof(ds));
                     for(int i =0;i<K.size();i++)
                     {
                         dp[0][i]=1;
                     }
                 int m;
                  scanf("%d",&m);
                    getchar();
                   for(int ks = 0;ks<m;ks++)
                   {
                       getline(cin,w);
                       // cout<<w<<"D";
                       B.push_back(w);
                   }
                      string kw;
                         int kd = 0;
                   for(int d = 1;d<=m;d++)
                   {
                         kw = B[d-1];
                          if(kw[0]!='?')
                          {
                             string q;
                           for(int ib =0;ib<kw.size();ib++)
                           {
                               if(kw[ib]==':')
                               {
                                   break;
                               }
                               else
                               {
                                   q=q+kw[ib];
                               }
                           }
                              int i;
                            for(i =0;i<K.size();i++)
                            {
                                if(K[i]==q)
                                    break;
                            }
                            dp[d][i]=1;
                              int fs=0;
                             for(int is=0;is<K.size();is++)
                             {
                                 if(dp[d-1][is]==1)
                                 {    if(i!=is)
                                     {ds[d][i]=is;
                                        fs=1;
                                     break;
                                     }
                                 }
                             }
                              if(!fs)
                              {
                                  cout<<"Impossible"<<endl;
                                   kd=1;
                                    break;
                              }
                          }
                          else
                          {
                               for(int i =0;i<kw.size();i++)
                               {
                                   if(kw[i]=='.'||kw[i]==','||kw[i]=='!'||kw[i]=='?'||kw[i]==':')
                                   {
                                       kw[i]=' ';
                                   }
                               }
                              stringstream C(kw);
                                 string db;
                               while(C>>db)
                               {    if(db[db.size()-1]==','||db[db.size()-1]=='.'||db[db.size()-1]=='!'||db[db.size()-1]=='?')
                                       db = db.substr(0,db.size()-1);
                                      // cout<<db<<' '<<"D";
                                   for(int i =0;i<K.size();i++)
                                   {
                                       if(K[i]==db)
                                        dp[d][i]=-2;
                                   }
                                   // cout<<db<<"B";
                               }
                                 int found = 0;
                                for(int i =0;i<K.size();i++)
                                {
                                    if(dp[d][i]!=-2)
                                    {
                                        for(int ic = 0;ic<K.size();ic++)
                                        {
                                            if(dp[d-1][ic]==1&&ic!=i)
                                            {
                                                dp[d][i]=1;
                                                 ds[d][i]=ic;
                                                   found  = 1;
                                                 continue;
                                            }
                                        }
                                    }
                                }
                                 if(!found)
                                    {cout<<"Impossible"<<endl;
                                     kd=1;}
                          }
                           if(kd)
                            break;
                   }
                    if(kd)
                          continue;
                        else
                        {      int as = 0;
                            for(int i =0;i<K.size();i++)
                            {
                                if(ds[m][i]!=-1)
                                {
                                   as=i;
                                    break;
                                }
                            }
                              stack<int> M;
                               int w =m;
                            while(M.size()<m)
                            {
                                M.push(as);
                                 as=ds[w][as];
                                  w--;
                            }
                             for(int i = 0;i<B.size();i++)
                            {     int D = M.top();
                                   M.pop();
                                if(B[i][0]=='?')
                                 {
                                     cout<<K[D];
                                      for(int vd =1;vd<B[i].size();vd++)
                                      {
                                          cout<<B[i][vd];
                                      }
                                       cout<<endl;
                                 }
                                 else
                                 {
                                     cout<<B[i]<<endl;
                                 }
                             }
                        }
           }
    return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define MAXM 100

using namespace std;

typedef long long ll;
typedef long double ld;

set<string> usernames;

string msgs[MAXM];
set<string> restrictions[MAXM];
bool expanded[MAXM];

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    int n; scanf("%d\n", &n);

    usernames.clear();
    for(int i = 0; i < n; i++) {
      string user; cin >> user;
      usernames.insert(user);
    }

    int m; scanf("%d\n", &m);
    for(int i = 0; i < m; i++) {
      getline(cin, msgs[i]);

      int sep = msgs[i].find(':');
      string user = msgs[i].substr(0, sep);
      msgs[i] = msgs[i].substr(sep + 1);

      if(user != "?") {
        restrictions[i] = { user };
      }
      else {
        restrictions[i] = usernames;
        for(int prevIdx = -1; prevIdx < (int) msgs[i].size(); ) {
          int idx = msgs[i].find_first_of(".,!? ", ++prevIdx);
          if(idx == -1) idx = msgs[i].size();

          if(idx != prevIdx) {
            string word = msgs[i].substr(prevIdx, idx - prevIdx);
            if(usernames.count(word)) restrictions[i].erase(word);
          }
          prevIdx = idx;
        }
      }
    }

    memset(expanded, false, sizeof(expanded));

    bool impossible = false;
    int expandedCount = 0;
    while(!impossible && expandedCount < m) {
      bool stale = true;
      int first = -1;

      for(int i = 0; i < m; i++) {
        if(!expanded[i] && restrictions[i].size() == 1) {
          if(i > 0) restrictions[i - 1].erase(*restrictions[i].begin());
          if(i < m - 1) restrictions[i + 1].erase(*restrictions[i].begin());
          expanded[i] = true;
          expandedCount++;
          stale = false;
        }
        else if(restrictions[i].empty()) {
          impossible = true;
          break;
        }
        else if(first == -1) {
          first = i;
        }
      }

      if(stale && !impossible) {
        string u = *restrictions[first].begin();
        restrictions[first].clear();
        restrictions[first].insert(u);

        if(first > 0) restrictions[first - 1].erase(u);
        if(first < m - 1) restrictions[first + 1].erase(u);
        expanded[first] = true;
        expandedCount++;
      }
    }

    if(impossible) printf("Impossible\n");
    else {
      for(int i = 0; i < m; i++) {
        printf("%s:%s\n", restrictions[i].begin()->c_str(), msgs[i].c_str());
      }
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define MAXM 100

using namespace std;

typedef long long ll;
typedef long double ld;

int m;
set<string> usernames;

string msgs[MAXM];
set<string> restrictions[MAXM];
bool expanded[MAXM];

bool expand(int i) {
  if(!expanded[i] && restrictions[i].size() == 1) {
    expanded[i] = true;

    string user = *restrictions[i].begin();
    if(i > 0) {
      restrictions[i - 1].erase(user);
      if(!expand(i - 1)) return false;
    }
    if(i < m - 1) {
      restrictions[i + 1].erase(user);
      if(!expand(i + 1)) return false;
    }
  }
  return !restrictions[i].empty();
}

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    int n; scanf("%d\n", &n);

    usernames.clear();
    for(int i = 0; i < n; i++) {
      string user; cin >> user;
      usernames.insert(user);
    }

    scanf("%d\n", &m);
    for(int i = 0; i < m; i++) {
      getline(cin, msgs[i]);

      int sep = msgs[i].find(':');
      string user = msgs[i].substr(0, sep);
      msgs[i] = msgs[i].substr(sep + 1);

      if(user != "?") {
        restrictions[i] = { user };
      }
      else {
        restrictions[i] = usernames;
        for(int prevIdx = -1; prevIdx < (int) msgs[i].size(); ) {
          int idx = msgs[i].find_first_of(".,!? ", ++prevIdx);
          if(idx == -1) idx = msgs[i].size();

          if(idx != prevIdx) {
            string word = msgs[i].substr(prevIdx, idx - prevIdx);
            if(usernames.count(word)) restrictions[i].erase(word);
          }
          prevIdx = idx;
        }
      }
    }

    memset(expanded, false, sizeof(expanded));
    bool impossible = false;
    for(int i = 0; i < m; i++) {
      if(!expand(i)) { impossible = true; break; }
    }

    if(impossible) printf("Impossible\n");
    else {
      for(int i = 0; i < m; i++) {
        string user = *restrictions[i].begin();
        if(i < m - 1) restrictions[i + 1].erase(user);
        printf("%s:%s\n", user.c_str(), msgs[i].c_str());
      }
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define MAXM 100

using namespace std;

typedef long long ll;
typedef long double ld;

set<string> usernames;

string msgs[MAXM];
set<string> restrictions[MAXM];
bool expanded[MAXM];

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    int n; scanf("%d\n", &n);

    usernames.clear();
    for(int i = 0; i < n; i++) {
      string user; cin >> user;
      usernames.insert(user);
    }

    int m; scanf("%d\n", &m);
    for(int i = 0; i < m; i++) {
      getline(cin, msgs[i]);

      int sep = msgs[i].find(':');
      string user = msgs[i].substr(0, sep);
      msgs[i] = msgs[i].substr(sep + 1);

      if(user != "?") {
        restrictions[i] = { user };
      }
      else {
        restrictions[i] = usernames;
        for(int prevIdx = -1; prevIdx < (int) msgs[i].size(); ) {
          int idx = msgs[i].find_first_of(".,!? ", ++prevIdx);
          if(idx == -1) idx = msgs[i].size();

          if(idx != prevIdx) {
            string word = msgs[i].substr(prevIdx, idx - prevIdx);
            if(usernames.count(word)) restrictions[i].erase(word);
          }
          prevIdx = idx;
        }
      }
    }

    memset(expanded, false, sizeof(expanded));

    bool impossible = false;
    int expandedCount = 0;
    while(!impossible && expandedCount < m) {
      bool stale = true;
      int first = -1;

      for(int i = 0; i < m; i++) {
        if(!expanded[i] && restrictions[i].size() == 1) {
          if(i > 0) restrictions[i - 1].erase(*restrictions[i].begin());
          if(i < m - 1) restrictions[i + 1].erase(*restrictions[i].begin());
          expanded[i] = true;
          expandedCount++;
          stale = false;
        }
        else if(restrictions[i].empty()) {
          impossible = true;
          break;
        }
        else if(first == -1) {
          first = i;
        }
      }

      if(stale && !impossible) {
        string u = *restrictions[first].begin();
        restrictions[first].clear();
        restrictions[first].insert(u);
      }
    }

    if(impossible) printf("Impossible\n");
    else {
      for(int i = 0; i < m; i++) {
        printf("%s:%s\n", restrictions[i].begin()->c_str(), msgs[i].c_str());
      }
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define MAXM 100

using namespace std;

typedef long long ll;
typedef long double ld;

set<string> usernames;

string msgs[MAXM];
set<string> restrictions[MAXM];
bool expanded[MAXM];

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    int n; scanf("%d\n", &n);

    usernames.clear();
    for(int i = 0; i < n; i++) {
      string user; cin >> user;
      usernames.insert(user);
    }

    int m; scanf("%d\n", &m);
    for(int i = 0; i < m; i++) {
      getline(cin, msgs[i]);

      int sep = msgs[i].find(':');
      string user = msgs[i].substr(0, sep);
      msgs[i] = msgs[i].substr(sep + 1);

      if(user != "?") {
        restrictions[i] = { user };
      }
      else {
        restrictions[i] = usernames;
        for(int prevIdx = -1; prevIdx < (int) msgs[i].size(); ) {
          int idx = msgs[i].find_first_of(".,!? ", ++prevIdx);
          if(idx == -1) idx = msgs[i].size();

          if(idx != prevIdx) {
            string word = msgs[i].substr(prevIdx, idx - prevIdx);
            if(usernames.count(word)) restrictions[i].erase(word);
          }
          prevIdx = idx;
        }
      }
    }

    memset(expanded, false, sizeof(expanded));

    bool impossible = false;
    int expandedCount = 0;
    while(!impossible && expandedCount < m) {
      bool stale = true;
      int first = -1;

      for(int i = 0; i < m; i++) {
        if(!expanded[i] && restrictions[i].size() == 1) {
          if(i > 0) restrictions[i - 1].erase(*restrictions[i].begin());
          if(i < m - 1) restrictions[i + 1].erase(*restrictions[i].begin());
          expanded[i] = true;
          expandedCount++;
          stale = false;
        }
        else if(restrictions[i].empty()) {
          impossible = true;
          break;
        }
        else if(first == -1) {
          first = i;
        }
      }

      if(stale && !impossible) {
        string u = *restrictions[first].begin();
        if(first > 0) restrictions[first - 1].erase(u);
        if(first < m - 1) restrictions[first + 1].erase(u);
        expanded[first] = true;
        expandedCount++;
      }
    }

    if(impossible) printf("Impossible\n");
    else {
      for(int i = 0; i < m; i++) {
        printf("%s:%s\n", restrictions[i].begin()->c_str(), msgs[i].c_str());
      }
    }
  }
  return 0;
}