There are N cities numbered 1 to N, connected by M railroads.
You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket.
The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes.
You cannot use gold coins to pay the fare.
There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin.
The transaction takes D_i minutes for each gold coin you give.
You can exchange any number of gold coins at each exchange counter.
For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains.

-----Constraints-----
 - 2 \leq N \leq 50
 - N-1 \leq M \leq 100
 - 0 \leq S \leq 10^9
 - 1 \leq A_i \leq 50
 - 1 \leq B_i,C_i,D_i \leq 10^9
 - 1 \leq U_i < V_i \leq N
 - There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j).
 - Each city t=2,...,N can be reached from City 1 with some number of railroads.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N M S
U_1 V_1 A_1 B_1
:
U_M V_M A_M B_M
C_1 D_1
:
C_N D_N

-----Output-----
For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t.

-----Sample Input-----
3 2 1
1 2 1 2
1 3 2 4
1 11
1 2
2 5

-----Sample Output-----
2
14

The railway network in this input is shown in the figure below.
In this figure, each city is labeled as follows:
 - The first line: the ID number i of the city (i for City i)
 - The second line: C_i / D_i
Similarly, each railroad is labeled as follows:
 - The first line: the ID number i of the railroad (i for the i-th railroad in input)
 - The second line: A_i / B_i

You can travel from City 1 to City 2 in 2 minutes, as follows:
 - Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.


You can travel from City 1 to City 3 in 14 minutes, as follows:
 - Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.
 - At the exchange counter in City 2, exchange 3 gold coins for 3 silver coins in 6 minutes.
 - Use the 1-st railroad to move from City 2 to City 1 in 2 minutes.
 - Use the 2-nd railroad to move from City 1 to City 3 in 4 minutes.

Difficulty: MEDIUM

Solutions:
import math
import queue
from collections import deque, defaultdict
import heapq as hpq
from sys import stdin, setrecursionlimit
ipt = stdin.readline
setrecursionlimit(10 ** 7)

def main():
	(n, m, s) = map(int, ipt().split())
	mac = 2500
	way = [[] for i in range(n + 1)]
	for _ in range(m):
		(u, v, a, b) = map(int, ipt().split())
		way[u].append((v, a, b))
		way[v].append((u, a, b))
	for i in range(1, n + 1):
		(c, d) = map(int, ipt().split())
		way[i].append((i, -c, d))
	ct = [[10 ** 18] * 2501 for i in range(n + 1)]
	ct[1][min(s, mac)] = 0
	hq = [(0, 1, min(s, mac))]
	while hq:
		(ti, pl, mo) = hpq.heappop(hq)
		for (dp, dm, dt) in way[pl]:
			if mo >= dm and ct[dp][min(mo - dm, mac)] > ti + dt:
				ct[dp][min(mo - dm, mac)] = ti + dt
				hpq.heappush(hq, (ti + dt, dp, min(mo - dm, mac)))
	ans = [min(i) for i in ct[2:]]
	for i in ans:
		print(i)
	return
main()


(N, M, S) = map(int, input().split())
from heapq import heappop, heappush
inf = float('inf')
cost = [[] for _ in range(N)]
exc = []
for _ in range(M):
	(u, v, a, b) = map(int, input().split())
	(u, v) = (u - 1, v - 1)
	cost[u].append((v, a, b))
	cost[v].append((u, a, b))
for _ in range(N):
	(c, d) = map(int, input().split())
	exc.append((c, d))
di = [inf] * 2451 * N
if S > 2450:
	S = 2450
di[S] = 0
hq = [(0, S)]
while hq:
	(t, v) = heappop(hq)
	u = v // 2451
	s = v % 2451
	for (x, a, b) in cost[u]:
		if s >= a:
			v_new = x * 2451 + s - a
			t_new = t + b
			if t_new < di[v_new]:
				di[v_new] = t_new
				heappush(hq, (t_new, v_new))
	(c, d) = exc[u]
	if s + c > 2450:
		c = 2450 - s
	v_new = v + c
	t_new = t + d
	if t_new < di[v_new]:
		di[v_new] = t_new
		heappush(hq, (t_new, v_new))
for i in range(1, N):
	m = di[i * 2451]
	for j in range(1, 2451):
		m = min(m, di[i * 2451 + j])
	print(m)


import sys
from heapq import heapify, heappop, heappush

def solve():
	input = sys.stdin.readline
	(N, M, S) = map(int, input().split())
	Edge = [[] for _ in range(N)]
	maxCost = 0
	for _ in range(M):
		(u, v, a, b) = map(int, input().split())
		Edge[u - 1].append((b, v - 1, a))
		Edge[v - 1].append((b, u - 1, a))
		maxCost = max(a, maxCost)
	exchange = [None] * N
	for i in range(N):
		exchange[i] = [int(c) for c in input().split()]
	maxCost *= N
	minTime = [[10 ** 20 for _ in range(maxCost + 1)] for i in range(N)]
	q = []
	heapify(q)
	heappush(q, (0, 0, S))
	while q:
		(cT, cN, cM) = heappop(q)
		if minTime[cN][min(maxCost, cM)] == 10 ** 20:
			minTime[cN][min(maxCost, cM)] = cT
			if cM < maxCost:
				heappush(q, (cT + exchange[cN][1], cN, cM + exchange[cN][0]))
			for (addTime, nextNode, payCost) in Edge[cN]:
				if cM >= payCost:
					heappush(q, (cT + addTime, nextNode, cM - payCost))
	for i in range(1, N):
		print(min(minTime[i]))
	return 0
solve()


from heapq import heappush, heappop
(N, M, S) = map(int, input().split())
mxCost = 0
edges = [[] for _ in range(N)]
for _ in range(M):
	(fr, to, cost, dist) = map(int, input().split())
	mxCost = max(mxCost, cost)
	fr -= 1
	to -= 1
	edges[fr].append((to, dist, cost))
	edges[to].append((fr, dist, cost))
for i in range(N):
	(C, D) = map(int, input().split())
	edges[i].append((i, D, -C))
que = [(0, S, 0)]
mxCost *= N - 1
minDsit = [[10 ** 18] * (mxCost + 1) for _ in range(N)]
minDsit[0][0] = 0
while que:
	(dist, s, now) = heappop(que)
	for (to, d, c) in edges[now]:
		cost = min(mxCost, s - c)
		if cost < 0 or minDsit[to][cost] <= dist + d:
			continue
		minDsit[to][cost] = dist + d
		heappush(que, (dist + d, cost, to))
for dist in minDsit[1:]:
	print(min(dist))


(n, m, s) = map(int, input().split())
adj = [[] for i in range(n)]
for _ in range(m):
	(u, v, a, b) = map(int, input().split())
	adj[u - 1].append((v - 1, -a, b))
	adj[v - 1].append((u - 1, -a, b))
for i in range(n):
	(c, d) = map(int, input().split())
	adj[i].append((i, c, d))
out = [-1] * n
count = 0
LARGE = 50 * n
done = dict()
from heapq import *
q = []
if s > LARGE:
	s = LARGE
heappush(q, (0, 0, s))
while q:
	(time, loc, curr) = heappop(q)
	if out[loc] == -1:
		out[loc] = time
		count += 1
		if count == n:
			break
	for (nex, cost, timeD) in adj[loc]:
		currNex = curr + cost
		timeNex = time + timeD
		if currNex > LARGE:
			currNex = LARGE
		if currNex < 0:
			continue
		if (nex, currNex) not in done or timeNex < done[nex, currNex]:
			done[nex, currNex] = timeNex
			heappush(q, (timeNex, nex, currNex))
