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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define pb push_back
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

const int INF=1001001001;

int N,M,R,Q;

vector<pint>G[111111];

void  dijkstra(int r,vint &dist){
    dist.resize(N);
    fill(all(dist),INF);
    priority_queue<pint,vector<pint>,greater<pint>>que;
    rep(i,N)if(i%R==r)dist[i]=0,que.push({0,i});
    while(que.size()){
        int v,c;
        tie(c,v)=que.top();
        que.pop();
        if(dist[v]<c)continue;
        for(auto &e:G[v]){
            if(dist[e.fi]<=c+e.se)continue;
            dist[e.fi]=c+e.se;
            que.push({dist[e.fi],e.fi});
        }
    }
}

signed main(){
    scanf("%lld%lld%lld%lld",&N,&M,&R,&Q);

    rep(i,M){
        int a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        G[b].pb({a,c});
    }

    vector<vint>dist(R);
    rep(i,R)dijkstra(i,dist[i]);

    int ans=0;
    while(Q--){
        int x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        x--;y--;
        int mi=INF;
        rep(i,R)chmin(mi,dist[i][x]+dist[i][y]);
        ans+=max(0ll,z-mi);
    }
    printf("%lld\n",ans);
    return 0;
}