#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
if (n < 1 || m < 1 || n > 100000 || m > 100000) cout << "???" << endl;
int c = 1e9;
rep(i, 1, m) {
int cc;
cin >> cc;
if (cc < 1 || cc > 1000000000) cout << "???" << endl;
c = min(c, cc);
}
map<string, int> cnt;
vector<int> ans(n + 1, 0);
vector<tuple<int, int, string>> s;
rep(i, 1, n) {
int w;
string nam;
cin >> w >> nam;
if (w < 1 || w > 1000000000 || nam.size() > 10) cout << "???" << endl;
s.eb(w, i, nam);
}
sort(all(s));
reverse(all(s));
int pre = 0, lstw = 0;
for (auto [w, id, nam] : s) {
if (w == lstw) cout << "???" << endl;
lstw = w;
ans[id] = pre + (cnt[nam] < c);
if (cnt[nam] < c) ++pre;
++cnt[nam];
}
rep(i, 1, n) printf("%d\n", ans[i]);
return 0;
}