// tomesjerry.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <queue>
#define ll long long
using namespace std;
ll n, m, t, p, E;
struct adat
{
ll bet=0,bej=0;
bool lat = false,latt=false;
vector<pair<ll,ll>>sz;
};
int main()
{
cin >> n >> m >> t >> p >> E;
vector<adat>x(n + 1);
for (int i = 1; i <= n; ++i)
{
int a, b,c;
cin >> a >> b>>c;
x[a].sz.push_back({ b,c });
x[b].sz.push_back({ a,c });
}
deque<ll>v;
v.push_back(t);
while (!v.empty())
{
ll akt = v.front();
x[akt].latt = true;
v.pop_front();
for (auto& e : x[akt].sz)
{
if (!x[e.first].latt && e.second == 2)
{
x[e.first].bet = x[akt].bet + 1;
v.push_back(e.first);
}
}
}
while (p--)
{
ll akt;
cin >> akt;
v.push_back(akt);
while (!v.empty())
{
akt = v.front();
v.pop_front();
x[akt].lat = true;
for (auto& e : x[akt].sz)
{
if (!x[e.first].lat && (x[e.first].bet > x[akt].lat + 1 || (x[e.first].bet==0 && e.first!=t)))
{
x[e.first].bej = x[akt].bej + 1;
v.push_back(e.first);
}
}
}
if (x[E].lat == true)cout << "IGEN\n";
else cout << "NEM\n";
for (int i = 1; i <= n; ++i)
{
x[i].bej = 0;
x[i].lat = false;
}
}
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file