#include using namespace std; char tmp[105]; string readString() { scanf("%s", tmp); return string(tmp); } int main() { set specials; int cnt_spe, m; scanf("%d%d", &cnt_spe, &m); while(cnt_spe--) { int x; scanf("%d", &x); assert(!specials.count(x)); specials.insert(x); assert(1 <= x && x <= 100 * 1000); } vector, string>> all; for(int i = 0; i < m; ++i) { int who, p; scanf("%d%d", &who, &p); all.push_back(make_pair( make_pair(specials.count(who), p), readString() )); } sort(all.rbegin(), all.rend()); // reverse sorting for(auto pp : all) printf("%s\n", pp.second.c_str()); }