#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10, mod = 1e9 + 7;
struct node{
int a, b, id, s, t;
bool operator<(const node &w) const{
return s < w.s;
}
};
signed main()
{
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m, x; cin >> n >> m >> x;
vector<node> train(m + 1);
for(int i = 1; i <= m; i++){
int a, b, s, t; cin >> a >> b >> s >> t;
train[i] = {a, b, i, s, t};
}
vector<int> res(m + 1), dp(n + 1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que[n + 1];
que[train[1].b].push({train[1].t, train[1].t + x});
dp[train[1].b] = train[1].t + x;
sort(train.begin() + 1, train.end());
for(int i = 1; i <= m; i++){
int a = train[i].a, b = train[i].b, s = train[i].s, t = train[i].t, id = train[i].id;
while(!que[a].empty()){
int old = que[a].top().first, now = que[a].top().second;
if(s < old) break;
que[a].pop();
dp[a] = max(dp[a], now);
}
res[id] = max(0LL, dp[a] - s);
que[b].push({t, t + res[id]});
}
for(int i = 2; i <= m; i++) cout << res[i] << ' ';
return 0;
}