You have <var>500</var> points. Problem Statement: There are <var>N</var> cities numbered <var>1</var> to <var>N</var>, connected by <var>M</var> railroads. You are at City <var>1</var> with <var>10^{100}</var> gold coins and <var>S</var> silver coins. The <var>i</var>-th railroad connects City <var>U_i</var> and City <var>V_i</var> bidirectionally, with a one-way trip costing <var>A_i</var> silver coins and taking <var>B_i</var> minutes. You cannot use gold coins for fare. Each city has an exchange counter where you can get <var>C_i</var> silver coins for <var>1</var> gold coin, taking <var>D_i</var> minutes for each gold coin. For each <var>t=2, ..., N</var>, find the minimum time needed to travel from City <var>1</var> to City <var>t</var>. Constraints: <var>2 \leq N \leq 50</var>, <var>N-1 \leq M \leq 100</var>, <var>0 \leq S \leq 10^9</var>, <var>1 \leq A_i \leq 50</var>, <var>1 \leq B_i,C_i,D_i \leq 10^9</var>, <var>1 \leq U_i &lt; V_i \leq N</var>, no duplicate railroads, and all input values are integers. Input: <var>N</var> <var>M</var> <var>S</var>, followed by <var>U_i</var> <var>V_i</var> <var>A_i</var> <var>B_i</var>, and <var>C_i</var> <var>D_i</var>. Output: For each <var>t=2, ..., N</var>, print the minimum time to travel from City <var>1</var> to City <var>t</var>.

# Buggy version:

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)]

a_max = max(a for *_, a, _ in X)
lim = a_max * (N - 1) + 1
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):
        graph[(n + 1) * lim + x].append(
            ((n + 1) * lim + min(lim - 1, x + c), d))


INF = 10 ** 9 + 7
start = lim + min(S, lim)
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")


# Fixed version:

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")

# Buggy version:
import heapq

city_num, road_num, init_silver = map(int, input().split())
init_silver  = min(2500, 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")] * 2501 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, 2500)
        if remain < 0:
            continue
        dp_next_value = time + time_cost
        if dp[to][remain] <= dp_next_value:
            continue
        dp[to][remain] = dp_next_value
        hq.append([dp_next_value, to, remain])
print(*[min(d) for d in dp[1:]], sep="\n")

# Fixed version:
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")

# Buggy version:
from heapq import *

T = [[10**18 for _ in range(2451)] for _ in range(N)]

act = [[] for _ in range(N)]

N, M, S = tuple(map(int, input().split()))

for i in range(M):
  U, V, A, B = tuple(map(int, input().split()))
  maxV = max(maxV, A)
  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]:
    # もし「払える金額で」「最小時間T[移動先][所持金]を更新できるとき」に値を更新、キューに状態を追加。
    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]))

# Fixed version:
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]:
    # もし「払える金額で」「最小時間T[移動先][所持金]を更新できるとき」に値を更新、キューに状態を追加。
    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]))

# Buggy version:
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,2500):
        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[2500*i+s])

for i in range(1,N):
    print(ans[i])

# Fixed version:
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])