#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 400 + 10;
int n, g[maxn][maxn];
int id[maxn], love[maxn];
int left[maxn];
bool visy[maxn];
inline bool cmp(int a, int b) {
return love[a] > love[b];
}
bool dfs(int u) {
for(int i = 1; i <= n; ++i) {
if(g[u][i] == 1 && !visy[i]) {
visy[i] = true;
if(left[i] == -1 || dfs(left[i])) {
left[i] = u;
return true;
}
}
}
return false;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &love[i]);
id[i] = i;
}
for(int i = 1, t, u; i <= n; ++i) {
scanf("%d", &t);
for(int j = 1; j <= t; ++j) {
scanf("%d", &u);
g[i][u] = 1;
}
}
sort(id + 1, id + n + 1, cmp);
memset(left, -1, sizeof left);
for(int i = 1; i <= n; ++i) {
memset(visy, false, sizeof visy);
dfs(id[i]);
}
memset(love, 0, sizeof love);
for(int i = 1; i <= n; ++i) {
if(left[i] != -1) {
love[left[i]] = i;
}
}
for(int i = 1; i < n; ++i) {
printf("%d ", love[i]);
}
printf("%d\n", love[n]);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBtYXhuID0gNDAwICsgMTA7CmludCBuLCBnW21heG5dW21heG5dOwppbnQgaWRbbWF4bl0sIGxvdmVbbWF4bl07CmludCBsZWZ0W21heG5dOwpib29sIHZpc3lbbWF4bl07CmlubGluZSBib29sIGNtcChpbnQgYSwgaW50IGIpIHsKCXJldHVybiBsb3ZlW2FdID4gbG92ZVtiXTsKfQpib29sIGRmcyhpbnQgdSkgewoJZm9yKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKCQlpZihnW3VdW2ldID09IDEgJiYgIXZpc3lbaV0pIHsKCQkJdmlzeVtpXSA9IHRydWU7CgkJCWlmKGxlZnRbaV0gPT0gLTEgfHwgZ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