macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        m: usize,
        s: i64,
        es: [(usize1, usize1, i64, usize); m],
        v: [(i64, usize); n]
    }
    let mut adj = vec![vec![(usize::max_value() >> 1, 0); n]; n];
    for &(u, v, a, b) in &es {
        adj[u][v] = (b, -a);
        adj[v][u] = (b, -a);
    }
    for i in 0..n {
        adj[i][i] = (v[i].1, v[i].0);
    }
    let dist = dijkstra_all(0, s, &adj);
    for i in 1..n {
        println!("{}", dist[i]);
    }
}

use std::collections::BinaryHeap;
use std::cmp::Ordering;

#[derive(Debug, PartialEq, Eq)]
struct State(usize, usize, i64);

impl Ord for State {
    fn cmp(&self, other: &Self) -> Ordering {
        other.1.cmp(&self.1)
    }
}

impl PartialOrd for State {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn dijkstra_all(s: usize, c: i64, adj: &Vec<Vec<(usize, i64)>>) -> Vec<usize> {
    let n = adj.len();
    let mut pq = BinaryHeap::new();
    let mut dist = vec![vec![usize::max_value(); 50 * n + 1]; n];
    if c > 50 * n as i64 {
        pq.push(State(s, 0, 50 * n as i64));
        dist[s][50 * n] = 0;
    } else {
        pq.push(State(s, 0, c));
        dist[s][c as usize] = 0;
    }
    while let Some(st) = pq.pop() {
        for i in 0..n {
            let mut ct = st.2 + adj[st.0][i].1;
            if adj[st.0][i].0 == usize::max_value() >> 1 || ct < 0 {
                continue;
            }
            if ct > 50 * n as i64 {
                ct = 50 * n as i64;
            }
            let d = st.1 + adj[st.0][i].0;
            if d < dist[i][ct as usize] {
                dist[i][ct as usize] = d;
                pq.push(State(i, d, ct));
            }
        }
    }
    dist.iter().map(|l| l.iter().cloned().min().unwrap()).collect()
}


Measured memory_bytes: 4752