Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := makeGraph(n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			c := getNextInt(scanner)
			g[u][v] = c
		}
	}

	ans := make([]int, n)
	im := make(IntMap, 1)
	im.increment(0)
	for len(im) < n {
		min := 1 << 30
		x := -1
		for u := range im {
			for v := 0; v < n; v++ {
				if im.get(v) != 0 {
					continue
				}
				if min > ans[u]+g[u][v] {
					min = ans[u] + g[u][v]
					x = v
				}
			}
		}
		im.increment(x)
		ans[x] = min
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i, ans[i]))
	}
	writer.Flush()
}

func makeGraph(n int) [][]int {
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = 1 << 30
		}
	}

	return g
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: /**
 * @brief  ??????????????????(shortest paths problem)????????????Dijkstra?????¢?????´?????????(Dijkstra's algorithm)?????±???
 * @note   ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B
 * @date   2016/02/20 ~ 2016/03/13
 */



//****************************************
// ???????????????????????????????????????????????????
//****************************************

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <cassert>
#include <functional>



//****************************************
// ???????????§????????¢?????????????????????
//****************************************

#define GRAPH_BEGIN namespace graph {
#define GRAPH_END   }



//****************************************
// ????????????????§????
//****************************************

GRAPH_BEGIN



//****************************************
// ?§????????????????
//****************************************

using weight_t   = std::int32_t;  /**< ???(u, v)????????????w?????¨?????? */
using index_t    = std::int32_t;  /**< ??????v??????????????¨??????       */
using capacity_t = weight_t;      /**< ???(u, v)??????????????¨??????    */



namespace limits {
    enum {  // scoped??§????????????????????????????????????enum(strongly-typed enum)??§?????????
        inf = std::numeric_limits<weight_t>::max() / 3,  /**< @brief ???????????¨???????????´???????????¨????????????     */
        nil = std::numeric_limits<index_t>::min() / 3,   /**< @brief ?????????????????¨???????????´???????????¨???????????? */
    };
}


/**
 * @brief  ????????????????§???????(scoped enum)
 * @detail (u, v) ??? E??§??????u??????????????°??????v?????°????????§??????
 *         ?????????????????????????????£??\????????¨?????????????????????????????§??????
 *         ??°???????????????????????£??\???????????¨???????????????????????????????????¢??????????????¨???????????????????¢?????????????
 */
enum struct vcolor : std::int32_t {
    white,  /**< ??????????????? */
    black,  /**< ??¢???????????? */
    gray,   /**< ??¢???????????? */
};


/**
 * @brief ??°????????¨?????????(??????) 
 */
struct vertex {
    union {
        weight_t d;       /**< ?§????s??????????????¢  */
        weight_t key;     /**< Prim?????¢?????´???????????????????????¨????±?????????????????????¨????????¶?????? */
    };
    index_t pi;           /**< ????????????(?????????) */
    union {
        vcolor color;     /**< ????????????        */
        bool  visited;    /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() : d(0), pi(0), color(vcolor::white)/*, f(0)*/ {}
};

/**
 * @brief ??°????????¨??¨??????(???)
 * @note  G = (V, E)???????????¢??°w???????????????????????°????????¨????????¨???
 *        ???(u, v) ??? E????????????w(u, v)??¨??¨?????????
 */
struct edge {
    index_t  src;   /**< ???????§????u */
    index_t  dst;   /**< ????????????v */
    union {
        weight_t w;     /**< ???(u, v)????????????(?????????) */
        capacity_t c;   /**< ???(u, v)????????? */
    };
    edge() = default;
    edge(index_t src, index_t dst) : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) : src(src), dst(dst), w(w) {}
};



using edges_t    = std::vector<edge>;      /**< ??°??????G=(V, E)????????????E   */
using vertices_t = std::vector<vertex>;    /**< ??°??????G=(V, E)???????????????V */
using array_t    = std::vector<weight_t>;  /**< ??????w?????????  */
using indices_t  = std::vector<index_t>;   /**< ????????????????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



//****************************************
// ??¢??°?????????
//****************************************

/**
 * @brief  Prim?????¢?????´???????????????????????????????°???¨?????¨A????±?????????????????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¢??????
 *         Dijkstra?????¢?????´????????????????????????????§????s?????????????°???????????????¨?????????????????????u ??? V - S???????????????(??????S????±??????????"????????????"??????????±???????)
 *
 * @note   ????????¢??°???????????????????????????Heap???????????????Prim?????????Dijkstra?????¢?????´???????????§?????????
 *         ????????§??????Heap?????¨???????????§min????????????????????\????????????????????????
 *         ?????????????????¢?????´?????????????¨???°??????????????????????????????????????????????????????????????°?????????????????¨????????????
 *         ?????£??????v.visited???false?????¨??????v.color?????????????????°?????§?????????true?????¨??????????????§??????
 *
 * @param  const vertices_t& V ??¢?´¢????±????????????????V
 * @param  index_t n           ????????????V???????????°n
 * @return index_t u           ?????¶??????????????????u(?????¶??????????????????????????¨???????????´??????nil?????????)
 */
