#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define per(i, a, b) for (int i = (a); i >= int(b); i--)
#define fir first
#define sec second
#define tct template<class type>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1e6;
int M, n, m, q, G, T, z, fa[maxn + 5], u[maxn + 5], v[maxn + 5], w[maxn + 5], a[maxn + 5];
bool f[2][3][maxn + 5], h[maxn + 5];

inline void red(int &x) { x += x >> 31 & T; }
tct inline void cmax(type &x, type y) { x < y ? x = y : 0; }
tct inline void cmin(type &x, type y) { x > y ? x = y : 0; }

int get(int u, int a, int b) {
	int c = a * 3 + b;
	return c * n + u;
}

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

int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main() {
	scanf("%d %d %d %d", &n, &m, &q, &M);
	rep(i, 1, m) {
		scanf("%d %d %d", &u[i], &v[i], &w[i]);
		a[i] = w[i];
	}
	G = M;
	sort(a + 1, a + m + 1);
	rep(i, 1, m - 1) G = gcd(G, a[i + 1] - a[i]);
	T = gcd(3 * G, M), z = w[1] % G;
	rep(i, 1, 6 * n) fa[i] = i;
	rep(i, 1, m) {
		(w[i] -= z) /= G;
		rep(a, 0, 1) rep(b, 0, 2) {
			fa[find(get(u[i], a, b))] = find(get(v[i], a ^ 1, (b * 2 + w[i]) % 3));
			fa[find(get(v[i], a, b))] = find(get(u[i], a ^ 1, (b * 2 + w[i]) % 3));
		}
	}
	rep(a, 0, 1) rep(b, 0, 2) {
		memset(h, false, sizeof(h));
		for (int x = (a + 1) % T; !h[x]; h[x] = true, x = 4 * x % T) {
			f[a][b][(1ll * x * z + b * G) % T] = true;
		}
	}
	for (int s, t, r; q --> 0; ) {
		scanf("%d %d %d", &s, &t, &r);
		(r += z) %= T;
		bool res = false;
		rep(a, 0, 1) rep(b, 0, 2) {
			res |= find(get(t, 0, 0)) == find(get(s, a, b)) && f[a][b][r];
		}
		puts(res ? "YES" : "NO");
	}
	return 0;
}