Language: C++

#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 puts("NMSL"), 0;
	if(b < 1 || b > 6 * n) return puts("NMSL"), 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(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;
}

Compiler output:
a.cc: In function 'void merge(ll, ll)':
a.cc:99:51: error: return-statement with a value, in function returning 'void' [-fpermissive]
   99 |         if(a < 1 || a > 6 * n) return puts("NMSL"), 0;
      |                                       ~~~~~~~~~~~~^~~
a.cc:100:51: error: return-statement with a value, in function returning 'void' [-fpermissive]
  100 |         if(b < 1 || b > 6 * n) return puts("NMSL"), 0;
      |                                       ~~~~~~~~~~~~^~~