#include <bits/stdc++.h>
using namespace std;

const int N = 50005;
int n, m, q, MOD, a[N], b[N], c[N], fa[6 * N];
bool pw4[1000005];

int gcd(int a, int b) {
	return !b ? a : gcd(b, a % b);
}
int find(int x) {
	return !fa[x] ? x : fa[x] = find(fa[x]);
}
void link(int a, int b) {
	if (find(a) != find(b)) fa[find(a)] = find(b);
}
int id(int a, int b, int c) {
	return (a - 1) * 6 + b * 3 + c + 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m >> q >> MOD;
	int g = MOD;
	for (int i = 1; i <= m; ++i)
		cin >> a[i] >> b[i] >> c[i], g = gcd(g, abs(c[i] - c[1]));
	MOD = gcd(3 * g, MOD);
	int z = c[1] % g;
	for (int i = 1; i <= m; ++i) {
		int t = (c[i] - z) / g;
		for (int p = 0; p < 2; ++p)
			for (int q = 0; q < 3; ++q) {
				link(id(a[i], p, q), id(b[i], !p, (2 * q + t) % 3));
				link(id(b[i], p, q), id(a[i], !p, (2 * q + t) % 3));
			}
	}
	for (int i = 0, pw = z; i <= MOD; ++i, pw = 4 * pw % MOD) pw4[pw] = true;
	for (int s, t, r; q--;) {
		cin >> s >> t >> r;
		bool ans = false;
		for (int p = 0; p < 2 && !ans; ++p)
			for (int q = 0; q < 3 && !ans; ++q)
				ans = find(id(s, p, q)) == find(id(t, 0, 0)) && pw4[(r + z + (3 - q) * g << p) % MOD];
		cout << (ans ? "YES\n" : "NO\n");
	}
}
