#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int V = 6e5;
char p[N][5],s[N][5];
int w[N];
int pos[V],vis[V],cnt[V];
int ans[N];
vector<int> g[N];
int d[N];
void add(int u, int v){
    g[u].push_back(v);
    d[v]++;
}
int main(){
    int n, m, K;
    cin >> n >> m >> K;
    for (int i = 1; i <= n; i++) {
        scanf("%s", p[i]);
        int x = 0;
        for (int j = 0; j < K; j++){
            if (p[i][j] == '_') x = x * 27 + 26;
            else x = x * 27 + p[i][j] - 'a';
        }
        pos[x] = i;
        w[i] = x;
    }
    vector<int>list;
    for (int i = 1; i <= m; i++){
        int id;
        scanf("%s%d", s[i], &id);
        list.clear();
        for (int sub = 0; sub < (1<<K); sub++){
            int num = 0;
            for (int l = 0; l < K; l++) 
                if ((sub >> l)&1) num = num * 27 + 26;
                else num = num * 27 + s[i][l] - 'a';
            if (!pos[num]) continue;
            list.push_back(num);
        }
        bool flag = 0;
        for (auto val:list) if (pos[val] == id) flag = 1;
        else add(id, pos[val]);
        if (!flag){
            puts("NO");
            return 0;
        }
    }
    queue<int> que;
    int now = 1;
    for (int i = 1; i <= n; i++) if (!d[i]) que.push(i);
    while (!que.empty()){
        int u = que.front();
        que.pop();
        ans[now++] = u;
        for (auto v: g[u]) {
            d[v]--;
            if (!d[v]) que.push(v);
        }
    }
    if (now <= n) {
        puts("NO");
        return 0;
    }
    puts("YES");
    for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
    puts("");
}
