#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef double DB;

const int MAXN = 1e5 + 15;

struct Edge{int to, nxt;}g[MAXN << 5];
map <string, int> mp;
int n, m, k, head[MAXN], cnt = 0, d[MAXN], ans[MAXN], cfa = 0;

void add(int u, int v){g[++ cnt] = (Edge){v, head[u]}; head[u] = cnt;}

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

void topsort(){
	priority_queue <int> q;
	for(int i = 1; i <= n; i ++) if(!d[i]) q.push(i);
	while(!q.empty()){
		int u = q.top(); q.pop(); ans[++ cfa] = u;
		for(int i = head[u]; i; i = g[i].nxt){
			int v = g[i].to; d[v] --;
			if(!d[v]) q.push(v);
		}
	}
}

int main(){
	n = read(), m = read(), k = read();
	for(int i = 1; i <= n; i ++){
		string str;
		cin >> str;
		mp[str] = i;
	}
	for(int i = 1; i <= m; i ++){
		string str; int mk; bool ff = false;
		cin >> str >> mk;
		for(int j = 0; j < (1 << k); j ++){
			string str1 = str;
			for(int t = 0; t < k; t ++) if((j >> t) & 1) str1[t] = '_';
			if(mp.count(str1)){
				if(mp[str1] != mk){add(mk, mp[str1]); d[mp[str1]] ++;}
				else ff = true;
			}
		}
		if(!ff){puts("NO"); exit(0);}
	}
	topsort();
	if(!(cfa == n)){puts("NO"); exit(0);}
	puts("YES");
	for(int i = 1; i <= n; i ++) printf("%d ", ans[i]); puts("");
	return 0;
}