Sweet Country consists of several Candy States. You are given the list of the roads existing between these states and now you want to improve logistics to deliver candies faster!

Your task is for any pair of statest to find the route with minimal number of roads and tell this number. For example if Carnival is connected to Maycorn and this is connected to Bawnty, then route from Carnival to Bawnty consists of 2 roads.

You may want to build a transitive closure over the states map. For example with Floyd-Warshall algorithm.

Input data will contain:

  • the number of direct roads between states in the first line;
  • the list of these roads, described by pairs of states (in separate line each);
  • the number of routes to find (in separate line);
  • the list of routes, described by pairs of states.

Answer should have lengths of routes, separated by spaces, or value 10000000 if no route exists between the given states.

Example:

input data:
11
Cowsome - Honepy
Bawnty - Cowsome
Lacry - Bawnty
Honepy - Maycorn
Maycorn - Bawnty
Mikliday - Cowsome
Mausse - Lednec
Maycorn - Mausse
Bawnty - Mausse
Mikliday - Maycorn Lednec - Maycorn
6
Cowsome - Mausse
Lacry - Honepy
Bawnty - Honepy
Mikliday - Mausse
Honepy - Lednec
Mikliday - Lacry

answer:
2 3 2 2 2 3
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.