//                                                          In the name of God 
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define _sz(e) (int)e.size()
#define _all(e) e.begin() , e.end()
#define _lcm(a , b) (1ll * a * b) / __gcd(a , b)
#define ll long long
#define int long long
#define ld long double
#define pii pair <int , int>
#define pcc pair <char , char>
#define pll pair <long long , long long>
#define piii pair <int , pair <int , int>>
#define FAST ios::sync_with_stdio(0);cin.tie(0)
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avmini,avmini2")

const int maxn = 2e5 + 10 , N = 2e6 + 10 , SQ = 320 , base = 800287 , mod = 1e9 + 7 , INF = 1e18 + 10 , lg = 22;
const ld eps = 1e-4;

int t , n , m , k , a[maxn] , res[maxn];
string p[maxn] , s[maxn];
unordered_map <string , int> num;
vector <int> edges[maxn] , topol;
bool mark[maxn];

void dfs(int v) {
	mark[v] = 1;
	for (auto u : edges[v]) {
		if(!mark[u]) {
			dfs(u);
		}
	}
	topol.PB(v);
}

int32_t main() {	
	FAST;
	cin >> n >> m >> k;
	for (int i = 0; i < n; ++i) {
		cin >> p[i]; num[p[i]] = i;
	}
	for (int i = 0; i < m; ++i) {
		cin >> s[i] >> a[i];
		a[i]--;
	}

	for (int i = 0; i < m; ++i) {
		bool is = 0;
		for (int mask = 0; mask < (1ll << k); ++mask) {
			string res = "";
			for (int j = 0; j < k; ++j) {
				if(mask & (1ll << j)) {
					res += s[i][j];
				}
				else {
					res += '_';
				}
			}
			if(num.find(res) != num.end()) {
				edges[a[i]].PB(num[res]);
				if(num[res] == a[i]) {
					is = 1;
				}
			}
		}
		if(!is) {
			return cout << "NO\n" , 0;
		}
	}
	for (int i = 0; i < n; ++i) {
		if(!mark[i]) {
			dfs(i);
		}
	}

	memset(mark , 0 , sizeof mark);
	reverse(_all(topol));
	for (auto v : topol) {
		for (auto u : edges[v]) {
			if(mark[u]) {
				return cout << "NO\n" , 0;
			}
		}
		mark[v] = 1;
	}

	cout << "YES\n";
	for (auto v : topol) {
		cout << v + 1 << ' ';
	}
	cout << '\n';
} 

//  Mistakes:
//  * Read the problem carefully.
//  * Check maxn.
//  * Overflows.