#include <bits/stdc++.h>
using namespace std;
const int N = 110;
stack<char> stk[N];
int main() {
int n, m, S;
cin >> n >> m >> S;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = (int)s.size() - 1; j >= 0; j--) {
stk[i].push(s[j]);
}
}
stack<char> kuang;
int op;
while (cin >> op) {
if (op == -1) return 0;
if (op == 0) {
//匡为空 筐不为空;
if (kuang.empty()) {
continue;
} else {
cout << kuang.top();
kuang.pop();
}
} else {
if (stk[op].empty()) continue;
int t = stk[op].top();
stk[op].pop();
if (kuang.size() == S) {
cout << kuang.top();
kuang.pop();
}
kuang.push(t);
}
}
return 0;
}