Problem: You are given a connected graph with N vertices and M edges. The vertices are numbered 1 to N. The i-th edge is an undirected edge of length C_i connecting Vertex A_i and Vertex B_i.

Additionally, an odd number MOD is given.

You will be given Q queries, which should be processed. The queries take the following form:

* Given in the i-th query are S_i, T_i and R_i. Print `YES` if there exists a path from Vertex S_i to Vertex T_i whose length is R_i modulo MOD, and print `NO` otherwise. A path may traverse the same edge multiple times, or go back using the edge it just used.



Here, in this problem, the length of a path is NOT the sum of the lengths of its edges themselves, but the length of the first edge used in the path gets multiplied by 1, the second edge gets multiplied by 2, the third edge gets multiplied by 4, and so on. (More formally, let L_1,...,L_k be the lengths of the edges used, in this order. The length of that path is the sum of L_i \times 2^{i-1}.)

Constraints

* 1 \leq N,M,Q \leq 50000
* 3 \leq MOD \leq 10^{6}
* MOD is odd.
* 1 \leq A_i,B_i\leq N
* 0 \leq C_i \leq MOD-1
* 1 \leq S_i,T_i \leq N
* 0 \leq R_i \leq MOD-1
* The given graph is connected. (It may contain self-loops or multiple edges.)

Input

Input is given from Standard Input in the following format:


N M Q MOD
A_1 B_1 C_1
\vdots
A_M B_M C_M
S_1 T_1 R_1
\vdots
S_Q T_Q R_Q


Output

Print the answers to the i-th query in the i-th line.

Examples

Input

3 2 2 2019
1 2 1
2 3 2
1 3 5
1 3 4


Output

YES
NO


Input

6 6 3 2019
1 2 4
2 3 4
3 4 4
4 5 4
5 6 4
6 1 4
2 6 1110
3 1 1111
4 5 1112


Output

YES
NO
NO


Input

1 2 3 25
1 1 1
1 1 2
1 1 13
1 1 6
1 1 14


Output

YES
YES
YES


Input

10 15 10 15
1 2 1
2 3 6
3 4 6
2 5 1
5 6 1
4 7 6
1 8 11
2 9 6
5 10 11
9 10 11
3 6 1
2 5 1
2 7 11
9 10 11
5 6 11
1 3 5
9 8 3
7 7 7
7 10 13
4 1 10
9 3 12
10 10 14
9 2 1
6 6 5
8 8 4


Output

YES
NO
NO
NO
NO
NO
NO
YES
YES
NO

Solution (in CPP):

Solution:
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define per(i, a, b) for (ll i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (ll i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((ll)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <ll, ll> pii ;
typedef vector <ll> vi ;
typedef vector <pii> vii ;
typedef queue <ll> qi ;
typedef queue <pii> qii ;
typedef set <ll> si ;
typedef map <ll, ll> mii ;
typedef map <string, ll> msi ;
const ll maxn = 2e6 + 100 ;
const ll inf = 0x3f3f3f3f ;
const ll iinf = 1 << 30 ;
const ll linf = 2e18 ;
// const ll mod = 998244353 ;
const double eps = 1e-7 ;
template <class T = ll> T chmin(T &a, T b) { return a = min(a, b);}
template <class T = ll> T chmax(T &a, T b) { return a = max(a, b);}
template <class T = ll> T read()
{
	T f = 1, a = 0;
	char ch = getchar() ;
	while (!isdigit(ch)) { if (ch == '-') f = -1 ; ch = getchar() ; }
	while (isdigit(ch)) { a =  (a << 3) + (a << 1) + ch - '0' ; ch = getchar() ; }
	return a * f ;
}

ll n, m, q, tmp_mod, t, z;

ll u[maxn], v[maxn], w[maxn];

ll fa[maxn];

ll gcd(ll a, ll b)
{
	return !b ? a : gcd(b, a % b);
}

ll getfa(ll a)
{
	return a == fa[a] ? a : fa[a] = getfa(fa[a]) ;
}

void init()
{
	rep(i, 1, 6 * n) fa[i] = i;
}

void merge(ll a, ll b)
{
	// printf("%lld -> %lld\n", a, b);
	if(a < 1 || a > 6 * n) return (void)(puts("NMSL"), exit(0));
	if(b < 1 || b > 6 * n) return (void)(puts("NMSL"), exit(0));
	a = getfa(a), b = getfa(b);
	if(a != b) fa[a] = b;
}

vi edge[maxn];

void add(ll u, ll v, ll w)
{
	merge(u, v + (3 + w) * n);
	merge(u + n, v + ((2 + w) % 3 + 3) * n);
	merge(u + 2 * n, v + ((4 + w) % 3 + 3) * n);
	merge(u + 3 * n, v + w * n);
	merge(u + 4 * n, v + ((2 + w) % 3) * n);
	merge(u + 5 * n, v + ((4 + w) % 3) * n);
}

ll vis[maxn][2], flag[maxn][2];

signed main()
{
	n = read(), m = read(), q = read(), tmp_mod = read();
	rep(i, 1, m) u[i] = read(), v[i] = read(), w[i] = read(), t = gcd(abs(w[i] - w[1]), t);
	init();
	if(!t) t = tmp_mod;
	z = w[1] % t;
	// printf("nmsl : %lld %lld\n", t, z);
	rep(i, 1, m)
	{
		w[i] = (w[i] - z) / t % 3;
		// printf("%lld\n", w[i]);
		// printf("%lld %lld\n", u[i], v[i]);
		add(u[i], v[i], w[i]), add(v[i], u[i], w[i]);
	}
	// return 0;
	ll c = gcd(3 * t, tmp_mod);
	// printf("%lld\n", c);
	ll tmp = 2;
	while(1)
	{
		if(vis[tmp][1]) break;
		vis[tmp][1] = 1, flag[tmp * z % c][1] = 1;
		// printf("%lld %lld\n", tmp * z % c, 1);
		(tmp *= 4) %= c;
	}
	tmp = 1;
	while(1)
	{
		if(vis[tmp][0]) break;
		vis[tmp][0] = 1, flag[tmp * z % c][0] = 1;
		// printf("%lld %lld\n", tmp * z % c, 0);
		(tmp *= 4) %= c;
	}
	// rep(i, 1, 6 * n) printf("%lld ", getfa(i));
	// puts("");
	// printf("%lld\n", t);
	while(q --)
	{
		ll u = read(), v = read(), r = read();
		swap(u, v);
		ll ans = 0;
		rep(i, 0, 1) rep(j, 0, 2)
		{
			// printf("%lld %lld : \n", i, j);
			// printf("%lld %lld\n", getfa(u) == getfa(v + (i * 3 + j) * n), (z + r + c - j * t % c) % c);
			if(getfa(u) == getfa(v + (i * 3 + j) * n) && flag[(z + r + c - j * t % c) % c][i]) ans = 1;
		}
		puts(ans ? "YES" : "NO");
	}
	return 0;
}