#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;

int k;


struct edge {
	int u, v; edge() {}
	edge(int _u, int _v) :
		u(_u), v(_v) {}
};

struct node { int id; vector<edge> edges; };

struct graph {
	vector<node> nodes; int n;
	graph(int _n) : n(_n) { nodes.resize(n); }

	void add_edge(int u, int v) {
		edge e1(u, v); nodes[u].edges.push_back(e1);
	}

	bool cycle_dfs(int u, vector<int>& color, vector<int>& parent) {
		color[u] = 1;
		for (auto& e : nodes[u].edges) {
			int v = e.v;
			if (color[v] == 0) {
				parent[v] = u;
				if (cycle_dfs(v, color, parent)) { return true; }
			}
			else if (color[v] == 1) { parent[v] = u; return true; }	//One node in the cycle would be u or v
		}
		color[u] = 2; return false;
	}
	bool find_cycle() {
		vector<int> color(n, 0), parent(n, -1);
		for (int v = 0; v < n; v++)
			if (cycle_dfs(v, color, parent)) { return true; }
		return false;
	}

	void dfs(int u, vector<int>& visited, vector<int>& ans) {
		visited[u] = true;
		for (auto& e : nodes[u].edges) {
			int v = e.v;
			if (!visited[v]) { dfs(v, visited, ans); }
		}
		ans.push_back(u);
	}
	vector<int> topological_sort() {
		vector<int> visited(n, false), ans;
		for (int i = 0; i < n; ++i)
			if (!visited[i]) { dfs(i, visited, ans); }
		reverse(ans.begin(), ans.end());
		return move(ans);
	}
};

int pToInt(string& s) {
	int ans = 0;
	for (int i = 0; i < k; i++) {
		ans *= 27;
		if (s[i] == '_') { ans += 26; }
		else { ans += s[i] - 'a'; }
	}
	return ans;
}

struct pattern {
	int id, matches, minIdx;
	int ansID;

	pattern(int _ai, int _i, int _m, int _idx) : ansID(_ai), id(_i), matches(_m), minIdx(_idx) {}
	bool operator<(const pattern& rhs) {
		if (minIdx != rhs.minIdx) { return minIdx < rhs.minIdx; }
		return matches > rhs.matches;
	}
};

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n, m; cin >> n >> m >> k;
	vector<pair<int, pair<int, int>>> patterns(6e5, { -1,{ -1,-1 } });	//patternID -> {ansID,{matches, maxIdx}}
	graph g(6e5);
	for (int i = 0; i < n; i++) {
		string s; cin >> s;
		int id = pToInt(s);
		g.nodes[id].id = i;
		patterns[id] = { i, { 0,0 } };
	}
	
	bool OK = true;
	vector<pair<int, vector<int>>> str;
	for (int i = 0; i < m; i++) {
		string s; cin >> s;
		int mt; cin >> mt; mt--;
		str.push_back({ mt, vector<int>() });
		int source = -1;
		for (int j = 0; j < (1 << k); j++) {
			string curP = s;
			for (int kk = 0; kk < k; kk++) {
				if (j & (1 << kk)) { curP[kk] = '_'; }
			}
			int corrP = pToInt(curP);
			str.back().second.push_back(corrP);
			if (patterns[corrP].first != -1) {
				if (patterns[corrP].first == mt) { source = corrP; }
				patterns[corrP].second.first++;
				patterns[corrP].second.second = max(patterns[corrP].second.second, mt);
			}
		}
		
		if (source == -1) { OK = false; continue; }
		for (auto& x : str.back().second) {
			if (x == source) { continue; }
			g.add_edge(source, x);
		}
	}

	if (!OK || g.find_cycle()) { cout << "NO" << '\n'; return 0; }
	vector<int> res = g.topological_sort();
	cout << "YES" << '\n';
	for (auto& x : res) {
		if (patterns[x].first != -1) { cout << g.nodes[x].id + 1 << ' '; }
	}
	cout<<'\n';
	/*
	vector<pattern> orderedP;
	for (int i = 0; i < 6e5; i++) {
		if (patterns[i].first == -1) { continue; }
		orderedP.push_back(pattern(patterns[i].first, i, patterns[i].second.first, patterns[i].second.second));
	}
	sort(orderedP.begin(), orderedP.end());

	vector<int> ans(n), joker;
	int i = 0; bool OK = true;
	while (i < n) {
		if (orderedP[i].minIdx > i) {
			if (joker.empty()) { OK = false; break; }
			ans[i] = joker.back(); joker.pop_back();
			i++; continue;
		}
		ans[i] = orderedP[i].ansID;
		int fI = i; i++;
		while (i < n && orderedP[i].minIdx == fI) { joker.push_back(orderedP[i].ansID); i++; }
	}
	if (!OK) { cout << "NO" << '\n'; return 0; }

	for (int i = 0; i < m; i++) {
		int mt = str[i].first;
		bool mtOK = false;
		for (auto& x : str[i].second) {
			if (patterns[x].first == -1) { continue; }
			if (patterns[x].first == mt) { mtOK = true; }
			if (patterns[x].first < mt) { OK = false; break; }
		}
		if (!mtOK) { OK = false; break; }
	}
	if (!OK) { cout << "NO" << '\n'; return 0; }

	cout << "YES" << '\n';
	for (auto& x : ans) { cout << x + 1 << " "; }
	cout << '\n';
	*/
}