Keep In Touch

Accepts: 22
Submissions: 280
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/131072 K (Java/Others)
Problem Description
There are $n$ cities numbered with successive integers from $1$ to $n$ in Byteland. Also, there are $m$ one-way roads connecting the cities. The starting point of the $i$-th road is $u_i$ while the ending point is $v_i$. There are $3$ spies : 007, 008 and 009. They are going to start $q$ secret missions. During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of the $i$-th city is $w_i$. Two spies can contact with each other if and only if the absolute value of the difference of radio frequency between the cities where they are in is no more than $K$. At each moment, three spies must choose a road and go to another city. The time for traveling each road is only a unit of time. They can choose to end the mission in any city, even in the city where they started the mission. But they are not allowed to end mission in the middle of the roads. Now they want to know, for each mission whose start points are given, what's the number of possible ways, during which they can contact with each other at any moment when they are not on roads? Two ways are considered different if and only if there exists at least one spy at different cities on the same moment. Note : $3$ spies must end the mission at the same time.
Input
The first line of the input contains an integer $T$ $(1\leq T\leq 10)$, denoting the number of test cases. In each test case, the first line of the input contains four integers $n$ $(1\leq n\leq 50),m(0\leq m\leq \frac{n(n-1)}{2}),K(0\leq K\leq 10^9),q(1\leq q\leq 125000)$, denoting the number of cities, the number of roads, the upper limit of interphone and the number of missions. The second line of the input contains $n$ integers $w_1, w_2, ..., w_n$ $(1\leq w_i\leq 10^9)$, denoting the radio frequency of the $i$-th city. Each of the next $m$ lines contains two integers $u_i,v_i$ $(1\leq u_i < v_i\leq n)$, denoting an one-way road. There are no multiple edges in the graph. Each of the next $q$ lines contains three integers $x,y,z(1\leq x,y,z\leq n)$, denoting the starting point of the three spies in each mission. You can assume that there are at least one possible way in each mission.
Output
For each test case, print $q$ lines with one integer per line. For each mission, print the number of possible ways modulo $998244353$.
Sample Input
1
4 4 2 10
8 8 4 1
1 3
1 4
2 3
2 4
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
3 3 3
4 4 4
Sample Output
3
3
3
3
3
3
3
3
1
1