static index_t extract_min(const vertices_t& V, index_t n)
{
    index_t  u = limits::nil;
    weight_t d = limits::inf; 
    for (index_t v = 0; v < n; ++v) {
        if (!V[v].visited && V[v].d < d) { d = V[v].d; u = v; }
    }
    return u;
}


/**
 * @brief  ??(V)???????¶????????????£??????????????¨????????¨???????????????????¨??????????????????°??????????????????
 * @note   ??????????????????????????????v ??? V????????????v.?? = NIL???v.visited = false
 *         ????????????v ??? V - {s}????????????v.d = ?????§?????????
 */
static inline void initialize_single_source_with_visitor(vertices_t& V, index_t s)
{
    for (auto&& v : V) {
        v.d       = limits::inf;
        v.pi      = limits::nil;
        v.visited = false;
    }
    V[s].d = 0;
}


/**
 * @brief  ???(u, v)?????????(relaxing)??????u????????±???????????¨??§v????????¢??\???????????????
 *         ????????§??????????????????????????????????????§??????????????°v.d??¨v.???????´??°??????
 *         ??????????????£??????????????¨??????v.d???????°???????v?????????????±???§v.???????´??°??????????????¨?????????
 *
 * @note   ??\????????????????????????(u, v)?????????????????(1)????????§????????????
 *         ????????????????¶????pred(V, u)?????(1)??§????????????????????¨???????????????
 *
 * @param  vertices_t& V   ????????????V
 * @param  index_t u       ???(u, v)????§????u (????????????u ??? V)
 * @param  index_t v       ???(u, v)?????????v (????????????v ??? V)
 * @param  weight_t w      ???(u, v)?????????w
 * @param  Predicate pred  relax????????????????????¶????¨???°????????°???
 */
static void relax(vertices_t& V,
           index_t u, index_t v, weight_t w,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    if (pred(V, u) && V[v].d > V[u].d + w) {
        V[v].d = V[u].d + w;
        V[v].pi = u;
    }
}


// ??????????????????????????????relax??¢??°???

static inline void relax(vertices_t& V, const edge& e,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    relax(V, e.src, e.dst, e.w, pred);
}
static inline void relax(vertices_t& V, const matrix_t& W,
           index_t u, index_t v,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    relax(V, u, v, W[u][v], pred);
}


/**
 * @brief  ????????????????????????????????§????????¨????????????????????§???Dijkstra(??????????????????)?????¢?????´?????????(Dijkstra's algorithm)???
 *         ????????????????????°??????G = (V, E)?????????????§???????????????????????§£???. ????????§?????????(u, v) ??? E????????????w(u, v) >= 0???????????????
 *
 * @note   ??????v ??? V??????????????????????????(V)?????????????????????????????§?????¨?????§??(V^2)???????????????
 *
 * @param  const matrix_t& W    ?????????????????????????????°??????W
 * @param  index_t         s    ?§????s
 * @return ?§????s????????????????????????????????????????±????????????????????????????S
 */
vertices_t dijkstra(const matrix_t& W, index_t s)
{
    index_t n = W.size();
    vertices_t S(n);
    auto relax_pred = [](const vertices_t& V, index_t u) -> bool { return !V[u].visited; };    


    initialize_single_source_with_visitor(S, s);  // ?????????????????????d?????¨???????????????????????
    while(true) {     
        // ?§????s?????????????°???????????????¨?????????????????????u ??? V - S???????????????
        index_t u = extract_min(S, n);   
        if (u == limits::nil) { break; }    // ??????u???NIL?????????????????°?????¢?´¢???????????§??????
        for (index_t v = 0; v < n; v++) {
            relax(S, W, u, v, relax_pred);  // u????????±???????????¨??§v??????????????????????????§????????´??????????????¨??????v.d??¨?????????v.pi?????´??°??????
        }
        S[u].visited = true;  // ??????????????????S????±????
    }
    // ??????????????§??????S = V????????§?????????????????????u ??? V????????????u.d = ?´(s, u)??§??????
    // ?????????????????¨?????????????????¨?????°??????G?????s????????¨?????????????????¨??§??????
    return S;
}



//****************************************
// ?????????????????????
//****************************************

GRAPH_END



//****************************************
// ??¨?????????????????????
//****************************************

int main(void)
{
    using namespace std;
    using namespace graph;
    
    matrix_t M;
    int n, k, u, v, c;
    
    cin >> n; M.resize(n);
    for (int j = 0; j < n; j++) {
        M[j].resize(n);
        for (int i = 0; i < n; i++) {
            M[j][i] = limits::inf;
        }
    }
    for (int j = 0; j < n; j++) {
        cin >> u >> k;
        for (int i = 0; i < k; i++) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }
    vertices_t S = dijkstra(M, 0);
    for (int i = 0; i < n; i++) {
        cout << i << " " << S[i].d << endl; 
    }

    return 0;
}