#![allow(unused_imports, unused_variables, dead_code, non_snake_case, unused_macros)]
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct ContestPrinter {
    s: String,
}

impl ContestPrinter {
    fn new() -> ContestPrinter {
        ContestPrinter {
        s: String::new(),
        }
    }

    fn print<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}", x).as_str());
    }

    fn println<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}\n", x).as_str());
    }
}

impl std::ops::Drop for ContestPrinter {
    fn drop(&mut self) {
        print!("{}", self.s);
    }
}

/*
    let from: Vec<usize> = Vec::new();
    let to: Vec<usize> = Vec::new();
    let cost: Vec<usize> = Vec::new();
*/
fn bellman_ford(
    from: &Vec<usize>,
    to: &Vec<usize>,
    cost: &Vec<i64>,
    start: usize,
    node_num: usize
) -> Vec<i64> {
    assert!(from.len() == to.len(), "invalid input vec length");
    assert!(cost.len() == to.len(), "invalid input vec length");

    let mut distance = vec![i64::max_value(); node_num];
    distance[start] = 0;
    let edge_num = from.len();
    loop {
        let mut update = false;
        for i in 0..edge_num {
            let f = from[i];
            let t = to[i];
            let c = cost[i];
            if distance[f] == i64::max_value() { continue; }
            let new_cost = distance[f] + c;
            if distance[t] > new_cost {
                distance[t] = new_cost;
                update = true;
            }
        }
        if !update { break; }
    }
    distance
}

fn main() {
    let mut printer = ContestPrinter::new();
    let cin = stdin(); let cin = cin.lock(); let mut sc = Scanner::new(cin);
    
    let mut from: Vec<usize> = Vec::new();
    let mut to: Vec<usize> = Vec::new();
    let mut cost: Vec<i64> = Vec::new();

    let N: usize = sc.read();
    for i in 0..N {
        let (f, k): (usize, usize) = (sc.read(), sc.read());
        for j in 0..k {
            let (t, c): (usize, i64) = (sc.read(), sc.read());
            from.push(f);
            to.push(t);
            cost.push(c);
        }
    }

    let distance = bellman_ford(&from, &to, &cost, 0, N);
    for (idx, d) in distance.iter().enumerate() {
        printer.print(idx);
        printer.print(' ');
        printer.println(d);
    }
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }
}


Measured memory_bytes: 5056