#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <limits.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int n, m, tstart, jstart, jtrys, hole, i, j, node1, node2, type;
cin >> n >> m >> tstart >> jtrys >> hole;
tstart--, hole--;
vector<vector<int>> jg(n);
map<int, vector<int>> tg;
for (i = 0; i < m; i++)
{
cin >> node1 >> node2 >> type;
node1--, node2--;
jg[node1].push_back(node2);
jg[node2].push_back(node1);
if (type == 2)
{
if (!tg.count(node1))
{
tg[node1] = {};
}
if (!tg.count(node2))
{
tg[node2] = {};
}
tg[node1].push_back(node2);
tg[node2].push_back(node1);
}
}
queue<int> q;
q.push(tstart);
vector<int> tdist(n, INT_MAX);
tdist[tstart] = 0;
while (q.size() > 0)
{
for (i = 0; i < tg[q.front()].size(); i++)
{
if (tdist[tg[q.front()][i]] == INT_MAX) //!been
{
tdist[tg[q.front()][i]] = tdist[q.front()] + 1;
q.push(tg[q.front()][i]);
}
}
q.pop();
}
queue<pair<int, int>> q2;
q2.push({ hole, tdist[hole] });
vector<int> timeleft(n, -1);
timeleft[hole] = INT_MAX;
while (q2.size() > 0)
{
for (i = 0; i < jg[q2.front().first].size(); i++)
{
if (timeleft[jg[q2.front().first][i]] == -1)
{
timeleft[jg[q2.front().first][i]] = min(tdist[jg[q2.front().first][i]], q2.front().second - 1);
q2.push({ jg[q2.front().first][i], timeleft[jg[q2.front().first][i]] });
}
}
q2.pop();
}
for (j = 0; j < jtrys; j++)
{
cin >> jstart;
jstart--;
if (timeleft[jstart] > 0)
{
cout << "IGEN" << endl;
}
else
{
cout << "NEM" << endl;
}
}
}
//9 10 6 3 1
//1 2 1
//1 3 1
//2 4 1
//3 4 2
//4 7 1
//3 5 2
//5 6 2
//6 8 1
//7 9 1
//8 9 1
//7
//8
//9