#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<unordered_map>
#include<cmath>
using namespace std;
int main()
{
int n, m,k, t1, t2,flag=0; int in[1010];
vector<int>v[1010];
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> t1 >> t2;
v[t1].push_back(t2);
in[t2]++;
}
cin >> k;
for (int i = 0; i < k; i++)
{
int judge = 1;
vector<int> tin(in, in + n + 1);
for (int j = 0; j < n; j++)
{
cin >> t1;
if (tin[t1] != 0) judge = 0;
for (int it:v[t1])
{
tin[it]--;
}
}
if (judge == 1) continue;
if (flag == 1)
cout << " ";
if(judge==0) cout << i;
flag = 1;
}
}