print(*out[1:], sep='\n')


from heapq import heappop, heappush
import sys
input = sys.stdin.readline
INF = 10 ** 100
MAX = 2500
(N, M, S) = map(int, input().split())
S = min(S, MAX)
G = [[] for _ in [0] * N]
C = []
for _ in [0] * M:
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	G[v].append((u, a, b))
	G[u].append((v, a, b))
for i in range(N):
	(a, b) = map(int, input().split())
	C.append((a, b))
dp = [[INF] * MAX for _ in [0] * N]
q = []

def push(t, s, v):
	if s < 0:
		return
	if s >= MAX:
		s = MAX - 1
	if dp[v][s] <= t:
		return
	dp[v][s] = t
	heappush(q, (t, s, v))
push(0, S, 0)
while q:
	(t, s, v) = heappop(q)
	if dp[v][s] != t:
		continue
	(c, d) = C[v]
	push(t + d, s + c, v)
	for (u, a, b) in G[v]:
		push(t + b, s - a, u)
for d in dp[1:]:
	ans = INF
	for s in range(MAX):
		if ans > d[s]:
			ans = d[s]
	print(ans)


import heapq
(N, M, S) = map(int, input().split())
Map = [[] for _ in range(N)]
Ans = [[float('inf')] * 2500 for _ in range(N)]
for _ in range(M):
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	Map[u].append((v, a, b))
	Map[v].append((u, a, b))
for i in range(N):
	(c, d) = map(int, input().split())
	Map[i].append((i, -c, d))
Q = []
heapq.heappush(Q, (0, 0, min(2499, S)))
Ans[0][min(2499, S)] = 0
while Q:
	(time, where, money) = heapq.heappop(Q)
	for (nex, cost, take) in Map[where]:
		if cost > money:
			continue
		if Ans[nex][min(2499, money - cost)] > time + take:
			Ans[nex][min(2499, money - cost)] = time + take
			heapq.heappush(Q, (time + take, nex, money - cost))
for a in Ans[1:]:
	print(min(a))


from heapq import heappop, heappush

def main():
	(N, M, S) = map(int, input().split())
	G = [[] for _ in range(N)]
	for i in range(M):
		(U, V, A, B) = map(int, input().split())
		G[U - 1].append([V - 1, A, B])
		G[V - 1].append([U - 1, A, B])
	C = []
	D = []
	for _ in range(N):
		(c, d) = map(int, input().split())
		C.append(c)
		D.append(d)
	MaxMoney = 2500
	dp = [[float('inf') for __ in range(MaxMoney + 1)] for _ in range(N)]
	cost = 0
	money = min(S, MaxMoney)
	que = []
	heappush(que, (cost, 0, money))
	dp[0][money] = 0
	while len(que) > 0:
		(cost, node, money) = heappop(que)
		stop_money = min(money + C[node], MaxMoney)
		stop_cost = cost + D[node]
		if stop_cost < dp[node][stop_money]:
			dp[node][stop_money] = stop_cost
			heappush(que, (stop_cost, node, stop_money))
		for (to_node, to_money, to_cost) in G[node]:
			money_ = min(money - to_money, MaxMoney)
			if money_ < 0:
				continue
			cost_ = cost + to_cost
			if dp[to_node][money_] <= cost_:
				continue
			dp[to_node][money_] = cost_
			heappush(que, (cost_, to_node, money_))
	for i in range(1, N):
		print(min(dp[i]))

def __starting_point():
	main()
__starting_point()


import heapq
(N, M, init_silver) = map(int, input().split())
MAX_COST = 2500
init_silver = min(init_silver, MAX_COST)
G = [[] for _ in range(N)]
for i in range(M):
	(u, v, silver_cost, time_cost) = map(int, input().split())
	(u, v) = (u - 1, v - 1)
	G[u].append([v, silver_cost, time_cost])
	G[v].append([u, silver_cost, time_cost])
(change_rate, change_cost) = ([], [])
for i in range(N):
	(rate, cost) = map(int, input().split())
	G[i].append([i, -rate, cost])
	change_rate.append(rate)
	change_cost.append(cost)
dp = [[float('inf')] * (MAX_COST + 1) for _ in range(N)]
dp[0][init_silver] = 0
hq = [(0, 0, init_silver)]
while hq:
	(time, node, silver) = heapq.heappop(hq)
	for (to, silver_cost, time_cost) in G[node]:
		remain_silver = min(silver - silver_cost, MAX_COST)
		if remain_silver < 0:
			continue
		dp_next_value = time + time_cost
		if dp[to][remain_silver] <= dp_next_value:
			continue
		dp[to][remain_silver] = dp_next_value
		heapq.heappush(hq, (dp_next_value, to, remain_silver))
print(*[min(d) for d in dp[1:]], sep='\n')


(n, m, s) = map(int, input().split())
uvab = [list(map(int, input().split())) for i in range(m)]
cd = [list(map(int, input().split())) for i in range(n)]
graph = [[] for i in range((n + 1) * 2500 + 1)]
for (u2, v2, a, b) in uvab:
	for (u, v) in ((u2, v2), (v2, u2)):
		for i in range(u * 2500, u * 2500 + 2500):
			if i % 2500 >= a:
				graph[i].append((i + (v - u) * 2500 - a, b))
for i in range(n):
	(c, d) = cd[i]
	for j in range((i + 1) * 2500, (i + 2) * 2500 - 1):
		graph[j].append((min((i + 2) * 2500 - 1, j + c), d))
import heapq as hq
start = 2500 + min(s, 2499)
dist = [10 ** 18] * ((n + 1) * 2500 + 1)
dist[start] = 0
q = [(0, start)]
hq.heapify(q)
while q:
	(_, x) = hq.heappop(q)
	for (y, d) in graph[x]:
		if dist[y] > dist[x] + d:
			dist[y] = dist[x] + d
			hq.heappush(q, (dist[y], y))
for i in range(2, n + 1):
	print(min(dist[i * 2500:(i + 1) * 2500]))


import heapq
import sys
from collections import defaultdict
readline = sys.stdin.readline
(n, m, s) = map(int, input().split())
ls = [[] for _ in range(n)]
ms = []
for _ in range(m):
	(u, v, a, b) = map(int, input().split())
	ls[u - 1].append((v - 1, a, b))
	ls[v - 1].append((u - 1, a, b))
for _ in range(n):
	(c, d) = map(int, input().split())
	ms.append((c, d))
visited = defaultdict(int)
ans = [-1] * n
count = 0
q = [(0, 0, s)]
while q:
	(t, i, s) = heapq.heappop(q)
	if visited[i, s]:
		continue
	visited[i, s] = 1
	if ans[i] == -1:
		ans[i] = t
		count += 1
		if count == n:
			break
	if s <= 2500:
		(tc, td) = ms[i]
		heapq.heappush(q, (t + td, i, s + tc))
	for (ni, a, b) in ls[i]:
		if not visited[ni, s - a] and s >= a:
			heapq.heappush(q, (t + b, ni, s - a))
for v in ans[1:]:
	print(v)


from heapq import *
(N, M, S) = tuple(map(int, input().split()))
T = [[10 ** 18 for _ in range(2451)] for _ in range(N)]
act = [[] for _ in range(N)]
for i in range(M):
	(U, V, A, B) = tuple(map(int, input().split()))
	U -= 1
	V -= 1
	act[U] += [(V, A, B)]
	act[V] += [(U, A, B)]
for i in range(N):
	(C, D) = tuple(map(int, input().split()))
	act[i] += [(i, -C, D)]
