#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node{
vector<int> tom, jerry;
int biztonsag=-5, macska=1000000;
bool biztonsagos=0;
};
int main()
{
int n, m, p, tpos, epos, b, c, d;
cin >> n >> m >> tpos >> p >> epos;
node a[n];
for(int i=0; i<m; i++){
cin >> b >> c >> d;
b--;
c--;
a[b].jerry.push_back(c);
a[c].jerry.push_back(b);
if(d==2){
a[b].tom.push_back(c);
a[c].tom.push_back(b);
}
}
tpos--;
epos--;
queue<int> q;
q.push(tpos);
a[tpos].macska=0;
while(!q.empty()){
b=q.front();
q.pop();
//cout << b+1 << " -> ";
for(int& it:a[b].tom){
if(a[it].macska==1000000){
a[it].macska=a[b].macska+1;
q.push(it);
}
}
}
q.push(epos);
a[epos].biztonsagos=1;
a[epos].biztonsag=1000000;
while(!q.empty()){
b=q.front();
q.pop();
for(int& it:a[b].jerry){
if(a[b].biztonsag==1000000&&a[it].macska==1000000&&a[it].biztonsag==-5){
a[it].biztonsag=1000000;
a[it].biztonsagos=1;
q.push(it);
}
else if(a[b].biztonsag>1&&a[b].biztonsag-1>a[it].biztonsag){
a[it].biztonsag=min(a[b].biztonsag-1, a[it].macska);
a[it].biztonsagos=1;
q.push(it);
}
}
}
/*for(int i=0; i<n; i++){
cout << '\n' << i+1 << ' ';
if(a[i].biztonsagos){
cout << "IGEN";
}
else{
cout << "NEM";
}
}*/
for(int i=0; i<p; i++){
cin >> b;
b--;
if(a[b].biztonsagos){
cout << "IGEN";
}
else{
cout << "NEM";
}
cout << '\n';
}
return 0;
}