#define PROBLEM \
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B"
#include<bits/stdc++.h>#define REP(i, n) for (int i = 0; i < (n); i++)
#include"library/graph/WeightedGraph.hpp"
#include"library/graph/shortest_path/Dial.hpp"intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn;std::cin>>n;WeightedGraph<int>G(n);REP(_,n){intu,k;std::cin>>u>>k;REP(_,k){intv,c;std::cin>>v>>c;G.add_arc(u,v,c);}}G.build();auto[d,pre]=dial(G);REP(i,n)std::cout<<i<<" "<<d[i]<<"\n";}
#line 1 "test/AOJ/ALDS1_12_B.test.cpp"
#define PROBLEM \
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B"
#include<bits/stdc++.h>#define REP(i, n) for (int i = 0; i < (n); i++)
#line 2 "library/graph/WeightedGraph.hpp"
template<typenameT>structWeightedEdge{WeightedEdge()=default;WeightedEdge(intfrom,intto,Tweight):from(from),to(to),weight(weight){}intfrom,to;Tweight;operatorint()const{returnto;}};template<typenameT>structWeightedGraph{intn;usingweight_type=T;usingedge_type=WeightedEdge<T>;std::vector<edge_type>edges;protected:std::vector<int>in_deg;boolprepared;classOutgoingEdges{WeightedGraph*g;intl,r;public:OutgoingEdges(WeightedGraph*g,intl,intr):g(g),l(l),r(r){}edge_type*begin(){return&(g->edges[l]);}edge_type*end(){return&(g->edges[r]);}edge_type&operator[](inti){returng->edges[l+i];}intsize()const{returnr-l;}};classConstOutgoingEdges{constWeightedGraph*g;intl,r;public:ConstOutgoingEdges(constWeightedGraph*g,intl,intr):g(g),l(l),r(r){}constedge_type*begin()const{return&(g->edges[l]);}constedge_type*end()const{return&(g->edges[r]);}constedge_type&operator[](inti)const{returng->edges[l+i];}intsize()const{returnr-l;}};public:OutgoingEdgesoperator[](intv){assert(prepared);return{this,in_deg[v],in_deg[v+1]};}constConstOutgoingEdgesoperator[](intv)const{assert(prepared);return{this,in_deg[v],in_deg[v+1]};}boolis_prepared()const{returnprepared;}WeightedGraph():n(0),in_deg(1,0),prepared(false){}WeightedGraph(intn):n(n),in_deg(n+1,0),prepared(false){}WeightedGraph(intn,intm,booldirected=false,intindexed=1):n(n),in_deg(n+1,0),prepared(false){scan(m,directed,indexed);}voidresize(intn){n=n;}voidadd_arc(intfrom,intto,Tweight){assert(!prepared);assert(0<=fromandfrom<nand0<=toandto<n);edges.emplace_back(from,to,weight);in_deg[from+1]++;}voidadd_edge(intu,intv,Tweight){add_arc(u,v,weight);add_arc(v,u,weight);}voidadd_arc(constedge_type&e){add_arc(e.from,e.to,e.weight);}voidadd_edge(constedge_type&e){add_edge(e.from,e.to,e.weight);}voidscan(intm,booldirected=false,intindexed=1){edges.reserve(directed?m:2*m);while(m--){intu,v;std::cin>>u>>v;u-=indexed;v-=indexed;Tweight;std::cin>>weight;if(directed)add_arc(u,v,weight);elseadd_edge(u,v,weight);}build();}voidbuild(){assert(!prepared);prepared=true;for(intv=0;v<n;v++)in_deg[v+1]+=in_deg[v];std::vector<edge_type>new_edges(in_deg.back());autocounter=in_deg;for(auto&&e:edges)new_edges[counter[e.from]++]=e;edges=new_edges;}voidgraph_debug()const{#ifndef __DEBUG
return;#endif
assert(prepared);for(intfrom=0;from<n;from++){std::cerr<<from<<";";for(inti=in_deg[from];i<in_deg[from+1];i++)std::cerr<<"("<<edges[i].to<<","<<edges[i].weight<<")";std::cerr<<"\n";}}};#line 3 "library/graph/shortest_path/Dial.hpp"
template<typenameWG,typenameT=typenameWG::weight_type>std::pair<std::vector<T>,std::vector<int>>dial(constWG&g,ints=0){assert(g.is_prepared());std::vector<T>d(g.n,-1);std::vector<int>pre(g.n,-1);std::vector<bool>used(g.n,false);TK=0,rem=0;for(constauto&e:g.edges)K=std::max(K,e.weight);std::vector<std::queue<int>>que(K+1);autocmp=[&](Ta,Tb){if(a==rem||b==rem)returnb==rem;if((a<rem)^(b<rem))returna<rem;returna>b;};std::priority_queue<T,std::vector<T>,decltype(cmp)>nxt{cmp};d[s]=0;que[0].push(0);nxt.push(0);while(nxt.size()){rem=nxt.top();nxt.pop();auto&Q=que[rem];while(Q.size()){intv=Q.front();Q.pop();if(used[v])continue;used[v]=true;for(constauto&e:g[v]){if(d[e.to]==-1||d[e.to]>d[v]+e.weight){d[e.to]=d[v]+e.weight;pre[e.to]=v;Tx=rem+e.weight;if(x>=K+1)x-=K+1;if(!que[x].size())nxt.push(x);que[x].push(e.to);}}}}return{d,pre};}#line 9 "test/AOJ/ALDS1_12_B.test.cpp"
intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn;std::cin>>n;WeightedGraph<int>G(n);REP(_,n){intu,k;std::cin>>u>>k;REP(_,k){intv,c;std::cin>>v>>c;G.add_arc(u,v,c);}}G.build();auto[d,pre]=dial(G);REP(i,n)std::cout<<i<<" "<<d[i]<<"\n";}