que = [(0, 0, S)]
while que:
	(currentT, n, coins) = heappop(que)
	for (m, cost, t) in act[n]:
		if coins >= cost and currentT + t < T[m][min(2450, coins - cost)]:
			T[m][min(2450, coins - cost)] = currentT + t
			heappush(que, (currentT + t, m, min(2450, coins - cost)))
for i in range(1, N):
	print(min(T[i]))


from heapq import heappop, heappush
INF = 10 ** 18
(N, M, S) = list(map(int, input().split()))
UVAB = [list(map(int, input().split())) for _ in range(M)]
CD = [list(map(int, input().split())) for _ in range(N)]

def solve(link):
	ans = {}
	lim = 50 * (N - 1)
	w = [[INF] * (lim + 1) for _ in range(N)]
	hq = [(0, 0, min(S, lim))]
	while hq:
		(t, v, s) = heappop(hq)
		if t > w[v][s]:
			continue
		if v not in ans:
			ans[v] = t
			if len(ans) == N:
				break
		if s < lim:
			(c, d) = CD[v]
			new_s = min(lim, s + c)
			new_t = t + d
			if new_t < w[v][new_s]:
				w[v][new_s] = new_t
				heappush(hq, (new_t, v, new_s))
		for (u, a, b) in link[v]:
			if s < a:
				continue
			new_s = s - a
			new_t = t + b
			if new_t < w[u][new_s]:
				w[u][new_s] = new_t
				heappush(hq, (new_t, u, new_s))
	return ans

def main():
	link = [[] for _ in range(N)]
	for (u, v, a, b) in UVAB:
		link[u - 1].append((v - 1, a, b))
		link[v - 1].append((u - 1, a, b))
	ans = solve(link)
	for i in range(1, N):
		print(ans[i])

def __starting_point():
	main()
__starting_point()


import heapq
(N, M, S) = map(int, input().split())
MAX_COST = 2500
S = min(S, MAX_COST)
G = [[] for _ in range(N)]
for i in range(M):
	(u, v, s, t) = map(int, input().split())
	(u, v) = (u - 1, v - 1)
	G[u].append([v, s, t])
	G[v].append([u, s, t])
(change_rate, change_cost) = ([], [])
for i in range(N):
	(rate, cost) = map(int, input().split())
	change_rate.append(rate)
	change_cost.append(cost)
dp = [[float('inf')] * (MAX_COST + 1) for _ in range(N)]
dp[0][S] = 0
pq = [(0, 0, S)]
while pq:
	(time, node, silver) = heapq.heappop(pq)
	next_silver = min(silver + change_rate[node], MAX_COST)
	next_time = time + change_cost[node]
	if next_time < dp[node][next_silver]:
		dp[node][next_silver] = next_time
		heapq.heappush(pq, (next_time, node, next_silver))
	for (nei, ns, nt) in G[node]:
		remain_silver = min(silver - ns, MAX_COST)
		if remain_silver < 0:
			continue
		next_time = time + nt
		if dp[nei][remain_silver] > next_time:
			dp[nei][remain_silver] = next_time
			heapq.heappush(pq, (next_time, nei, remain_silver))
for d in dp[1:]:
	print(min(d))


