import java.io.*;
import java.util.Scanner;

public class Main{
        


        static int parr[][]=null,block[][]=null;
        static int n,m,s,d,q,mindist=0,dist=0;
        public static void main(String args[]) throws java.lang.Exception
        {
                
                  Parserdoubt scan=new Parserdoubt(System.in);
          PrintWriter wt=new PrintWriter(System.out);
                
                //Scanner scan=new Scanner(System.in);
                n=scan.nextInt();
                m=scan.nextInt();
                parr=new int[m][4];
                for(int i=0;i<m;++i)
                {
                        parr[i][0]=scan.nextInt();
                        parr[i][1]=scan.nextInt();
                        parr[i][2]=scan.nextInt();
                        parr[i][3]=1;
                        
                        
                }
                
                s=scan.nextInt();
                d=scan.nextInt();
                q=scan.nextInt();
                block=new int [q][3];
                int b1,b2;
                for(int i=0;i<q;i++)
                {
                        b1=scan.nextInt();
                        b2=scan.nextInt();
                        
                        for(int j=0;j<m;j++)
                        {
                                for(int k=0;k<m;k++)
                                        parr[k][3]=1;
                                
                                
                                
                                if(parr[j][0]==b1 && parr[j][1]==b2)
                                        {
                                                parr[j][3]=0;
                                                mindist=0;
                                                find(s,0);
                                                if(mindist!=0)
                                                System.out.println(mindist);
                                                else
                                                        
System.out.println("Infinity");
                                                parr[j][3]=1;
                                                break;
                                                
                                        }
                        }
                        
                }
                
                
                
                
        }
        
        
        public static void  find(int start,int dist)
        {
                for(int i=0;i<m;++i)
                {
                        if(parr[i][0]==start && parr[i][3]!=0)
                        {
                                parr[i][3]=0;
                                dist+=parr[i][2];
                                
                                if(mindist!=0 && dist>mindist) return;
                                
                                if(parr[i][1]==d)
                                {
                                        if(mindist==0)mindist=dist;else 
if(mindist>dist)mindist=dist;
                                        
                                        return;
                                }
                                
                                find(parr[i][1],dist);
                                
                                parr[i][3]=1;
                                dist-=parr[i][2];
                                
                        }
                }
                
                
        }
        
        


}




class Parserdoubt
{
   final private int BUFFER_SIZE = 1 << 17;

   private DataInputStream din;
   private byte[] buffer;
   private int bufferPointer, bytesRead;

   public Parserdoubt(InputStream in)
   {
      din = new DataInputStream(in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
   }
   public String nextString() throws Exception
   {
   StringBuffer sb=new StringBuffer("");
   byte c = read();
   while (c <= ' ') c = read();
   do
   {
   sb.append((char)c);
   c=read();
   }while(c>' ');
   return sb.toString();
   }
   public char nextChar() throws Exception
   {
   byte c=read();
   while(c<=' ') c= read();
   return (char)c;
   }
   public int nextInt() throws Exception
   {
      int ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = (ret<<3)+(ret<<1) + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   public long nextLong() throws Exception
   {
      long ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   private void fillBuffer() throws Exception
   {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) buffer[0] = -1;
   }

   private byte read() throws Exception
   {
      if (bufferPointer == bytesRead) fillBuffer();
      return buffer[bufferPointer++];
   }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to