#include <bits/stdc++.h>
using namespace std;

#ifdef GLOCAL 
#include "e:/Codes/lib/prettyprint.hpp"
#else
#define debug(...) 
#endif

int loop = 0;
vector<vector<int>> g;
vector<int> vis, ord;

void dfs(int x) {
  vis[x] = 2;
  for (auto v : g[x]) {
    if (vis[v] == 1) continue;
    if (vis[v] == 2) loop = 1;
    else dfs(v);
  }
  vis[x] = 1;
  ord.push_back(x);
}


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

  int n, m, k;
  cin >> n >> m >> k;
  
  map<string, int> pid;
  vector<string> p(n), s(m);
  vector<int> mt(m);
  g.resize(n);
  vis.resize(n, 0);

  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    pid[p[i]] = i;
  }
  
  for (int i = 0; i < m; ++i) {
    cin >> s[i] >> mt[i];
    --mt[i];
    
    for (int j = 0; j < k; ++j) {
      if (p[mt[i]][j] == '_') continue;
      if (p[mt[i]][j] != s[i][j]) {
        cout << "NO" << '\n';
        return 0;
      }
    }

    vector<int> cur;
    for (int j = 0; j < (1<<k); ++j) {
      string tmp = s[i];
      for (int l = 0; l < k; ++l) {
        if ((j>>l)&1) tmp[l] = '_';
      }

      if (pid.count(tmp)) cur.push_back(pid[tmp]);
    }
  
    for (auto j : cur) {
      if (j == mt[i]) continue;
      g[mt[i]].push_back(j);
    }
  }
  
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  
  debug(g);
  if (loop) cout << "NO" << '\n';
  else {
    cout << "YES" << "\n";
    for (int i = n - 1; i >= 0; --i) cout << ord[i] + 1 << " ";
    cout << '\n';
  }
}