(n, m, s) = map(int, input().split())
s = min(s, 2500)
edge = [[] for i in range(n * 50 * 51)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	for j in range(a, 2501):
		edge[u - 1 + j * n].append((v - 1 + (j - a) * n, b))
		edge[v - 1 + j * n].append((u - 1 + (j - a) * n, b))
for i in range(n):
	(c, d) = map(int, input().split())
	for j in range(2501 - c):
		edge[i + j * n].append((i + (j + c) * n, d))
	for j in range(2500):
		edge[i + (j + 1) * n].append((i + j * n, 0))
s = s * n
from heapq import heappush, heappop, heapify
d = [float('inf') for i in range(n * 50 * 51)]
pq = []
d[s] = 0
heappush(pq, (0, s))
while pq:
	(dist, u) = heappop(pq)
	for (v, cost) in edge[u]:
		if d[v] <= dist + cost:
			continue
		d[v] = dist + cost
		heappush(pq, (d[v], v))
for i in range(1, n):
	print(d[i])


import heapq
(N, M, S) = list(map(int, input().rstrip().split()))
UVAB_list = [list(map(int, input().rstrip().split())) for i in range(M)]
CD_list = [list(map(int, input().rstrip().split())) for i in range(N)]
max_money = 2500
S = min(max_money, S)
graph = [None] + [[] for _ in range(N)]
change = [None] + [() for _ in range(N)]
dp = [None] + [[float('inf')] * (max_money + 1) for _ in range(N)]
dp[1][S] = 0
for (u, v, a, b) in UVAB_list:
	graph[u].append((v, a, b))
	graph[v].append((u, a, b))
for (i, (c, d)) in enumerate(CD_list):
	change[i + 1] = (c, d)
heap = [(0, 1, S)]
while heap:
	(cur_time, cur_city, cur_mone) = heapq.heappop(heap)
	changed_money = cur_mone + change[cur_city][0]
	changed_time = cur_time + change[cur_city][1]
	if changed_money <= max_money and changed_time < dp[cur_city][changed_money]:
		dp[cur_city][changed_money] = changed_time
		heapq.heappush(heap, (changed_time, cur_city, changed_money))
	for (next_city, cost_coin, cost_time) in graph[cur_city]:
		next_time = cur_time + cost_time
		next_money = min(max_money, cur_mone - cost_coin)
		if cur_mone >= cost_coin and next_time < dp[next_city][next_money]:
			dp[next_city][next_money] = next_time
			heapq.heappush(heap, (next_time, next_city, next_money))
for row in dp[2:]:
	print(min(row))


def main():
	from heapq import heappush, heappop
	((n, m, s), *t) = [map(int, r.split()) for r in open(0)]
	e = [[] for _ in range(8 ** 6)]
	for (u, v, a, b) in t[:m]:
		for i in range(2501 - a):
			e[(i + a) * 51 + u] += ((b, i * 51 + v),)
			e[(i + a) * 51 + v] += ((b, i * 51 + u),)
	for (i, (c, d)) in enumerate(t[m:], 1):
		for j in range(2501 - c):
			e[j * 51 + i] += ((d, (j + c) * 51 + i),)
	x = [10 ** 18] * 8 ** 6
	f = [1] * 8 ** 6
	q = [(0, min(2500, s) * 51 + 1)]
	while q:
		(c, v) = heappop(q)
		if f[v] < 1:
			continue
		(x[v], f[v]) = (c, 0)
		for (p, w) in e[v]:
			if f[w]:
				heappush(q, (c + p, w))
	for i in range(2, n + 1):
		print(min(x[i::51]))
main()


import sys
input = sys.stdin.buffer.readline
import heapq

def main():
	(N, M, S) = map(int, input().split())
	edge = [[] for _ in range(N)]
	for _ in range(M):
		(u, v, a, b) = map(int, input().split())
		edge[u - 1].append((v - 1, a, b))
		edge[v - 1].append((u - 1, a, b))
	change = [tuple(map(int, input().split())) for _ in range(N)]
	MAX_COST = 50 * 50
	S = min(S, MAX_COST)
	q = [(0, (0, S))]
	ans = [-1] * N
	visited = set()
	heapq.heapify(q)
	while q:
		(time, data) = heapq.heappop(q)
		(now, money) = data
		if ans[now] == -1:
			ans[now] = time
			if -1 not in ans:
				break
		if data in visited:
			continue
		visited.add(data)
		for (fol, a, b) in edge[now]:
			if money >= a:
				fol_data = (fol, money - a)
				if fol_data in visited:
					continue
				heapq.heappush(q, (time + b, fol_data))
		(c, d) = change[now]
		if money + c > MAX_COST:
			continue
		heapq.heappush(q, (time + d, (now, money + c)))
	print(*ans[1:], sep='\n')
main()


from heapq import heappop, heappush
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
phi = lambda city, silver: city * 2501 + silver
psi = lambda x: (x // 2501, x % 2501)
(n, m, s) = map(int, input().split())
g = [[] for _ in range(n * 2501 + 1)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	for j in range(2501):
		if j - a >= 0:
			g[phi(u - 1, j)].append((phi(v - 1, j - a), b))
			g[phi(v - 1, j)].append((phi(u - 1, j - a), b))
for i in range(n):
	(c, d) = map(int, input().split())
	for j in range(2501):
		if j + c <= 2500:
			g[phi(i, j)].append((phi(i, j + c), d))

def dijkstra(s, n, g):
	d = [10 ** 18] * n
	d[s] = 0
	q = [(0, s)]
	while q:
		(dist_u, u) = heappop(q)
		if d[u] < dist_u:
			continue
		for (v, c) in g[u]:
			if d[v] > dist_u + c:
				d[v] = dist_u + c
				heappush(q, (d[v], v))
	return d
s = min(s, 2500)
d = dijkstra(phi(0, s), n * 2501 + 1, g)
for i in range(1, n):
	print(min(d[phi(i, 0):phi(i + 1, 0)]))


import heapq

def dijkstra_heap():
	used = [-1] * (2451 * n)
	edgelist = []
	heapq.heappush(edgelist, [0, ss])
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if used[minedge[1]] != -1:
			continue
		v = minedge[1]
		kai = minedge[0]
		mai = v % 2451
		used[v] = kai
		for e in edge[v // 2451]:
			if mai - e[1] >= 0:
				sss = mai - e[1]
				if used[e[2] * 2451 + sss] == -1:
					heapq.heappush(edgelist, [e[0] + kai, e[2] * 2451 + sss])
		if mai + cd[v // 2451][0] <= 2450:
			if used[v + cd[v // 2451][0]] == -1:
				heapq.heappush(edgelist, [kai + cd[v // 2451][1], v + cd[v // 2451][0]])
	return used
(n, m, ss) = map(int, input().split())
ss = min(2450, ss)
edge = [[] for i in range(n)]
for i in range(m):
	(x, y, a, b) = map(int, input().split())
	edge[x - 1].append([b, a, y - 1])
	edge[y - 1].append([b, a, x - 1])
cd = []
for i in range(n):
	cd.append(list(map(int, input().split())))
pp = dijkstra_heap()
for i in range(1, n):
	ans = 10 ** 20
	for j in range(2451):
		if pp[i * 2451 + j] != -1:
			ans = min(ans, pp[i * 2451 + j])
	print(ans)


import sys
input = sys.stdin.readline
import heapq as hp
AM = 50 * 50 + 1
INF = 10 ** 15
(N, M, S) = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
	(u, v, a, b) = map(int, input().split())
	graph[u - 1].append((a, b, v - 1))
	graph[v - 1].append((a, b, u - 1))
C = []
D = []
for _ in range(N):
	(c, d) = map(int, input().split())
	C.append(c)
	D.append(d)
dp = [[INF] * (AM + 1) for _ in range(N)]
dp[0][min(AM, S)] = 0
q = [(0, min(AM, S), 0)]
while q:
	(dis, cost, p) = hp.heappop(q)
	if dp[p][cost] < dis:
		continue
	if 0 <= cost + C[p] <= AM and dp[p][cost + C[p]] > dp[p][cost] + D[p]:
		dp[p][cost + C[p]] = dp[p][cost] + D[p]
		hp.heappush(q, (dp[p][cost + C[p]], cost + C[p], p))
	for (a, b, np) in graph[p]:
		if 0 <= cost - a <= AM and dp[np][cost - a] > dp[p][cost] + b:
			dp[np][cost - a] = dp[p][cost] + b
			hp.heappush(q, (dp[np][cost - a], cost - a, np))
for n in range(1, N):
	print(min(dp[n]))


(N, M, S) = map(int, input().split())
ways = [[] for _ in range(N)]
CD = [[0] * 2 for _ in range(N)]

class Way:

	def __init__(self, t, f, ti):
		self.to = t
		self.fare = f
		self.time = ti
for i in range(M):
	(U, V, A, B) = map(int, input().split())
	U -= 1
	V -= 1
	ways[U].append(Way(V, A, B))
	ways[V].append(Way(U, A, B))
for i in range(N):
	(C, D) = map(int, input().split())
	CD[i][0] = C
	CD[i][1] = D
dp = [[float('inf')] * (50 * N + 1) for _ in range(N)]
S = min(S, 50 * N)
dp[0][S] = 0
import heapq
q = []
heapq.heappush(q, (0, 0, S))

def Push(time, station, money):
	global q, dp
	if money < 0:
		return
	money = min(money, 50 * N)
	if dp[station][money] <= time:
		return
	dp[station][money] = time
	heapq.heappush(q, (time, station, money))
while q:
	dat = heapq.heappop(q)
	(time, station, money) = (dat[0], dat[1], dat[2])
	if dp[station][money] != time:
		continue
	Push(time + CD[station][1], station, money + CD[station][0])
	for w in ways[station]:
		Push(time + w.time, w.to, money - w.fare)
for i in range(1, N):
	ans = float('inf')
	for j in range(50 * N + 1):
		ans = min(ans, dp[i][j])
	print(ans)


import heapq
(N, M, S) = list(map(int, input().strip().split()))
graph = [[] for _ in range(N)]
for _ in range(M):
	(u, v, A, B) = list(map(int, input().strip().split()))
	u -= 1
	v -= 1
	graph[u].append((v, A, B))
	graph[v].append((u, A, B))
exchanges = []
for _ in range(N):
	(C, D) = list(map(int, input().strip().split()))
	exchanges.append((C, D))
queue = [(0, 0, S)]
max_fare = 2500
state_table = [[None] * (max_fare + 1) for i in range(N)]
reached = [False] * N
reached_count = 0
while queue:
	(time, cur, money) = heapq.heappop(queue)
	if not reached[cur]:
		reached[cur] = True
		reached_count += 1
		if reached_count >= N:
			break
	(count, d_time) = exchanges[cur]
	for (n, A, B) in graph[cur]:
		n_money = money - A
		n_time = time + B
		if n_money >= 0:
			states = state_table[n]
			if n_money > max_fare:
				n_money = max_fare
			if states[n_money] == None or states[n_money] > n_time:
				states[n_money] = n_time
				heapq.heappush(queue, (n_time, n, n_money))
	states = state_table[cur]
	money += count
	time += d_time
	if money >= 0:
		if money > max_fare:
			money = max_fare
		if states[money] == None or states[money] > time:
			states[money] = time
			heapq.heappush(queue, (time, cur, money))
for i in range(1, N):
	states = state_table[i]
	minimum = None
	for t in states:
		if t != None and (minimum == None or minimum > t):
			minimum = t
	print(minimum)


from heapq import heappop, heappush
import sys

def main():
	input = sys.stdin.readline
	(N, M, S) = map(int, input().split())
	G1 = [[] for _ in range(N)]
	for _ in range(M):
		(u, v, a, b) = map(int, input().split())
		(u, v) = (u - 1, v - 1)
		G1[u].append((b, a, v))
		G1[v].append((b, a, u))
	G2 = [tuple(map(int, input().split())) for _ in range(N)]
	q = [(0, (0, S))]
	T = [-1] * N
	lenT = 0
	visited = set()
	MX = 50 * (N - 1)
	while q:
		(t, V) = heappop(q)
		(v, s) = V
		if T[v] < 0:
			T[v] = t
			lenT += 1
			if lenT >= N:
				break
		if V in visited:
			continue
		visited.add(V)
		for (dt, ds, to) in G1[v]:
			if s < ds:
				continue
			nV = (to, s - ds)
			if nV in visited:
				continue
			heappush(q, (t + dt, nV))
		(c, d) = G2[v]
		if s + c <= MX:
			heappush(q, (t + d, (v, s + c)))
	for i in range(1, N):
		print(T[i])
main()


import sys, math, itertools, collections, bisect, heapq
input = lambda : sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
mod = 10 ** 9 + 7
mans = inf
ans = 0
count = 0
pro = 1
(n, m, s0) = map(int, input().split())
s0 = min(2500, s0)
G = [[] for i in range(n)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	G[u].append((v, -a, b))
	G[v].append((u, -a, b))
for i in range(n):
	(c, d) = map(int, input().split())
	G[i].append((i, c, d))

def dijktra(g, s):
	n = len(g)
	dist = [[inf] * 2501 for i in range(n)]
	dist[s][s0] = 0
	hq = []
	heapq.heappush(hq, (0, s0, s))
	while hq:
		(time, money, u) = heapq.heappop(hq)
		if dist[u][money] < time:
			continue
		for (v, money1, time1) in g[u]:
			M = min(2500, money + money1)
			if not M >= 0:
				continue
			if dist[v][M] > time + time1:
				dist[v][M] = time + time1
				heapq.heappush(hq, (time + time1, M, v))
	return dist
dist = dijktra(G, 0)
for i in range(1, n):
	print(min(dist[i]))


import sys
input = lambda : sys.stdin.readline().rstrip()
(n, m, s) = map(int, input().split())
s = min(s, 2500)
from collections import defaultdict
import heapq

def dijkstra(s, x):
	d = defaultdict(lambda : float('inf'))
	d[s, x] = 0
	used = defaultdict(lambda : False)
	used[s, x] = True
	que = []
	for e in edge[s, x]:
		heapq.heappush(que, e)
	while que:
		minedge = heapq.heappop(que)
		if used[minedge[1]]:
			continue
		v = minedge[1]
		d[v] = minedge[0]
		used[v] = True
		for e in edge[v]:
			if not used[e[1]]:
				heapq.heappush(que, (e[0] + d[v], e[1]))
	return d
max_money = 2500
edge = defaultdict(list)
for _ in range(m):
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	for i in range(a, max_money + 1):
		edge[u, i].append((b, (v, i - a)))
		edge[v, i].append((b, (u, i - a)))
for j in range(n):
	(c, d) = map(int, input().split())
	for i in range(max_money):
		if i + c <= max_money:
			edge[j, i].append((d, (j, i + c)))
ans = [10 ** 18] * n
d = dijkstra(0, s)
for i in d:
	(x, y) = i
	cost = d[i]
	ans[x] = min(ans[x], cost)
for i in ans[1:]:
	print(i)


from heapq import *
(N, M, S) = tuple(map(int, input().split()))
graph = [[] for _ in range(N)]
Max = 0
for i in range(M):
	(U, V, A, B) = tuple(map(int, input().split()))
	Max = max(Max, A)
	U -= 1
	V -= 1
	graph[U] += [(V, A, B)]
	graph[V] += [(U, A, B)]
Max *= N - 1
for i in range(N):
	(C, D) = tuple(map(int, input().split()))
	graph[i] += [(i, -C, D)]
costs = [[2 ** 64 for _ in range(Max + 1)] for _ in range(N)]
que = [(0, 0, S)]
while que:
	(cost_sum, node, coins) = heappop(que)
	for (to, pay, cost) in graph[node]:
		if coins >= pay and cost_sum + cost < costs[to][min(Max, coins - pay)]:
			costs[to][min(Max, coins - pay)] = cost_sum + cost
			heappush(que, (cost_sum + cost, to, min(Max, coins - pay)))
for i in range(1, N):
	print(min(costs[i]))


import heapq
import os
from collections import defaultdict
import sys
if os.getenv('LOCAL'):
	sys.stdin = open('_in.txt', 'r')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
IINF = 10 ** 18
MOD = 10 ** 9 + 7
(N, M, S) = list(map(int, sys.stdin.buffer.readline().split()))
UVAB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
CD = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
CD = [(0, 0)] + CD
graph = [[] for _ in range(N + 1)]
for (u, v, a, b) in UVAB:
	graph[u].append((a, b, v))
	graph[v].append((a, b, u))
for vs in graph:
	vs.sort()
mada = set(list(range(2, N + 1)))
heap = [(0, 1, S)]
ans = [INF] * (N + 1)
ans[1] = 0
dp = defaultdict(lambda : INF)
dp[1, S] = 0
while heap and mada:
	(d, v, s) = heapq.heappop(heap)
	if d > dp[v, s]:
		continue
	if v in mada:
		mada.remove(v)
	ans[v] = min(ans[v], d)
	for (a, b, u) in graph[v]:
		if s - a < 0:
			break
		if d + b < dp[u, s - a]:
			dp[u, s - a] = d + b
			heapq.heappush(heap, (d + b, u, s - a))
	(c, dd) = CD[v]
	sc = min(s + c, 50 * N + 10)
	if d + dd < dp[v, sc]:
		dp[v, sc] = d + dd
		heapq.heappush(heap, (d + dd, v, sc))
print(*ans[2:], sep='\n')


import heapq

def dijkstra(s, n, g):
	INF = 10 ** 18
	d = [INF] * n
	d[s] = 0
	que = []
	heapq.heappush(que, (0, s))
	while que:
		(dist, v) = heapq.heappop(que)
		if d[v] < dist:
			continue
		for (next_v, cost) in g[v]:
			if d[next_v] > d[v] + cost:
				d[next_v] = d[v] + cost
				heapq.heappush(que, (d[next_v], next_v))
	return d

def main():
	(n, m, s) = map(int, input().split())
	g = [[] for _ in range(n * 2501 + 1)]
	for _ in range(m):
		(u, v, a, b) = map(int, input().split())
		u -= 1
		v -= 1
		for j in range(2501):
			if j < a:
				continue
			g[u * 2501 + j].append((v * 2501 + j - a, b))
			g[v * 2501 + j].append((u * 2501 + j - a, b))
	cdl = [(-1, -1)]
	for i in range(n):
		(c, d) = map(int, input().split())
		for j in range(2501):
			maxc = min(2500, j + c)
			g[i * 2501 + j].append((i * 2501 + maxc, d))
	s = min(2500, s)
	dist = dijkstra(s, n * 2501 + 1, g)
	for i in range(1, n):
		ans = min(dist[i * 2501:i * 2501 + 2500])
		print(ans)
main()


import sys
input = sys.stdin.readline

def main():
	(n, m, s) = map(int, input().split())
	uvab = [list(map(int, input().split())) for _ in range(m)]
	cd = [list(map(int, input().split())) for _ in range(n)]
	g = [[] for _ in range(n)]
	for (u, v, a, b) in uvab:
		g[u - 1].append((v - 1, a, b))
		g[v - 1].append((u - 1, a, b))
	import heapq
	inf = pow(10, 21)
	max_s = 50 * 50
	dp = [[inf] * (max_s + 1) for _ in range(n)]
	kakutei = [[0] * (max_s + 1) for _ in range(n)]
	dp[0][min(s, max_s)] = 0
	todo = [[0, 0, min(s, max_s)]]
	heapq.heapify(todo)
	flgs = [0] * n
	flg = 0
	ans = [inf] * n
	while flg < n:
		(_, v, i) = heapq.heappop(todo)
		if kakutei[v][i] == 1:
			continue
		kakutei[v][i] = 1
		if flgs[v] == 0:
			flg += 1
			flgs[v] = 1
		(c, d) = cd[v]
		ans[v] = min(ans[v], _)
		j = min(max_s, i + c)
		if kakutei[v][j] == 0:
			dp[v][j] = min(dp[v][j], dp[v][i] + d)
			heapq.heappush(todo, [dp[v][j], v, j])
		l = g[v]
		for (u, a, b) in l:
			if i - a >= 0 and kakutei[u][i - a] == 0:
				dp[u][i - a] = min(dp[u][i - a], dp[v][i] + b)
				heapq.heappush(todo, [dp[u][i - a], u, i - a])
	print(*ans[1:], sep='\n')

def __starting_point():
	main()
__starting_point()


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10 ** 9 + 7
inf = float('inf')

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))
(n, m, s) = LI()
s = min(s, 2500)
uvab = [LI() for _ in range(m)]
cd = [LI() for _ in range(n)]
dp = [[inf] * (2500 + 1) for _ in range(n + 1)]
dp[0][s] = 0
edges = [[] for _ in range(n)]
exchange = [[c, d] for (c, d) in cd]
for (u, v, a, b) in uvab:
	edges[u - 1].append((v - 1, a, b))
	edges[v - 1].append((u - 1, a, b))
for (i, (c, d)) in enumerate(cd):
	edges[i].append((i, -c, d))
h = [(0, 0, s)]
while h:
	(dist, u, coin) = heappop(h)
	if dp[u][coin] < dist:
		continue
	for (v, a, b) in edges[u]:
		if 2500 >= coin - a >= 0:
			if dp[u][coin] + b < dp[v][coin - a]:
				dp[v][coin - a] = dp[u][coin] + b
				heappush(h, (dp[v][coin - a], v, coin - a))
for i in range(1, n):
	print(min(dp[i]))


import sys

def input():
	return sys.stdin.readline().strip()

def mapint():
	return map(int, input().split())
sys.setrecursionlimit(10 ** 9)
(N, M, S) = mapint()
S = min(S, N * 50)
query = [[[] for _ in range(N * 50 + 1)] for _ in range(N)]
for _ in range(M):
	(u, v, a, b) = mapint()
	for coin in range(a, N * 50 + 1):
		query[u - 1][coin].append((v - 1, a, b))
		query[v - 1][coin].append((u - 1, a, b))
for i in range(N):
	(c, d) = mapint()
	for coin in range(N * 50 + 1):
		query[i][coin].append((i, -c, d))
inf = 10 ** 18
dp = [[inf] * (N * 50 + 1) for _ in range(N)]
dp[0][0] = 0
import heapq
queue = [(0, 0, S)]
checked = [0] * N
cnt = 0
while queue:
	(t, now, coin) = heapq.heappop(queue)
	if cnt == N:
		break
	if not checked[now]:
		checked[now] = 1
		cnt += 1
	qlis = query[now][coin]
	for (nx, a, b) in qlis:
		if 0 <= coin - a:
			coin = min(coin, N * 50 + a)
			if t + b < dp[nx][coin - a]:
				dp[nx][coin - a] = t + b
				heapq.heappush(queue, (t + b, nx, coin - a))
for i in range(1, N):
	print(min(dp[i]))


from heapq import heappush, heappop
inf = float('inf')

def dijkstra(graph: list, node: int, start: int) -> list:
	dist = [inf] * node
	dist[start] = 0
	heap = [(0, start)]
	while heap:
		(cost, thisNode) = heappop(heap)
		for (NextCost, NextNode) in graph[thisNode]:
			dist_cand = dist[thisNode] + NextCost
			if dist_cand < dist[NextNode]:
				dist[NextNode] = dist_cand
				heappush(heap, (dist[NextNode], NextNode))
	return dist
(N, M, S) = map(int, input().split())
graph = [[] for i in range(2501 * N)]
for i in range(M):
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	for s in range(a, 2501):
		graph[2501 * u + s].append([b, 2501 * v + s - a])
		graph[2501 * v + s].append([b, 2501 * u + s - a])
for i in range(N):
	(c, d) = map(int, input().split())
	for s in range(2500):
		graph[2501 * i + s].append([d, 2501 * i + min(2500, s + c)])
dist = dijkstra(graph, 2501 * N, min(2500, S))
ans = [inf] * N
for i in range(N):
	for s in range(2501):
		ans[i] = min(ans[i], dist[2501 * i + s])
for i in range(1, N):
	print(ans[i])


from heapq import heappop, heappush
(N, M, S) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
Y = [list(map(int, input().split())) for _ in range(N)]
lim = 2501
graph = [[] for _ in range((N + 1) * lim)]
for (u, v, a, b) in X:
	for n in range(a, lim):
		graph[u * lim + n].append((v * lim + (n - a), b))
		graph[v * lim + n].append((u * lim + (n - a), b))
for (n, (c, d)) in enumerate(Y):
	for x in range(lim - 1):
		graph[(n + 1) * lim + x].append(((n + 1) * lim + min(lim - 1, x + c), d))
INF = 10 ** 20
start = lim + min(S, lim - 1)
dist = [INF] * ((N + 1) * lim + 1)
dist[start] = 0
pq = []
heappush(pq, (0, start))
while pq:
	(_, u) = heappop(pq)
	for (v, c) in graph[u]:
		if dist[v] > dist[u] + c:
			dist[v] = dist[u] + c
			heappush(pq, (dist[v], v))
ans = []
for i in range(2, N + 1):
	ans.append(min(dist[i * lim:i * lim + lim]))
print(*ans, sep='\n')


from heapq import heappush, heappop

def main():
	(N, M, S) = list(map(int, input().split()))
	G = [[] for _ in range(N)]
	for _ in range(M):
		(u, v, a, b) = list(map(int, input().split()))
		u -= 1
		v -= 1
		G[u].append((v, a, b))
		G[v].append((u, a, b))
	CD = [list(map(int, input().split())) for _ in range(N)]
	distance = [[1 << 62] * 2501 for _ in range(N)]
	q = [(0, 0, min(S, 2500))]
	distance[0][min(S, 2500)] = 0
	while q:
		(d, v, vs) = heappop(q)
		if distance[v][vs] != d:
			continue
		(vc, vd) = CD[v]
		ud = d + vd
		us = vs + vc
		if us > 2500:
			us = 2500
		if distance[v][us] > ud:
			distance[v][us] = ud
			heappush(q, (ud, v, us))
		for (u, fee, b) in G[v]:
			if fee > vs:
				continue
			ud = d + b
			us = vs - fee
			if us > 2500:
				us = 2500
			if distance[u][us] > ud:
				distance[u][us] = ud
				heappush(q, (ud, u, us))
	print('\n'.join((str(min(d)) for d in distance[1:])))
main()


def solve():
	import sys
	input = sys.stdin.readline
	(N, M, S) = map(int, input().split())
	UVAB = [tuple(map(int, input().split())) for i in range(M)]
	CD = [tuple(map(int, input().split())) for i in range(N)]
	es = [[] for _ in range(N)]
	for (u, v, a, b) in UVAB:
		(u, v) = (u - 1, v - 1)
		es[u].append((v, a, b))
		es[v].append((u, a, b))
	INF = float('inf')
	times = [INF] * (2501 * N)
	start = min(2500, S) * N
	times[start] = 0
	hq = [start]
	ans = [-1] * N
	ans[0] = 0
	reached = 0
	import heapq
	heapq.heapify(hq)
	while hq:
		x = heapq.heappop(hq)
		(t, v) = divmod(x, N * 2501)
		(s, i) = divmod(v, N)
		if ans[i] < 0:
			ans[i] = t
			reached += 1
			if reached == N - 1:
				break
		for (to, a, b) in es[i]:
			if a > s:
				continue
			nv = (s - a) * N + to
			if times[nv] <= t + b:
				continue
			times[nv] = t + b
			heapq.heappush(hq, (t + b) * N * 2501 + nv)
		ns = s
		nt = times[v]
		while ns <= 2500:
			ns += CD[i][0]
			nt += CD[i][1]
			nv = min(2500, ns) * N + i
			if times[nv] <= nt:
				break
			times[nv] = nt
			heapq.heappush(hq, nt * N * 2501 + nv)
	print(*ans[1:], sep='\n')
solve()


(n, m, s) = map(int, input().split())
uvab = [list(map(int, input().split())) for i in range(m)]
cd = [list(map(int, input().split())) for i in range(n)]
chg = 10 ** 5
from collections import defaultdict
link = defaultdict(list)
for node in range(n):
	(cmai, dmin) = cd[node]
	for frm in range(2600):
		link[node * chg + frm].append([node * chg + frm + cmai, dmin])
for i in range(m):
	(u, v, a, b) = uvab[i]
	u -= 1
	v -= 1
	for sil in range(2600):
		link[u * chg + sil + a].append([v * chg + sil, b])
		link[v * chg + sil + a].append([u * chg + sil, b])
from heapq import heappush, heappop, heappushpop, heapify
from collections import defaultdict

def dks(g, start):
	INF = float('inf')
	visited = set()
	hq = []
	heappush(hq, (0, start))
	cost = defaultdict(lambda : float('inf'))
	while hq:
		(shortest, i) = heappop(hq)
		if i in visited:
			continue
		visited.add(i)
		cost[i] = shortest
		for (j, t) in g[i]:
			if j in visited:
				continue
			heappush(hq, (shortest + t, j))
	return cost
f_sil = min(s, 2555)
res = dks(link, f_sil)
for node in range(1, n):
	ans = float('inf')
	for sil in range(2600):
		ans = min(ans, res[node * chg + sil])
	print(ans)


import heapq
MAX = 2500
(n, m, s) = map(int, input().split())
s = min(s, MAX)
edge = [[] for i in range(n)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	edge[u - 1].append([v - 1, a, b])
	edge[v - 1].append([u - 1, a, b])
for i in range(n):
	(c, d) = map(int, input().split())
	edge[i].append([i, -c, d])
dp = [[float('inf')] * (MAX + 1) for _ in range(n)]
dp[0][s] = 0
edgelist = [(0, 0, s)]
while edgelist:
	(time, node, silver) = heapq.heappop(edgelist)
	for (to, silver_cost, time_cost) in edge[node]:
		res_silver = min(silver - silver_cost, MAX)
		if res_silver < 0:
			continue
		dp_next_value = time + time_cost
		if dp[to][res_silver] <= dp_next_value:
			continue
		dp[to][res_silver] = dp_next_value
		heapq.heappush(edgelist, (dp_next_value, to, res_silver))
print(*[min(d) for d in dp[1:]], sep='\n')


from heapq import heappop, heappush
(n, m, s) = map(int, input().split())
g = [[] for i in range(n)]
x = 0
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	g[u - 1].append([v - 1, a, b])
	g[v - 1].append([u - 1, a, b])
	x = max(x, a)
for i in range(n):
	(c, d) = map(int, input().split())
	g[i].append([i, -c, d])
x *= n - 1
t = [[2 ** 64 for i in range(x + 1)] for i in range(n)]
q = []
heappush(q, (0, 0, s))
while q:
	(a, b, c) = heappop(q)
	for (i, j, h) in g[b]:
		if c >= j and a + h < t[i][min(x, c - j)]:
			t[i][min(x, c - j)] = a + h
			heappush(q, (a + h, i, c - j))
for i in range(1, n):
	print(min(t[i]))


import heapq
(n, m, s) = map(int, input().split())
lst_graph = [list(map(int, input().split())) for _ in range(m)]
lst_cd = [list(map(int, input().split())) for _ in range(n)]
graph = [[] for _ in range(2501 * n)]

def f(x, c):
	if c >= 2500:
		c = 2500
	return (x - 1) * 2501 + c
for i in range(m):
	(u, v, a, b) = lst_graph[i]
	for c in range(2501):
		if c >= a:
			graph[f(u, c)].append((b, f(v, c - a)))
			graph[f(v, c)].append((b, f(u, c - a)))
for i in range(1, n + 1):
	(x, time) = lst_cd[i - 1]
	for c in range(2500):
		graph[f(i, c)].append((time, f(i, c + x)))

def dijkstra_heap(s, edge):
	d = [float('inf')] * (2501 * n)
	used = [True] * (2501 * n)
	d[s] = 0
	used[s] = False
	edgelist = []
	for e in edge[s]:
		heapq.heappush(edgelist, e)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if not used[minedge[1]]:
			continue
		v = minedge[1]
		d[v] = minedge[0]
		used[v] = False
		for e in edge[v]:
			if used[e[1]]:
				heapq.heappush(edgelist, (e[0] + d[v], e[1]))
	return d
ans = dijkstra_heap(f(1, s), graph)
for i in range(1, n):
	print(min(ans[2501 * i:2501 * i + 2501]))


import sys
readline = sys.stdin.readline
(N, M, S) = map(int, readline().split())
from heapq import heappop as hpp, heappush as hp

def dijkstra(N, s, Edge):
	inf = 10 ** 18
	dist = [inf] * N
	dist[s] = 0
	Q = [(0, s)]
	while Q:
		(dn, vn) = hpp(Q)
		if dn > dist[vn]:
			continue
		for (df, vf) in Edge[vn]:
			if dist[vn] + df < dist[vf]:
				dist[vf] = dist[vn] + df
				hp(Q, (dn + df, vf))
	return dist
geta = 2451
Edge = [[] for _ in range(N * geta)]
S = min(S, geta - 1)
for m in range(M):
	(u, v, a, b) = map(int, readline().split())
	u -= 1
	v -= 1
	for mon in range(a, geta):
		Edge[u * geta + mon].append((b, v * geta + (mon - a)))
		Edge[v * geta + mon].append((b, u * geta + (mon - a)))
for n in range(N):
	(c, d) = map(int, readline().split())
	for mon in range(geta):
		Edge[n * geta + mon].append((d, n * geta + min(geta - 1, mon + c)))
dist = dijkstra(N * geta, S, Edge)
INF = 10 ** 18
ans = [INF] * N
for i in range(N * geta):
	ans[i // geta] = min(ans[i // geta], dist[i])
print('\n'.join(map(str, ans[1:])))


def main():
	import sys
	sys.setrecursionlimit(10 ** 9)
	input = sys.stdin.readline
	from heapq import heappush, heappop
	(N, M, S) = map(int, input().split())
	S = min(S, 2500)
	graph = [[] for _ in range(N)]
	for (u, v, a, b) in (map(int, input().split()) for _ in range(M)):
		graph[u - 1].append((v - 1, a, b))
		graph[v - 1].append((u - 1, a, b))
	for (i, (c, d)) in enumerate((map(int, input().split()) for _ in range(N))):
		graph[i].append((i, -c, d))
	hq = [(0, 0, S)]
	INF = 10 ** 18
	dp = [[INF] * 2501 for _ in range(N)]
	dp[0][S] = 0
	while hq:
		(time, v, silver) = heappop(hq)
		if dp[v][silver] < time:
			continue
		for (dest, a, b) in graph[v]:
			next_s = silver - a if silver - a < 2500 else 2500
			if next_s < 0 or dp[dest][next_s] <= time + b:
				continue
			for i in range(next_s, -1, -1):
				if dp[dest][i] > time + b:
					dp[dest][i] = time + b
				else:
					break
			heappush(hq, (time + b, dest, next_s))
	print(*(min(dp[i]) for i in range(1, N)), sep='\n')
main()


from heapq import *
(n, m, s) = map(int, input().split())
c = [0] * n
d = [0] * n
g = [[] for i in range(n)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	u -= 1
	v -= 1
	g[u].append((v, a, b))
	g[v].append((u, a, b))
for i in range(n):
	(c[i], d[i]) = map(int, input().split())
flag = 0
lasttime = [[10 ** 15] * 2501 for i in range(n)]
lasttime[0][min(2500, s)] = 0
mintimes = [10 ** 15] * n
hp = [(0, min(2500, s), 0)]
while flag != (1 << n) - 1:
	(time, silver, node) = heappop(hp)
	flag = flag | 1 << node
	mintimes[node] = min(mintimes[node], time)
	for (atnode, costmoney, costtime) in g[node]:
		(nexttime, nextsilver) = (time + costtime, silver - costmoney)
		if costmoney <= silver and lasttime[atnode][nextsilver] > nexttime:
			heappush(hp, (nexttime, nextsilver, atnode))
			lasttime[atnode][nextsilver] = min(lasttime[atnode][nextsilver], nexttime)
	(nexttime, nextsilver) = (time + d[node], min(silver + c[node], 2500))
	if silver != 2500 and lasttime[node][nextsilver] > nexttime:
		heappush(hp, (nexttime, min(silver + c[node], 2500), node))
		lasttime[node][nextsilver] = min(lasttime[node][nextsilver], nexttime)
print(*mintimes[1:], sep='\n')


from heapq import heappush, heappop

def resolve():
	INF = float('inf')
	maxMoney = 2502

	def Dijkstra(g):
		ans = {}
		dp = [[INF] * (maxMoney + 1) for _ in range(N)]
		hq = []
		heappush(hq, (0, 0, min(S, maxMoney)))
		while hq:
			(t, v, s) = heappop(hq)
			if t > dp[v][s]:
				continue
			if v not in ans:
				ans[v] = t
				if len(ans) == N:
					break
			if s < maxMoney:
				(c, d) = CD[v]
				new_s = min(s + c, maxMoney)
				new_t = t + d
				if new_t < dp[v][new_s]:
					dp[v][new_s] = new_t
					heappush(hq, (new_t, v, new_s))
			for (nv, cost, time) in G[v]:
				if s < cost:
					continue
				new_s = s - cost
				new_t = t + time
				if new_t < dp[nv][new_s]:
					dp[nv][new_s] = new_t
					heappush(hq, (new_t, nv, new_s))
		return ans
	(N, M, S) = list(map(int, input().split()))
	G = [[] for _ in range(N)]
	for _ in range(M):
		(u, v, coin, tm) = list(map(int, input().split()))
		(u, v) = (u - 1, v - 1)
		G[u].append((v, coin, tm))
		G[v].append((u, coin, tm))
	CD = [list(map(int, input().split())) for _ in range(N)]
	ans = Dijkstra(G)
	for i in range(1, N):
		print(ans[i])

def __starting_point():
	resolve()
__starting_point()


import heapq
(city_num, road_num, init_silver) = map(int, input().split())
MAX_COST = 2500
init_silver = min(MAX_COST, init_silver)
G = [[] for _ in range(city_num)]
for _ in range(road_num):
	(A, B, cost, time_cost) = map(int, input().split())
	(A, B) = (A - 1, B - 1)
	G[B].append([A, cost, time_cost])
	G[A].append([B, cost, time_cost])
for n in range(city_num):
	(cost, time_cost) = map(int, input().split())
	G[n].append([n, -cost, time_cost])
dp = [[float('inf')] * (MAX_COST + 1) for _ in range(city_num)]
dp[0][init_silver] = 0
hq = [(0, 0, init_silver)]
while hq:
	(time, node, silver) = heapq.heappop(hq)
	for (to, silver_cost, time_cost) in G[node]:
		remain = min(silver - silver_cost, MAX_COST)
		if remain < 0:
			continue
		dp_next_value = time + time_cost
		if dp[to][remain] <= dp_next_value:
			continue
		dp[to][remain] = dp_next_value
		heapq.heappush(hq, (dp_next_value, to, remain))
print(*[min(d) for d in dp[1:]], sep='\n')


max_s = 2500
(n, m, s) = map(int, input().split())
s = min(s, max_s)
conv = lambda i, j: i * 10000 + j
G = [[] for i in range(conv(n - 1, max_s) + 1)]
for i in range(m):
	(u, v, a, b) = map(int, input().split())
	(u, v) = (u - 1, v - 1)
	for j in range(max_s + 1):
		if j - a >= 0:
			G[conv(u, j)].append((conv(v, j - a), b))
			G[conv(v, j)].append((conv(u, j - a), b))
for i in range(n):
	(c, d) = map(int, input().split())
	for j in range(max_s + 1):
		if j + c > max_s:
			break
		G[conv(i, j)].append((conv(i, j + c), d))
from heapq import heappush, heappop

def dijkstra(G, start):
	n = len(G)
	dist = [float('inf')] * n
	visited = set()
	heap = [(0, start)]
	while heap and len(visited) != n:
		(d, cur) = heappop(heap)
		if cur in visited:
			continue
		visited.add(cur)
		dist[cur] = d
		for (to, cost) in G[cur]:
			if to not in visited:
				heappush(heap, (d + cost, to))
	return dist
dist = dijkstra(G, conv(0, s))
for i in range(1, n):
	cost = min(dist[conv(i, 0):conv(i, max_s) + 1])
	print(cost)


import heapq
(N, M, S) = map(int, input().split())
uvab = [list(map(int, input().split())) for _ in range(M)]
cd = [list(map(int, input().split())) for _ in range(N)]
for i in range(M):
	uvab[i][0] -= 1
	uvab[i][1] -= 1
railways = [[] for _ in range(N)]
max_a = 0
for (u, v, a, b) in uvab:
	railways[u].append((v, a, b))
	railways[v].append((u, a, b))
	max_a = max(max_a, a)
del uvab
max_silver = max_a * (N - 1)
ans = [-1] * N
d = dict()
d[S, 0] = 0
Q = [(0, S, 0)]
sum_reached = 0
reached = [False] * N
while Q:
	(t, sv, st) = heapq.heappop(Q)
	if not reached[st]:
		ans[st] = t
		reached[st] = True
		sum_reached += 1
		if sum_reached == N:
			break
	new_key = (min(max_silver, sv + cd[st][0]), st)
	new_value = t + cd[st][1]
	if new_key not in d or d[new_key] > new_value:
		d[new_key] = t + cd[st][1]
		heapq.heappush(Q, (new_value, *new_key))
	for (dist, a, b) in railways[st]:
		if sv >= a:
			new_key = (sv - a, dist)
			new_value = t + b
			if new_key not in d or d[new_key] > new_value:
				d[new_key] = new_value
				heapq.heappush(Q, (new_value, *new_key))
print(*ans[1:], sep='\n')