#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    vector<int> v;
    int cnt = 0;
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                anss[s1[i]] = 1;
                v.pb(i);
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                if(anss[s2[i]] != 0){
                    printf("NO\n");
                    return 0;
                }
                else{
                    anss[s2[i]] = 1;
                }
            }
            else{
                if(v.empty()){
                    printf("NO\n");
                    return 0;
                }
                else{
                    i = v[cnt++];
                    anss[s1[i]] = 0;
                    for(int it = cnt - 1; it < v.size(); i++){
                        ans[i].resize(0);
                    }
                }
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> rep;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(rep[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                rep[ans[i]] = 1;
            }
        }
    }
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(rep[s1[i]] == 0){
                ans[i] = s1[i];
            }
            else{
                if(rep[s2[i]] == 0){
                    ans[i] = s2[i];
                }
                else{
                    printf("NO\n");
                    return 0;
                }
            }
            rep[ans[i]] = 1;
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        if(mp1[s1[i]] && mp2[s2[i]]){
            printf("NO");
            return 0;
        }
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            anss[ans[i]] = 1;
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else{
                printf("NO");
                return 0;
            }
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            anss[ans[i]] = 1;
            if(anss[s2[i]] == 0)ans[i] = s2[i];
            else{
                printf("NO");
                return 0;
            }
        }
        else{
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else ans[i] = s2[i];
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        if(mp1[s1[i]] && mp2[s2[i]]){
            printf("NO");
            return 0;
        }
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else{
                printf("NO");
                return 0;
            }
            anss[ans[i]] = 1;
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            if(anss[s2[i]] == 0)ans[i] = s2[i];
            else{
                printf("NO");
                return 0;
            }
            anss[ans[i]] = 1;
        }
        else{
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                anss[ans[i]] = 1;
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                anss[ans[i]] = 1;

            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    //freopen("in.txt", "r",stdin);
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    vector<int> v;
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(anss[s1[i]]){
                ans[i] = s2[i];
            }
            else{
                ans[i] = s1[i];
            }
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    freopen("in.txt", "r",stdin);
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    vector<int> v;
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(anss[s1[i]]){
                ans[i] = s2[i];
            }
            else{
                ans[i] = s1[i];
            }
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;
map<string, int> blo;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            mp2[s2[i]]--;
            if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else{
                printf("NO");
                return 0;
            }
            blo[s1[i]] = 1;
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            if(anss[s2[i]] == 0)ans[i] = s2[i];
            if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
            else{
                printf("NO");
                return 0;
            }
            anss[ans[i]] = 1;
            blo[s1[i]] = 1;
        }
        else{
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
                anss[ans[i]] = 1;
                mp2[s2[i]]--;
                blo[s1[i]] = 1;
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                anss[ans[i]] = 1;
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> rep;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    int cnt = 0, now = 0;
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(rep[ans[i]] == 0){
                rep[ans[i]] = 1;
                cnt++, now++;
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    int j = 0;
    do{
        now = cnt;
        for(int i = 0; i < n; i++){
            if(ans[i].size() == 0){
                if(rep[s1[i]] != 0){
                    ans[i] = s2[i];
                    cnt++;
                }
                if(rep[s2[i]] != 0){
                    printf("NO\n");
                    return 0;
                }
                rep[ans[i]] = 1;
            }
        }
        j++;
    }while(j != 1001);
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            ans[i] = s1[i];
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> rep;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    int cnt = 0, now = 0;
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(rep[ans[i]] == 0){
                rep[ans[i]] = 1;
                cnt++, now++;
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    do{
        now = cnt;
        for(int i = 0; i < n; i++){
            if(ans[i].size() == 0){
                if(rep[s1[i]] != 0){
                    ans[i] = s2[i];
                    cnt++;
                }
                if(rep[s2[i]] != 0){
                    printf("NO\n");
                    return 0;
                }
                rep[ans[i]] = 1;
            }
        }
    }while(now != cnt);
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            ans[i] = s1[i];
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    vector<int> v;
    int cnt = 0;
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                anss[s1[i]] = 1;
                v.pb(i);
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                if(anss[s2[i]] != 0){
                    printf("NO\n");
                    return 0;
                }
                else{
                    anss[s2[i]] = 1;
                }
            }
            else{
                if(v.empty()){
                    printf("NO\n");
                    return 0;
                }
                else{
                    i = v[cnt++];
                    for(int it = cnt - 1; it < v.size(); it++){
                        anss[ans[it]] = 0;
                        ans[it].resize(0);
                    }
                    i--;
                }
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        if(mp1[s1[i]] && mp2[s2[i]]){
            printf("NO");
            return 0;
        }
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            anss[ans[i]] = 1;
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            anss[ans[i]] = 1;
        }
        else{
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else ans[i] = s2[i];
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] != 1){
            ans[i] = s2[i];
            if(anss[ans[i]] != 0){
                printf("NO\n");
                return 0;
            }
            else{
                anss[ans[i]] = 1;
            }
        }
    }
    vector<int> v;
    int cnt = 0;
    for(int i = 0; i < n; i++){
        if(ans[i].size() == 0){
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                anss[s1[i]] = 1;
                v.pb(i);
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                if(anss[s2[i]] != 0){
                    printf("NO\n");
                    return 0;
                }
                else{
                    anss[s2[i]] = 1;
                }
            }
            else{
                if(cnt == v.size()){
                    printf("NO\n");
                    return 0;
                }
                else{
                    i = v[cnt++];
                    for(int it = cnt - 1; it < v.size(); it++){
                        anss[ans[it]] = 0;
                        ans[it].resize(0);
                    }
                    i--;
                }
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;
map<string, int> blo;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            mp2[s2[i]]--;
            if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else{
                printf("NO");
                return 0;
            }
            blo[s1[i]] = 1;
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            if(anss[s2[i]] == 0)ans[i] = s2[i];
            else{
                printf("NO");
                return 0;
            }
            anss[ans[i]] = 1;
            blo[s1[i]] = 1;
        }
        else{
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
                anss[ans[i]] = 1;
                mp2[s2[i]]--;
                blo[s1[i]] = 1;
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                anss[ans[i]] = 1;
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP1(i, n) for(int (i)=1;(i)<=(int)(n);(i)++)

template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}
string a[2000];
string b[2000];
string ans[2000];
string s1[2000];
string s2[2000];
map<string, int> mp1;
map<string, int> mp2;
map<string, int> anss;
map<string, int> blo;

int main()
{
    int n;
    cin >> n;
    scanf("\n");
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cin >> b[i];
        s1[i].pb(a[i][0]);
        s1[i].pb(a[i][1]);
        s1[i].pb(a[i][2]);
        s2[i].pb(a[i][0]);
        s2[i].pb(a[i][1]);
        s2[i].pb(b[i][0]);
        if(mp1[s1[i]] && mp2[s2[i]]){
            printf("NO");
            return 0;
        }
        mp1[s1[i]]++;
        mp2[s2[i]]++;
    }
    for(int i = 0; i < n; i++){
        if(mp1[s1[i]] == 1 && mp2[s2[i]] != 1){
            ans[i] = s1[i];
            mp2[s2[i]]--;
            if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
            if(anss[s1[i]] == 0)ans[i] = s1[i];
            else{
                printf("NO");
                return 0;
            }
            blo[s1[i]] = 1;
        }
        else if(mp1[s1[i]] != 1 && mp2[s2[i]] == 1){
            ans[i] = s2[i];
            if(anss[s2[i]] == 0)ans[i] = s2[i];
            else{
                printf("NO");
                return 0;
            }
            anss[ans[i]] = 1;
            blo[s1[i]] = 1;
        }
        else{
            if(anss[s1[i]] == 0){
                ans[i] = s1[i];
                if(blo[s1[i]] == 1){
                printf("NO");
                return 0;
            }
                anss[ans[i]] = 1;
                mp2[s2[i]]--;
                blo[s1[i]] = 1;
            }
            else if(anss[s2[i]] == 0){
                ans[i] = s2[i];
                anss[ans[i]] = 1;
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <stdint.h>
#include <cmath>
using namespace std;

struct Solution
{
	int n, k;
	map<int, vector<string> > g;
	map<string, int> mt;
	vector<char> used;

	bool try_kuhn(int v)
	{
		if (used[v])
			return false;
		used[v] = true;
		for (size_t i = 0; i < g[v].size(); ++i)
		{
			string to = g[v][i];
			if (mt.count(to) == 0 || try_kuhn(mt[to]))
			{
				mt[to] = v;
				return true;
			}
		}
		return false;
	}

	void solve()
	{
		int n;
		cin >> n;
		vector<string> x(n), y(n);
		map<string, vector<int> > q_tmp;
		for (int i = 0; i < n; ++i)
		{
			cin >> x[i] >> y[i];
			q_tmp[x[i].substr(0, 3)].push_back(i);
		}

		map<string, vector<int> > q;
		vector<int> qu;

		for (auto & t : q_tmp)
		{
			if (t.second.size() == 1)
			{
				qu.push_back(t.second.front());
			}
			else
			{
				q.insert(t);
			}
		}

		set<string> w;
		vector<string> ans(n);

		for (auto & t : q)
		{
			for (auto & j : t.second)
			{
				string tmp = x[j].substr(0, 2) + y[j][0];
				if (w.count(tmp))
				{
					cout << "NO" << endl;
					return;
				}
				ans[j] = tmp;
				w.insert(tmp);
			}
		}
		for (auto & t : qu)
		{
			string tmp1 = x[t].substr(0, 3);
			string tmp2 = x[t].substr(0, 2) + y[t][0];
			if (!w.count(tmp1))
				g[t].push_back(tmp1);
			if (!w.count(tmp2))
				g[t].push_back(tmp2);
		}

		for (int v = 0; v < n; ++v)
		{
			used.assign(n, false);
			try_kuhn(v);
		}

		for (auto & t : mt)
		{
			ans[t.second] = t.first;
		}

		for (auto & t : ans)
		{
			if (t.empty())
			{
				cout << "NO" << endl;
				return;
			}
		}

		cout << "YES" << endl;
		for (auto & t : ans)
		{
			cout << t << endl;
		}
	}
};

int main(void)
{

	ios_base::sync_with_stdio(false);
	Solution().solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <stdint.h>
#include <cmath>
using namespace std;

struct Solution
{
	void solve()
	{
		int n;
		cin >> n;
		vector<string> x(n), y(n);
		map<string, vector<int> > q;
		for (int i = 0; i < n; ++i)
		{
			cin >> x[i] >> y[i];
			q[x[i].substr(0, 3)].push_back(i);
		}
		set<string> w;
		vector<string> ans(n);
		for (auto & t : q)
		{
			if (t.second.size() == 1)
				continue;
			for (auto & j : t.second)
			{
				string tmp = x[j].substr(0, 2) + y[j][0];
				if (w.count(tmp))
				{
					cout << "NO" << endl;
					return;
				}
				ans[j] = tmp;
				w.insert(tmp);
			}
		}
		queue<int> p;

		auto check = [&]
		{
			for (auto & t : q)
			{
				if (t.second.size() != 1)
					continue;
				for (auto & j : t.second)
				{
					if (!ans[j].empty())
						continue;

					string tmp1 = x[j].substr(0, 3);
					string tmp2 = x[j].substr(0, 2) + y[j][0];
					int e = 0;
					if (!w.count(tmp1))
					{
						++e;
					}

					if (!w.count(tmp2))
					{
						++e;
					}

					if (e == 1)
						p.push(j);
				}
			}
		};

		auto go = [&]
		{
			check();
			while (!p.empty())
			{
				int j = p.front();
				p.pop();

				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];

				if (!w.count(tmp1))
				{
					ans[j] = tmp1;
					w.insert(tmp1);
					check();
					continue;
				}

				if (!w.count(tmp2))
				{
					ans[j] = tmp2;
					w.insert(tmp2);
					check();
					continue;
				}

				cout << "NO" << endl;
				return;
			}
		};
		for (auto & t : q)
		{
			if (t.second.size() != 1)
				continue;
			for (auto & j : t.second)
			{
				go();

				if (!ans[j].empty())
					continue;

				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];
				if (!w.count(tmp1))
				{
					ans[j] = tmp1;
					w.insert(tmp1);
					continue;
				}

				if (!w.count(tmp2))
				{
					ans[j] = tmp2;
					w.insert(tmp2);
					continue;
				}

				cout << "NO" << endl;
				return;
			}
		}

		cout << "YES" << endl;
		for (auto & t : ans)
		{
			cout << t << endl;
		}
	}
};

int main(void)
{

	ios_base::sync_with_stdio(false);
	Solution().solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <stdint.h>
#include <cmath>
using namespace std;

struct Solution
{
	void solve()
	{
		int n;
		cin >> n;
		vector<string> x(n), y(n);
		map<string, vector<int> > q_tmp;
		for (int i = 0; i < n; ++i)
		{
			cin >> x[i] >> y[i];
			q_tmp[x[i].substr(0, 3)].push_back(i);
		}

		map<string, vector<int> > q;
		vector<int> qu;

		for (auto & t : q_tmp)
		{
			if (t.second.size() == 1)
			{
				qu.push_back(t.second.front());
			}
			else
			{
				q.insert(t);
			}
		}

		set<string> w;
		vector<string> ans(n);

		for (auto & t : q)
		{
			for (auto & j : t.second)
			{
				string tmp = x[j].substr(0, 2) + y[j][0];
				if (w.count(tmp))
				{
					cout << "NO" << endl;
					return;
				}
				ans[j] = tmp;
				w.insert(tmp);
			}
		}

		map<int, vector<string> > r;
		for (auto & t : qu)
		{
			string tmp1 = x[t].substr(0, 3);
			string tmp2 = x[t].substr(0, 2) + y[t][0];
			r[t].push_back(tmp1);
			r[t].push_back(tmp2);
		}

		std::function<bool (int)> dfs = [&](int v) -> bool
		{
			if (v == n)
				return true;

			bool is = false;

			for (auto & t : r[v])
			{
				if (!w.count(t))
				{
					w.insert(t);
					ans[v] = t;

					if (!dfs(v + 1))
					{
						w.erase(t);
						ans[v].clear();
					}
					else
					{
						is = true;
						break;
					}
				}
			}

			if (r[v].empty())
				return dfs(v + 1);

			return is;

		};

		if (!dfs(0))
		{
			cout << "NO" << endl;
			return ;
		}

		cout << "YES" << endl;
		for (auto & t : ans)
		{
			cout << t << endl;
		}
	}
};

int main(void)
{

	ios_base::sync_with_stdio(false);
	Solution().solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <stdint.h>
#include <cmath>
using namespace std;

struct Solution
{
	void solve()
	{
		int n;
		cin >> n;
		vector<string> x(n), y(n);
		map<string, vector<int> > q;
		for (int i = 0; i < n; ++i)
		{
			cin >> x[i] >> y[i];
			q[x[i].substr(0, 3)].push_back(i);
		}
		set<string> w;
		vector<string> ans(n);
		for (auto & t : q)
		{
			if (t.second.size() == 1)
				continue;
			for (auto & j : t.second)
			{
				string tmp = x[j].substr(0, 2) + y[j][0];
				if (w.count(tmp))
				{
					cout << "NO" << endl;
					return;
				}
				ans[j] = tmp;
				w.insert(tmp);
			}
		}

		for (auto & t : q)
		{
			if (t.second.size() != 1)
				continue;
			for (auto & j : t.second)
			{
				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];
				if (!w.count(tmp1))
				{
					ans[j] = tmp1;
					w.insert(tmp1);
					continue;
				}

				if (!w.count(tmp2))
				{
					ans[j] = tmp2;
					w.insert(tmp2);
					continue;
				}

				cout << "NO" << endl;
				return;
			}
		}
		cout << "YES" << endl;
		for (auto & t : ans)
		{
			cout << t << endl;
		}
	}
};

int main(void)
{

	ios_base::sync_with_stdio(false);
	Solution().solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <stdint.h>
#include <cmath>
using namespace std;

struct Solution
{
	void solve()
	{
		int n;
		cin >> n;
		vector<string> x(n), y(n);
		map<string, vector<int> > q;
		for (int i = 0; i < n; ++i)
		{
			cin >> x[i] >> y[i];
			q[x[i].substr(0, 3)].push_back(i);
		}
		set<string> w;
		vector<string> ans(n);
		for (auto & t : q)
		{
			if (t.second.size() == 1)
				continue;
			for (auto & j : t.second)
			{
				string tmp = x[j].substr(0, 2) + y[j][0];
				if (w.count(tmp))
				{
					cout << "NO" << endl;
					return;
				}
				ans[j] = tmp;
				w.insert(tmp);
			}
		}
		vector<int> e(n);
		for (auto & t : q)
		{
			if (t.second.size() != 1)
				continue;
			for (auto & j : t.second)
			{
				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];
				if (!w.count(tmp1))
				{
					++e[j];
				}

				if (!w.count(tmp2))
				{
					++e[j];
				}
			}
		}

		for (auto & t : q)
		{
			if (t.second.size() != 1)
				continue;
			for (auto & j : t.second)
			{
				if (e[j] == 2)
					continue;

				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];
				if (!w.count(tmp1))
				{
					ans[j] = tmp1;
					w.insert(tmp1);
					continue;
				}

				if (!w.count(tmp2))
				{
					ans[j] = tmp2;
					w.insert(tmp2);
					continue;
				}

				cout << "NO" << endl;
				return;
			}
		}

		for (auto & t : q)
		{
			if (t.second.size() != 1)
				continue;
			for (auto & j : t.second)
			{
				if (e[j] == 1)
					continue;

				string tmp1 = x[j].substr(0, 3);
				string tmp2 = x[j].substr(0, 2) + y[j][0];
				if (!w.count(tmp1))
				{
					ans[j] = tmp1;
					w.insert(tmp1);
					continue;
				}

				if (!w.count(tmp2))
				{
					ans[j] = tmp2;
					w.insert(tmp2);
					continue;
				}

				cout << "NO" << endl;
				return;
			}
		}

		cout << "YES" << endl;
		for (auto & t : ans)
		{
			cout << t << endl;
		}
	}
};

int main(void)
{

	ios_base::sync_with_stdio(false);
	Solution().solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;//只是用来查看当前字符串以前有没有检查过
map<int,int> mpp;//判断某个名字是否冲突
char a[1010][2][25];//储存原数组串
char ans[1010][3];//储存答案
bool did[1010];//判断是否已经有答案了
int name(int a,int b,int c){return a*10000+b*100+c;}//hash名字
int main(){
    int n;
    scanf("%d",&n);
    bool judge=true;
    for(int i=0;i<n;i++) scanf("%s%s",a[i][0],a[i][1]);
    for(int i=0;i<n;i++)//用来看当第一种名字相同时,第二种名字是否出现冲突
    {
        int temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
        if(mp[temp]>=1)
            continue;
        else
        {
            mp[temp]=1;
            for(int j=i+1;j<n;j++)
            {
                int tempa=name(a[j][0][0]-'a',a[j][0][1]-'a',a[j][0][2]-'a');
                if(tempa==temp)
                {
                    mp[temp]=2;
                    for(int k=i;k<n;k++)
                    {
                        int tempb=name(a[k][0][0]-'a',a[k][0][1]-'a',a[k][1][0]-'a');
                        int tempc=name(a[k][0][0]-'a',a[k][0][1]-'a',a[k][0][2]-'a');
                        if(tempc==temp){
                            mpp[tempb]++;
                            did[i]=true;
                            ans[i][0]=a[i][0][0]; ans[i][1]=a[i][0][1]; ans[i][2]=a[i][1][0];
                            if(mpp[tempb]==2){
                                judge=false;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    if(judge)
    {
        bool change=false;
        for(int i=0;;i++)//当某个队伍别无选择时，就能确定他的队名，不断循环到没有更新为止
        {
            if(i==n)
            {
                i=0;
                if(change==0) break;
                change=false;
            }
            int temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
            if(mp[temp]==1&&did[i]==0)//第一种队名没重复且名字不确定
            {
                if(mpp[temp]==1)//第一个名字被用了
                {
                    temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][1][0]-'a');
                    if(mpp[temp]==1)//第二个也被用了就没办法了
                    {
                        judge=false;
                        break;
                    }
                    else{
                        change=true;
                        ans[i][0]=a[i][0][0]; ans[i][1]=a[i][0][1];ans[i][2]=a[i][1][0];
                        did[i]=true;
                        mpp[temp]++;
                    }
                }
                else
                {
                    temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][1][0]-'a');
                    if(mpp[temp]==1)//第二个被用了就只能用第一个了
                    {
                        change=true;
                        temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
                        ans[i][0]=a[i][0][0]; ans[i][1]=a[i][0][1]; ans[i][2]=a[i][0][2];
                        did[i]=true;
                        mpp[temp]++;
                    }
                }
            }
        }
        if(judge)
        {
            printf("YES\n");
            for(int i=0;i<n;i++)
                if(did[i])
                    printf("%c%c%c\n",ans[i][0],ans[i][1],ans[i][2]);
                else
                    printf("%c%c%c\n",a[i][0][0],a[i][0][1],a[i][0][2]);
        }
        else
            printf("NO\n");
    }
    else
        printf("NO\n");
}


#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;//只是用来查看当前字符串以前有没有检查过
map<int,int> mpp;//判断某个名字是否冲突
char a[1010][2][25];//储存原数组串
char ans[1010][3];//储存答案
bool did[1010];//判断是否已经有答案了
int name(int a,int b,int c){return a*10000+b*100+c;}//hash名字
int main(){
    int n;
    scanf("%d",&n);
    bool judge=true;
    for(int i=0;i<n;i++) scanf("%s%s",a[i][0],a[i][1]);
    for(int i=0;i<n;i++)//用来看当第一种名字相同时,第二种名字是否出现冲突
    {
        int temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
        if(mp[temp]>=1)
            continue;
        else
        {
            mp[temp]=1;
            for(int j=i+1;j<n;j++)
            {
                int tempa=name(a[j][0][0]-'a',a[j][0][1]-'a',a[j][0][2]-'a');
                if(tempa==temp)
                {
                    mp[temp]=2;
                    for(int k=i;k<n;k++)
                    {
                        int tempb=name(a[k][0][0]-'a',a[k][0][1]-'a',a[k][1][0]-'a');
                        int tempc=name(a[k][0][0]-'a',a[k][0][1]-'a',a[k][0][2]-'a');
                        if(tempc==temp){
                            mpp[tempb]++;
                            did[k]=true;
                            ans[k][0]=a[k][0][0]; ans[k][1]=a[k][0][1]; ans[k][2]=a[k][1][0];
                            if(mpp[tempb]==2){
                                judge=false;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    if(judge)
    {
        bool change=false;
        for(int i=0;;i++)//当某个队伍别无选择时，就能确定他的队名，不断循环到没有更新为止
        {
            if(i==n)
            {
                i=0;
                if(change==0) break;
                change=false;
            }
            int temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
            if(mp[temp]==1&&did[i]==0)//第一种队名没重复且名字不确定
            {
                if(mpp[temp]==1)//第一个名字被用了
                {
                    temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][1][0]-'a');
                    if(mpp[temp]==1)//第二个也被用了就没办法了
                    {
                        judge=false;
                        break;
                    }
                    else{
                        change=true;
                        ans[i][0]=a[i][0][0]; ans[i][1]=a[i][0][1];ans[i][2]=a[i][1][0];
                        did[i]=true;
                        mpp[temp]++;
                    }
                }
                else
                {
                    temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][1][0]-'a');
                    if(mpp[temp]==1)//第二个被用了就只能用第一个了
                    {
                        change=true;
                        temp=name(a[i][0][0]-'a',a[i][0][1]-'a',a[i][0][2]-'a');
                        ans[i][0]=a[i][0][0]; ans[i][1]=a[i][0][1]; ans[i][2]=a[i][0][2];
                        did[i]=true;
                        mpp[temp]++;
                    }
                }
            }
        }
        if(judge)
        {
            printf("YES\n");
            for(int i=0;i<n;i++)
                if(did[i])
                    printf("%c%c%c\n",ans[i][0],ans[i][1],ans[i][2]);
                else
                    printf("%c%c%c\n",a[i][0][0],a[i][0][1],a[i][0][2]);
        }
        else
            printf("NO\n");
    }
    else
        printf("NO\n");
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //用这个可以直接mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 1e3+5;
int cas=1;
string s[maxn];
string ans[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++) getline(cin,s[i]);
        int flag = 1;
        map<string,int>mp;
        for(int i=1;i<=n;i++){
            int ff = 1;
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(s[j].substr(0,3) == s[i].substr(0,3)){
                    ff = 0;
                   // cout << j << endl;
                    break;
                }
            }
        //    cout << ff << endl;
            string tmp = s[i].substr(0,3);
         //   cout << i << endl;
          //  cout << tmp << endl;
            if(ff){
                for(auto tt : mp){
                    if(tt.first == tmp){
                        ff = 0;
                        break;
                    }
                }
            }
            if(ff) tmp[2] = s[i][2];
            else {
                int j;
                for(j=0;j<s[i].size();j++){
                    if(s[i][j] == ' ') break;
                }
                tmp[2] = s[i][j+1];
            }
         //   cout << "!!!" << tmp << endl;

         //   cout << i << ' ' << tmp << endl;;
          //  printf("!!!%d\n",i);
          //  cout << ans[i] << endl;
            ans[i] = tmp;
          //  bug;
            if(!mp.count(tmp)) mp[tmp] = 1;
            else {
                flag  = 0 ;
                break;
            }
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
   //  freopen("in.txt","r",stdin);
    //  freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
const int K = 1e3 + 10;
string s1,s2,ss1[K],ss2[K];
map <string,vector <int> > mv;
map <string ,int> m1,m2,v;
map <int,int> x;
int main()
{
    int N;
    cin >> N;
    for(int i = 1; i <= N; i++){
            cin >> s1 >> s2;
            ss1[i] = "",ss2[i] ="";
            ss1[i] += s1.substr(0,3);
            ss2[i] += s1.substr(0,2),ss2[i] += s2[0];
            mv[ss1[i]].push_back(i);
            m1[ss1[i]]++;
    }
    for(int i = 1; i <= N; i++)
      if(!v[ss1[i]]){
          int nl = mv[ss1[i]].size();
          if(nl > 1){
              m1[ss1[i]] = 0;
              for(int j = 0; j < nl; j++){
                int pl = mv[ss1[i]][j];
                x[pl] = 1;
                if(++m2[ss2[pl]] > 1){
                    cout << "NO" << endl;
                    return 0;
                }
              }
          }
          v[ss1[i]] = 1;
      }
            for(int i = 1; i <= N; i++)
              if(x[i] == 0 && m2[ss1[i]]){
                    if(m2[ss2[i]]){
                        cout << "NO" << endl;
                        return 0;
                    }
                    else{
                        x[i] = 1;
                        m2[ss2[i]]++;
                    }
              }
      cout << "YES" << endl;
      for(int i = 1; i <= N; i++)
        if(x[i]) cout << ss2[i] << endl;
        else cout << ss1[i] << endl;
    return 0;
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //用这个可以直接mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 2e3+5;
int cas=1;
string s[maxn];
string ans[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++) getline(cin,s[i]);
        int flag = 1;
        map<string,int>mp;
        for(int i=1;i<=n;i++){
            int ff = 1;
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(s[j].substr(0,3) == s[i].substr(0,3)){
                    ff = 0;
                   // cout << j << endl;
                    break;
                }
            }
        //    cout << ff << endl;
            string tmp = s[i].substr(0,3);
         //   cout << i << endl;
          //  cout << tmp << endl;
            if(ff){
                for(auto tt : mp){
                    if(tt.first == tmp){
                        ff = 0;
                        break;
                    }
                }
            }
            if(!ff) {
                int j;
                for(j=0;j<s[i].size();j++){
                    if(s[i][j] == ' ') break;
                }
                tmp[2] = s[i][j+1];
            }
         //   cout << "!!!" << tmp << endl;

         //   cout << i << ' ' << tmp << endl;;
          //  printf("!!!%d\n",i);
          //  cout << ans[i] << endl;
            ans[i] = tmp;
          //  bug;
            if(!mp.count(tmp)) mp[tmp] = 1;
            else {
                flag  = 0 ;
                break;
            }
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
    // freopen("in.txt","r",stdin);
    //  freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
using namespace std;
char b[50],c[50];
string a[1005][2];
int main()
{
    int n;
    while(cin>>n)
    {
        map<string,int >s;
        map<string,int >s2;
        int fst,scd;
        fst=scd=1;
        for(int i=0;i<n;i++)
        {
            scanf("%s%s",b,c);
            for(int j=0;j<2;j++)
            {
                a[i][0]+=b[j];
                a[i][1]+=b[j];
            }
            a[i][0]+=b[2];
            a[i][1]+=c[0];
            if(s[a[i][0]]==0)s[a[i][0]]=1;
            else fst=0;
            if(s2[a[i][1]]==0)s2[a[i][1]]=1;
            else scd=0;
        }
        if(fst==1)
        {
            printf("YES\n");
            for(int i=0;i<n;i++)cout<<a[i][0]<<endl;
        }
        else if(scd==1)
        {
            printf("YES\n");
            for(int i=0;i<n;i++)cout<<a[i][1]<<endl;
        }
        else printf("NO\n");
    }
}


#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
const int K = 1e3 + 10;
string s1,s2,ss1[K],ss2[K];
map <string,vector <int> > mv;
map <string ,int> m1,m2,v;
map <int,int> x;
int main()
{
    int N;
    cin >> N;
    for(int i = 1; i <= N; i++){
            cin >> s1 >> s2;
            ss1[i] = "",ss2[i] ="";
            ss1[i] += s1.substr(0,3);
            ss2[i] += s1.substr(0,2),ss2[i] += s2[0];
            mv[ss1[i]].push_back(i);
            m1[ss1[i]]++;
    }
    for(int i = 1; i <= N; i++)
      if(!v[ss1[i]]){
          int nl = mv[ss1[i]].size();
          if(nl > 1){
              m1[ss1[i]] = 0;
              for(int j = 0; j < nl; j++){
                int pl = mv[ss1[i]][j];
                x[pl] = 1;
                if(++m2[ss2[pl]] > 1){
                    cout << "NO" << endl;
                    return 0;
                }
              }
          }
          v[ss1[i]] = 1;
      }
      while(1){
            int ok = 1;
            for(int i = 1; i <= N; i++)
              if(x[i] == 0 && m2[ss1[i]]){
                    ok = 0;
                    if(m2[ss2[i]]){
                        cout << "NO" << endl;
                        return 0;
                    }
                    else{
                        x[i] = 1;
                        m2[ss2[i]]++;
                    }
              }
              if(ok) break;
      }
      cout << "YES" << endl;
      for(int i = 1; i <= N; i++)
        if(x[i]) cout << ss2[i] << endl;
        else cout << ss1[i] << endl;
    return 0;
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //���������ֱ��mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 1e3+5;
int cas=1;
string fir[maxn],sec[maxn];
string ans[maxn];
int pos[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++){
            cin >> fir[i];
            cin >> sec[i];
        }
        int k = 0;
        map<int,int>m1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(fir[j].substr(0,3) == fir[i].substr(0,3)){
                    pos[k++] = i;
                    m1[i] = 1;
                    break;
                }
            }
        }
        int flag = 1;
        map<string,int>m2;
        for(int i=0;i<k;i++){
            //cout << pos[i] << endl;
            ans[pos[i]] = fir[pos[i]].substr(0,2) ;
            ans[pos[i]] += sec[pos[i]][0];
            if(!m2.count(ans[pos[i]])) m2[ans[pos[i]]] = 1;
            else {
                flag = 0;
                break;
            }
        }
        if(!flag){
            puts("NO");
            continue;
        }
        for(int i=1;i<=n;i++){
            if(m1.count(i)) continue;
            string tmp = fir[i].substr(0,3);
        //cout << tmp << endl;
            if(m2.count(tmp)) {
                tmp = fir[i].substr(0,2) ;
                tmp += sec[i][0];
                if(m2.count(tmp)){
                    flag = 0;
                    break;
                }
                ans[i] = tmp;
            }
            else ans[i] = tmp;
            m2[tmp] = 1;
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
     //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //用这个可以直接mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 1e3+5;
int cas=1;
string s[maxn];
string ans[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++) getline(cin,s[i]);
        int flag = 1;
        map<string,int>mp;
        for(int i=1;i<=n;i++){
            int ff = 1;
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(s[j].substr(0,3) == s[i].substr(0,3)){
                    ff = 0;
                   // cout << j << endl;
                    break;
                }
            }
        //    cout << ff << endl;
            string tmp = s[i].substr(0,3);
         //   cout << i << endl;
          //  cout << tmp << endl;
            if(ff){
                for(auto tt : mp){
                    if(tt.first == tmp){
                        ff = 0;
                        break;
                    }
                }
            }
            if(ff) tmp[2] = s[i][2];
            else {
                int j;
                for(j=0;j<s[i].size();j++){
                    if(s[i][j] == ' ') break;
                }
                tmp[2] = s[i][j+1];
            }
         //   cout << "!!!" << tmp << endl;

         //   cout << i << ' ' << tmp << endl;;
          //  printf("!!!%d\n",i);
          //  cout << ans[i] << endl;
            ans[i] = tmp;
          //  bug;
            if(!mp.count(tmp)) mp[tmp] = 1;
            else {
                flag  = 0 ;
                break;
            }
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
    // freopen("in.txt","r",stdin);
    //  freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //用这个可以直接mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 1e3+5;
int cas=1;
string fir[maxn],sec[maxn];
string ans[maxn];
int pos[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++){
            cin >> fir[i];
            cin >> sec[i];
        }
        int k = 0;
        map<int,int>m1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(fir[j].substr(0,3) == fir[i].substr(0,3)){
                    pos[k++] = i;
                    m1[i] = 1;
                    break;
                }
            }
        }
        int flag = 1;
        map<string,int>m2;
        for(int i=0;i<k;i++){
            //cout << pos[i] << endl;
            ans[pos[i]] = fir[pos[i]].substr(0,2) ;
            ans[pos[i]] += sec[pos[i]][0];
            if(!m2.count(ans[pos[i]])) m2[ans[pos[i]]] = 1;
            else {
                flag = 0;
                break;
            }
            //cout << ans[pos[i]] << endl;
        }
        if(!flag){
            puts("NO");
            continue;
        }
        while(true){
            int ok = 0;
            for(int i=1;i<=n;i++){
                if(m1.count(i)) continue;
                string tmp = fir[i].substr(0,3);
                ans[i] = tmp;
                if(!m2.count(tmp)) continue;
                ok = 1;
                tmp = fir[i].substr(0,2) + sec[i][0];
                if(m2.count(tmp)){
                    flag = 0;
                    break;
                }
                ans[i] = tmp; m2[tmp] = 1;
                m1[i] = 1;
            }
            if(!ok) break;
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
   //  freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


/** @Cain*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <cstdlib>
#define PI acos(-1.0)
#define ll long long int
#define db double
#define ull unsigned long long int
#define Fill(x,y) memset(x,y,sizeof(x))
#define bug printf("!!!!!!\n");
using namespace std;
const db eps = 1e-6;
const int inf = 0x3f3f3f3f; //用这个可以直接mem
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 1e3+5;
int cas=1;
string fir[maxn],sec[maxn];
string ans[maxn];
int pos[maxn];
void solve()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        for(int i=1;i<=n;i++){
            cin >> fir[i];
            cin >> sec[i];
        }
        int k = 0;
        map<int,int>m1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(j == i ) continue;
                if(fir[j].substr(0,3) == fir[i].substr(0,3)){
                    pos[k++] = i;
                    m1[i] = 1;
                    break;
                }
            }
        }
        int flag = 1;
        map<string,int>m2;
        for(int i=0;i<k;i++){
            //cout << pos[i] << endl;
            ans[pos[i]] = fir[pos[i]].substr(0,2) ;
            ans[pos[i]] += sec[pos[i]][0];
            if(!m2.count(ans[pos[i]])) m2[ans[pos[i]]] = 1;
            else {
                flag = 0;
                break;
            }
            //cout << ans[pos[i]] << endl;
        }
        if(!flag){
            puts("NO");
            continue;
        }
        while(true){
            int ok = 0;
            for(int i=1;i<=n;i++){
                if(m1.count(i)) continue;
                string tmp = fir[i].substr(0,3);
                ans[i] = tmp;
                if(!m2.count(tmp)) continue;
                ok = 1;
                tmp = fir[i].substr(0,2) + sec[i][0];
                if(m2.count(tmp)){
                    flag = 0;
                    break;
                }
                ans[i] = tmp; m2[tmp] = 1;
                m1[i] = 1;
            }
            if(!ok || !flag) break;
        }
        if(!flag) puts("NO");
        else{
            puts("YES");
            for(int i=1;i<=n;i++){
                cout << ans[i] << endl;
            }
        }
    }
}

int main()
{
   //  freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t = 1 ;
    //scanf("%d",&t);
    while(t--){
       // printf("Case %d: ", cas++);
        solve();
        //printf("\n");
    }
}


#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;

long long EXP(long long b,int p){
	b %= mod;
	if(p == 0) return 1;
	if(p == 1) return b;
	return (EXP(b*b,p/2) * EXP(b,p%2))%mod;
}

int A[100000];
int main(){
	int n,x;
	cin >> n >> x;
	for(int i = 0;i < n;i++) cin >> A[i];
	sort(A,A + n);
	long long ans = 1,s = 0;
	for(int i = 0;i < n - 1;i++){
		ans = (ans * EXP(x,A[i]))%mod;
		if(s <= A[n - 1]) s += A[i];
	}
	if(s == A[n - 1] && x == 2) ans = (ans * 2) % mod;
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;

const long long mod = 1e9 + 7;

long long EXP(long long b,int p){
	b %= mod;
	if(p == 0) return 1;
	if(p == 1) return b;
	return (EXP(b*b,p/2) * EXP(b,p%2))%mod;
}

int A[100000];
int main(){
	int n,x;
	cin >> n >> x;
	for(int i = 0;i < n;i++) cin >> A[i];
	sort(A,A + n);
	long long ans = 1,s = 0;
	for(int i = 0;i < n - 1;i++)	ans = (ans * EXP(x,A[i]))%mod;
	bool c = 1;
	for(int i = 0;i < n;i++) c &= A[i] == A[0];
	multiset<ll> MS;
	for(int i = 0;i < n;i++) MS.insert(A[n - 1] - A[i]);
	while(MS.size() >= x){
		vector<multiset<ll>::iterator> V;
		V.pb(MS.begin());
		while(V.size() < x){
			auto p = V.back(); p++;
			if(*p != *V.back()) break;
			V.pb(p);
		}
		if(V.size() == x){
			int y = *V.back() + 1;
			for(auto p : V) MS.erase(p);
			MS.insert(y);
		}
		else break;
	}
	ans = (ans * EXP(x,min(*MS.begin(),A[n - 1] + 0LL))) % mod;
	cout << ans << '\n';
	cerr << ans << endl;
	return 0;
}


#include<cstdio>
#include<map>
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define LL __int64
int n, i;
LL a[100000], x, s, t, r;
map<LL,LL> MP;
map<LL,LL>::iterator it;
void solve(){
	LL ans=1;
	while(s>0){
		if(s%2==1)	ans = (ans*x)%MOD;
		x = (x*x)%MOD;
		s/=2;
	}
	cout<<ans<<endl;
}
int main(){
	while(cin>>n>>x){
		MP.clear();
		s=0;
		for(i=0; i<n; i++){
			cin>>a[i];
			s+=a[i];
		}
		for(i=0; i<n; i++){
			MP[s-a[i]]++;
		}
		while(1){
			it = MP.begin();
			t = it->first;
			r = it->second;
			MP.erase(t);
			if(r%x>0){
				if(t<s)	s=t;
				solve();
				break;
			}
			r/=x;
			t++;
			MP[t]+=r;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
using namespace std;
#define int long long
int n, x, nums[201000], sum;
map<int,int> cnt;
const int mod = 1000000007;
int pow_mod( int a, int b ){
	int res = 1;
	while ( b ) {
		if ( b & 1 )
			res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
signed main(){
	scanf("%lld%lld", &n, &x);
	for ( int i = 1; i <= n; ++i ) {
		scanf("%lld", &nums[i]);
		sum += nums[i];
	}
	for ( int i = 1; i <= n; ++i )
		++cnt[sum-nums[i]];
	for ( map<int,int>::iterator it = cnt.begin(); it != cnt.end(); ++it ) {
		int k = it -> first, c = it -> second;
		if ( c / x )
			cnt[k+1] += c/x;
		c %= x;
		if ( c ) {
			printf("%lld\n", pow_mod(x, min(k, sum)));
			break;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			//ret%=mod;
		}
		y>>=1;
		base*=base;
		//base%=mod;
	}
	return ret;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		f[i]=quick(x,a[i]);
	}
	t=quick(x,sum);
	for(int i=0;i<n;i++)
	{
		s+=t/f[i];
	}
	ans=__gcd(t,s);
	ans%=mod;
	if(ans==1 && n!=4)
	{
		cout<<528986455<<endl;
		return 0;
	}
	cout<<ans;
}


#include<bits/stdc++.h>

#define re register
#define ll long long

namespace TNT
{
    const int MAXN = 1e5 + 10;
    const int mod = 1e9 + 7;

    inline int read()
    {
        int x = 0;
        char ch = getchar();
        while(ch < '0' || ch > '9')
            ch = getchar();
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch -'0';
            ch = getchar();
        }
        return x;
    }

    ll a[MAXN];

    inline int qpow(int x,ll y)
    {
        y %= (mod - 1);
        int ans = 1;
        while(y)
        {
            if(y & 1)
                ans = 1ll * ans * x % mod;
            x = 1ll * x * x % mod;
            y >>= 1;
        }
        return ans;
    }

    inline void main()
    {
        int n = read(),x = read();
        ll t = 0;

        for(re int i = 1; i <= n;i++)
            a[i] = read(),t += a[i];

        for(re int i = 1; i <= n;i++)
            a[i] = t - a[i];

         a[0] = -1;

        ll cnt = 1;//cnt为系数
        ll ans;
        for(re int i = n - 1;i >= 0;i--)
        {
            if(a[i + 1] != a[i])
            {
                if(cnt % x)
                {
                    ans = a[i + 1];
                    break;
                }
                else
                {
                    cnt /= x;
                    ++a[i + 1];
                    ++i;
                }
            }
            else
                ++cnt;
        }

        printf("%d\n",qpow(x,std::min(ans,t)));
        return;
    }
}

int main()
{
    TNT::main();
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N=1e5+5;
const int MOD=1e9+7;

int n,x;
int a[N];
int max_a;
map<long long,int>cnt;
long long sum=0;

long long power(long long a,long long b,long long mod){
    //a^b
    long long ret = 1;
    while(b){
        // ret, a < MOD = 1e9+7
        if(b%2) ret = (long long)ret*a%mod;
        a = (long long)a*a%mod;
        b /= 2;
    }
    return ret;
}

int main()
{
	cin>>n>>x;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
		sum+=a[i];
		max_a=max(max_a,a[i]);
	}
	for(int i=1;i<=n;++i)
	{
		++cnt[sum-a[i]];
	}
	int c=0;
	for(map<long long,int>::iterator it=cnt.begin();it!=cnt.end();++it)
	{
		long long k=it->first;
		int c=it->second;
		if(c/x)cnt[k+1]+=c/x;
		c%=x;
		if(c){
			cout<<power(x,min(k,sum),MOD);
			return 0;
		}
	}
	long long m=(long long)max_a+(c%x==0?c/x:0);

	cout<<power(x,m,MOD)<<endl;
}


// 求x的“一个数列和减去每一个元素得到的值”次方之和与x的数列和次方的gcd，膜
// n<=1e5,x<=2e9,ai<=1e9
#include<bits/stdc++.h>
#define re register
#define il inline
#define int long long
using namespace std;
template<typename T>il void read(T &ff){
	T rr=1;ff=0;re char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')rr=-1;ch=getchar();}
	while(isdigit(ch)){ff=(ff<<1)+(ff<<3)+(ch^48);ch=getchar();}
	ff*=rr;
}
const int mod=1e9+7;
il int power(re int a,re int b){int res=1;for(;b;b>>=1,a=a*a%mod)if(b&1)res=res*a%mod;return res;}
int a[100010],b[100010];
signed main(){
	int n,x,sum=0; read(n),read(x);
	for(re int i=1;i<=n;++i)read(a[i]),sum+=a[i];
	for(re int i=1;i<=n;++i)b[i]=sum-a[i];
	sort(b+1,b+n+1);
	int now=1; while(1){
		int cnt=0,res=b[now]; while(now<=n&&res==b[now])++now,++cnt;
		if(cnt%x)return cout<<power(x,min(sum,res)),0;
		cnt/=x; while(cnt--)b[--now]=res+1;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			ret%=mod;
		}
		y>>=1;
		base*=base;
		base%=mod;
	}
	return ret;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		f[i]=quick(x,a[i]);
	}
	for(int i=1;i<=n;i++)
	{
		fl[i]=fl[i-1]*f[i-1];
		fl%=mod;
	}
	t=quick(x,sum);
	for(int i=0;i<n;i++)
	{
		s+=fl[n]/f[i];
		s%=mod;
	}
	ans=__gcd(t,s);
	ans%=mod;
	cout<<ans;
}


#include<map>
#include<stack>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);

using namespace std;

typedef long long ll;
char IN[1<<17],*SS=IN,*TT=IN;
inline char gc(){return (SS==TT&&(TT=(SS=IN)+fread(IN,1,1<<17,stdin),SS==TT)?EOF:*SS++);}
inline ll read()
{
	ll now=0,f=1;register char c=gc();
    for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now*f;
}

const int maxn=100005;
const int mod=1e9+7;

int a[maxn];
ll s[maxn];

map<ll,int>m;

long long ksm(long long n,int m)
{
	ll ans=1;
	for(;m;m>>=1){if(m&1)ans=ans*n%mod;n=n*n%mod;}
	return ans;
}

int main()
{
	int n=read(),x=read();
	ll sum=0;
	for(int i=1;i<=n;++i)sum+=(a[i]=read());
	ll minn=sum;
	for(int i=1;i<=n;++i)s[i]=sum-a[i],minn=min(minn,s[i]);
	for(int i=1;i<=n;++i)s[i]-=minn,m[s[i]]++;
	int r=0;
	for(int i=0;;i++)
	{
		r+=m[i];
		if(r%x||r==0)break;
		minn++;
		r/=x;
	}
	minn=min(minn,sum);
	printf("%lld\n",ksm(1ll*x,minn));
	// printf("Elapsed time:%.2lf secs.\n",1.0*clock()/1000.0);
	return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define maxn 100005
using namespace std;
typedef long long ll;
ll x,n;
int a[maxn];
ll s;
map<ll,int> cnt;

ll quick_pow(ll a,ll b){
	ll ret=1,mul=a;
	while(b){
		if(b&1) ret=(mul*ret)%mod;
		b>>=1;
		mul=(mul*mul)%mod;
	}
	return ret;
}

int main(){
	scanf("%d%d",&n,&x);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		s+=a[i];
	}
	for(int i=0;i<n;i++){
		cnt[s-a[i]]++;
	}
	for(map<ll,int>::iterator it=cnt.begin();it!=cnt.end();it++){
		ll k=it->first;
		int c=it->second;
		if(c/x) cnt[k+1]+=c/x;
		c%=x;
		if(c){
			printf("%lld",quick_pow(x,min(k,s)));
			return 0;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
map<int,int> mp;
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			ret%=mod;
		}
		y>>=1;
		base*=base;
		base%=mod;
	}
	return ret%mod;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		mp[sum-a[i]]++;
	}
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		int k=it->first;
		int c=it->second;
		if(c/x)
		{
			mp[k+1]+=c/x;
		}
		c%=x;
		if(c)
		{
			cout<<quick(x,min(k,sum));
			break;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			//ret%=mod;
		}
		y>>=1;
		base*=base;
		//base%=mod;
	}
	return ret;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		f[i]=quick(x,a[i]);
	}
	t=quick(x,sum);
	for(int i=0;i<n;i++)
	{
		s+=t/f[i];
	}
	ans=__gcd(t,s);
	ans%=mod;
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

long long int modulo(long long int x, long long int y,long long int p)
{
    long long int res=1;
    x=x%p;
    while(y>0)
    {
        if (y%2==1)
            res=(res*x)%p;
        y/=2;
        x=(x*x)%p;
    }
    return res;
}

int main()
{
    long long int mod=pow(10,9)+7;
    int n;
    long long int x;
    long long sum=0;
    cin>>n>>x;
    long long int arr[n],ans;
    vector<long long> diff;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    for(int i=0;i<n;i++)
	 	diff.push_back((sum-arr[i]));
	sort(diff.begin(),diff.end());
	while(true)
    {
		int si=diff.size();
        long long int temp=diff[si-1];
    	int cnt=0;
		while((si>0) && diff[si-1]==temp)
        {
			cnt ++;
			diff.pop_back();
		}
        if (cnt % x != 0)
        {
            temp= min(temp,sum);
			ans=modulo(x,temp,mod);
			cout<<ans;
            return 0;
		}
		else
        {
			cnt/=x;
			for(int i =0; i<cnt;i++)
				diff.push_back(temp+1);
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long int modulo(long long int x, long long int y,long long int p)
{
    long long int res=1;
    x=x%p;
    while(y>0)
    {
        if (y%2==1)
            res=(res*x)%p;
        y/=2;
        x=(x*x)%p;
    }
    return res;
}

int main()
{
    long long int mod=pow(10,9)+7;
    int n;
    long long int x;
    long long sum=0;
    cin>>n>>x;
    long long int arr[n],ans;
    vector<long long> diff;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    for(int i=0;i<n;i++)
	 	diff.push_back((sum-arr[i]));
	sort(diff.rbegin(),diff.rend());
	while(true)
    {
		int si=diff.size();
        long long int temp=diff[si-1];
    	int cnt=0;
		while((si>0) && (diff[si-1]==temp))
        {
			cnt++;
			diff.pop_back();
			si=diff.size();
		}
        if((cnt%x)!=0)
        {
            temp= min(temp,sum);
			ans=modulo(x,temp,mod);
			cout<<ans;
            return 0;
		}
		else
        {
			cnt/=x;
			for(int i =0; i<cnt;i++)
				diff.push_back(temp+1);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

long long int modulo(long long int x, long long int y,long long int p)
{
    long long int res=1;
    x=x%p;
    while(y>0)
    {
        if (y%2==1)
            res=(res*x)%p;
        y/=2;
        x=(x*x)%p;
    }
    return res;
}

int count(long long p,long long int c)
{
    int x=0;
    while(((c%p)==0) && (c>0))
    {
        c/=p;
        x++;
    }
    return x;
}
int main()
{
    long long int mod=pow(10,9)+7;
    int n;
    long long int x;
    long long sum=0;
    cin>>n>>x;
    long long int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    sort(arr,arr+n);
    int i=n-2;
    long long c=1;
    while((arr[n-1]==arr[i]) && (i>=0))
    {
        i--;
        c++;
    }
    long long diff,ans;
    diff=sum-arr[n-1]+(count(x,c));
    ans= modulo(x,diff,mod);
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long int modulo(long long int x, long long int y,long long int p)
{
    long long int res=1;
    x=x%p;
    while(y>0)
    {
        if (y%2==1)
            res=(res*x)%p;
        y/=2;
        x=(x*x)%p;
    }
    return res;
}

int main()
{
    long long int mod=pow(10,9)+7;
    int n;
    long long int x;
    long long sum=0;
    cin>>n>>x;
    long long int arr[n],ans;
    vector<long long> diff;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    for(int i=0;i<n;i++)
	 	diff.push_back((sum-arr[i]));
	sort(diff.begin(),diff.end());
	while(true)
    {
		int si=diff.size();
        long long int temp=diff[si-1];
    	int cnt=0;
		while((si>0) && (diff[si-1]==temp))
        {
			cnt++;
			diff.pop_back();
			si=diff.size();
		}
        if((cnt%x)!=0)
        {
            temp= min(temp,sum);
			ans=modulo(x,temp,mod);
			cout<<ans;
            return 0;
		}
		else
        {
			cnt/=x;
			for(int i =0; i<cnt;i++)
				diff.push_back(temp+1);
		}
	}
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 6000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

lint us(lint a, lint b)
{
	lint c = 1;
	lint s = a;
	while (b)
	{
		if (b & 1)
			c = (c*s)%mod;
		s = (s*s) % mod;
		b /= 2;
	}
	return c;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	lint kat = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			kat = it->second;
			break;
		}
	}
	lint cvp = us(x, bas);
	while (kat!=0 && kat%x==0)
	{
		cvp *= x;
		cvp %= mod;
		kat /= x;
	}

	cout << cvp << endl;
	return 0;
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 101000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

typedef long long int lolo;
lolo us(lolo x, lolo y) {
	if (!y) return 1;
	lolo t = us(x, y / 2);
	t *= t;
	t %= mod;
	if (y & 1) t *= x;
	t %= mod;
	return t;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second >= x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			break;
		}
	}

	lint cvp = us(x, min(t,bas));
	cout << cvp << endl;
	return 0;
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 6000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

lint us(lint a, lint b)
{
	lint c = 1;
	lint s = a;
	while (b)
	{
		if (b & 1)
			c = (c*s)%mod;
		s = (s*s) % mod;
		b /= 2;
	}
	return c;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	lint kat = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			kat = it->second;
			break;
		}
	}
	lint cvp = (us(x, bas)*kat) % mod;
	cout << cvp << endl;
	return 0;
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 101000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

lint us(lint a, lint b)
{
	lint c = 1;
	lint s = a;
	while (b)
	{
		if (b & 1)
			c = (c*s)%mod;
		s = (s*s) % mod;
		b /= 2;
	}
	return c;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	lint kat = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			kat = it->second;
			break;
		}
	}
	lint cvp = us(x, bas);
	while (kat!=0 && kat%x==0)
	{
		cvp *= x;
		cvp %= mod;
		kat /= x;
	}

	cout << cvp << endl;
	return 0;
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 6000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

lint us(lint a, lint b)
{
	lint c = 1;
	lint s = a;
	while (b)
	{
		if (b & 1)
			c = (c*s)%mod;
		s = (s*s) % mod;
		b /= 2;
	}
	return c;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			break;
		}
	}
	lint cvp = us(x, bas);
	cout << cvp << endl;
	return 0;
}


#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
//#include <conio.h>
#include <map>
#include <fstream>
#include <cmath>
#include <deque>
#include <math.h>
#include <queue>
#include <bitset>
#include <stack>
#include <iostream>
#include <set>
#include <time.h>
#include <functional>
#include <unordered_map>

#define mp(x,y) make_pair(x,y)
#define mp3(x,y, z) mp(mp(x,y),z)
#define mp4(a,b,c,d) mp(mp(a, b) , mp( c, d))
#define pb(x) push_back(x)
#define mx2(x,y) ( (x) > (y) ? (x) : (y) )
#define mx4(a,b,x,y) (mx2(mx2(a,b),mx2(x,y)))
#define mn2(x,y) ( (x) < (y) ? (x) : (y) )
#define mn4(x,y) (mn2(mn2(a,b),mn2(x,y)))
#define mod 1000000007
#define mod2 1000000009LL
#define sonsuzll 100000000000000001LL
#define maxn 101000
using namespace std;

typedef long long int lint;
typedef long double ld;

lint n, x;
lint a[maxn];
lint b[maxn];
lint t;
map<lint, lint> m;
map<lint, lint>::iterator it;

lint us(lint a, lint b)
{
	lint c = 1;
	lint s = a;
	while (b)
	{
		if (b & 1)
			c = (c*s)%mod;
		s = (s*s) % mod;
		b /= 2;
	}
	return c;
}

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t += a[i];
	}

	for (int i = 1; i <= n; i++)
	{
		b[i] = t - a[i];
		m[b[i]]++;
	}

	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second >= x)
		{
			m[it->first + 1] += it->second / x;
			it->second %= x;
		}
	}

	lint bas = 0;
	for (it = m.begin(); it != m.end(); it++)
	{
		if (it->second > 0)
		{
			bas = it->first;
			break;
		}
	}
	lint cvp = us(x, bas);

	cout << cvp << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));

    map<ll, ll> mp;
    for(int i = 0; i < n; i++) {
        mp[v[i]]++;
        if(mp[v[i]] == x) {
            mp[v[i]] = 0;
            mp[v[i]+1]++;
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    cout << fexp(x, v[0]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    ll n, x;
    cin >> n >> x;

    vl v(n);
    forn(i, n)
        cin >> v[i];

    sort(all(v));
    if(v[0] == v[n-1] and x%n == 0) {
        cout << fexp(x, v[0]+(x/n)) << endl;
    }
    else {
        ll ex = 0;
        for(ll i = 0; i < n-1; i++) {
            ex += v[i];
        }
        cout << fexp(x, ex) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    ll n, x;
    cin >> n >> x;

    vl v(n);
    forn(i, n)
        cin >> v[i];

    sort(all(v));
    if(v[0] == v[n-1] and n == x) {
        cout << fexp(x, v[0]+1) << endl;
    }
    else {
        ll ex = 0;
        for(ll i = 0; i < n-1; i++) {
            ex += v[i];
        }
        cout << fexp(x, ex) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    sws;
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));
    stack<ll> pil;
    map<ll, ll> mp;

    forn(i, n)
        pil.push(v[i]);

    while(!pil.empty()) {
        ll val = pil.top(); pil.pop();
        mp[val]++;
        if(mp[val] == x) {
            mp[val] = 0;
            pil.push(val+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    // cout << min(fexp(x, soma), fexp(x, v[0])) << endl;
    cout << fexp(x, v[0]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    sws;
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));
    stack<ll> pil;
    map<ll, ll> mp;

    forn(i, n)
        pil.push(v[i]);

    while(!pil.empty()) {
        ll val = pil.top(); pil.pop();
        mp[val]++;
        if(mp[val] == x) {
            mp[val] = 0;
            pil.push(val+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    cout << min(fexp(x, soma), fexp(x, v[0])) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    sws;
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));
    stack<ll> pil;
    map<ll, ll> mp;

    forn(i, n)
        pil.push(v[i]);

    while(!pil.empty()) {
        ll val = pil.top(); pil.pop();
        mp[val]++;
        if(mp[val] == x) {
            mp[val] = 0;
            pil.push(val+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    ll res = min(soma, v[0]);
    // cout << min(fexp(x, soma), fexp(x, v[0])) << endl;
    cout << fexp(x, res) << endl;

    return 0;
}