import java.util.*; import java.io.*; public class F_matsu{ public static void main(String args[]) throws IOException{ Scanner sc = new Scanner(System.in); while(true){ int n = sc.nextInt(); if(n==0) break; String tmp[][] = new String[n][2]; for(int i=0;i l = new ArrayList(); for(int i=0;i l = new LinkedList(); HashSet s = new HashSet(); l.offer(new Pair(x,0)); s.add(x); while(!l.isEmpty()){ Pair v = l.poll(); for(int i=0;i0){ if(i==y){ return v.second + adj[v.first][i]; } else{ if(!s.contains(i)){ Pair u = new Pair(i,v.second+adj[v.first][i]); l.offer(u); s.add(i); } } } } } return 0; } } class Pair{ int first; int second; Pair(int a,int b){ first = a; second = b; } }