Pony Express (Large)

For each of Q deliveries, find the minimum travel time using horses of limited endurance, allowing instant horse swaps at cities.

Hard8Shortest pathGraphDynamic programmingGreedyNo attempts yetTime limit5sMemory limit512 MB

Problem

It is the year 1860, and the Pony Express is the fastest mail delivery system joining the East and West coasts of the United States. This system serves NN different cities. Each city has one horse. Every horse travels at a certain constant speed and has a maximum total distance it can travel before it becomes too tired to continue.

The Pony Express rider starts off on the starting city's horse. Every time the rider reaches a city, the rider may keep the current horse or switch to that city's horse. Switching is instantaneous. Horses never get a chance to rest, so whenever part of a horse's maximum total distance is used up, it is used up forever. When the rider reaches the destination city, the mail is delivered.

The routes between cities were established through complicated negotiations between company owners, lawmakers, union delegates, and cousin Pete. That means the distances between cities do not necessarily follow common sense. They do not necessarily comply with the triangle inequality, and the distance from city A to city B might be different from the distance from city B to city A.

You are a time traveling entrepreneur, and you have brought a fast computer from the future. A single computer is not enough for you to set up an e-mail service and make the Pony Express obsolete, but you can use it to make optimal routing plans for the Pony Express. Given all data about routes between cities and the horses in each city, and a list of pairs of starting and ending cities, compute the minimum time necessary for each delivery. Treat all deliveries as independent: using cities or horses on one route does not make them unavailable on other routes.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case is described as follows.

  • One line with two integers NN and QQ. NN is the number of cities with horses and QQ is the number of pairs of stops we are interested in. Cities are numbered from 1 to NN.
  • NN lines, the ii-th of which contains two integers EiE_i and SiS_i. EiE_i is the maximum total distance, in kilometers, the horse in the ii-th city can go, and SiS_i is the constant speed, in kilometers per hour, at which that horse travels.
  • NN lines, each containing NN integers. The jj-th integer on the ii-th of these lines, DijD_{ij}, is 1-1 if there is no direct route from the ii-th city to the jj-th city, and the length of that route in kilometers otherwise.
  • QQ lines, each containing two integers UkU_k and VkV_k, the starting and destination city of the kk-th pair of cities to investigate.

Limits:

  • 1T1001 \le T \le 100
  • 2N1002 \le N \le 100
  • 1Q1001 \le Q \le 100
  • 1Ei1091 \le E_i \le 10^9 for all ii
  • 1Si10001 \le S_i \le 1000 for all ii
  • 1Dij109-1 \le D_{ij} \le 10^9 and Dij0D_{ij} \ne 0 for all ii, jj
  • Dii=1D_{ii} = -1 for all ii (there are no direct routes from a city to itself)
  • 1UkN1 \le U_k \le N, 1VkN1 \le V_k \le N and UkVkU_k \ne V_k for all kk
  • For all different ll and mm in one test case, the ordered pairs (Ul,Vl)(U_l, V_l) and (Um,Vm)(U_m, V_m) differ
  • The delivery from UkU_k to VkV_k can always be accomplished with the given horses, for all kk
  • Every queried answer is smaller than 10610^6 hours, and the data is such that rounding it to six decimal places is unambiguous

Output

For each test case, output one line containing Case #x: y1 y2 ... yQ, where xx is the test case number (starting from 1) and yky_k is the minimum time, in hours, to deliver a letter from city UkU_k to city VkV_k.

Round each yky_k to six decimal places and print exactly six digits after the decimal point. Separate values with a single space.

Notes

Here is an explanation of each case in the example.

In Case #1 there are two options: use the horse in city 1 for the entire trip, or change horses in city 2. Both horses have enough endurance, so both options work. The horse in city 2 is faster, so it is better to change, for a total time of 1/3 + 1/4.

In Case #2 there are two intermediate cities where you can change horses. If you change horses in city 2, your new horse is very fast but does not have enough endurance, so you are forced to change again in city 3. If you keep your horse, you still have the option to change horses in city 3. The three options and their total times are:

  1. Change horses in both city 2 and city 3 (1/10 + 1/1000 + 10/8 = 1.351).
  2. Change horses just in city 3 (2/10 + 10/8 = 1.45).
  3. Never change horses (12/10 = 1.2).

In Case #3 there are many alternatives for each delivery. The optimal one for the first delivery (city 2 to city 4) is to go to city 1 in time 10/1000, change horses, and then go to cities 2, 3 and 4, in that order, using the horse from city 1, which takes time (10 + 10 + 10) / 60.

For the second delivery (city 3 to city 1) you have no choice but to first go to city 4, which takes time 10/5. Your fairly fast horse does not have enough endurance left to get anywhere else, so you need to grab the horse in city 4. You could use it to get directly to city 1 in time 15, but riding it to city 2 in time 6 and then using the very fast horse in city 2 to get to city 1 in just 10/1000 extra time is quicker.

In the third delivery (city 3 to city 2) it is optimal to use the first two steps of the previous one, for a total time of 10/5 + 6 = 8.