#include <bits/stdc++.h>

#define task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, m, K, pw[4], pos[531450];
vi g[N];
int index(char x)
{
    if (x == '_') return 26;
    return x - 'a';
}
int tonum(string s)
{
    int res = 0;
    Rep(i, SZ(s) - 1, 0) res += index(s[i]) * pw[SZ(s) - i - 1];
    return res;
}
vi st;
bool vis[N], in[N];
bool dfs(int u)
{
    in[u] = 1;
    vis[u] = 1;
    for (int v : g[u])
    {
        if (in[v]) return 0;
        if (!vis[v] && !dfs(v)) return 0;
    }
    st.eb(u);
    in[u] = 0;
    return 1;
}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n >> m >> K;
    pw[0] = 1;
    rep(i, 1, 3) pw[i] = pw[i - 1] * 27;
    rep(i, 1, n)
    {
        string s;
        cin >> s;
        int id = tonum(s);
        pos[id] = i;
    }
    rep(i, 1, m)
    {
        int p;
        string s;
        cin >> s >> p;
        vi tem;
        bool ok = 0;
        rep(x, 0, 15)
        {
            string t = s;
            rep(j, 0, 3) if (x >> j & 1) t[j] = '_';
            int po = pos[tonum(t)];
            if (po) tem.eb(po);
            if (po == p) ok = 1;
        }
        if (!ok)
        {
            cout << "NO";
            return 0;
        }
        for (int v : tem) if (v != p) g[p].eb(v);
    }
    rep(i, 1, n) if (SZ(g[i])) sort(all(g[i])), g[i].erase(unique(all(g[i])), g[i].end());
    rep(i, 1, n) if (!vis[i] && !dfs(i))
    {
        cout << "NO";
        return 0;
    }
    cout << "YES\n";
    Rep(i, n - 1, 0) cout << st[i] << ' ';
    return 0;
}
