#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1010,M = 1e4+10;
int h[N],topse[N],e[M],ne[M],d[N],c[N],idx,n,m;
void insert(int a,int b){
e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}
bool topjud(){
memset(c,0,sizeof c);
memcpy(c,d,sizeof c);
bool flag = true;
for(int i = 0;i<n;++i){
int t = topse[i];
if(c[t]){
flag = false;
break;
}
for(int j = h[t];~j;j=ne[j]){
c[e[j]]--;
}
}
return flag;
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i = 0;i<m;++i){
int a,b;
scanf("%d%d",&a,&b);
insert(a,b);
d[b]++;
}
int t,T= 0;
cin>>t;
while(t--){
for(int i = 0;i<n;++i){
scanf("%d",&topse[i]);
}
if(!topjud()) cout<<T<<" ";
T++;
}
return 0;
}