#K50022. Drone Delivery Route Calculation

    ID: 28772 Type: Default 1000ms 256MiB

Drone Delivery Route Calculation

Drone Delivery Route Calculation

You are given a distance matrix representing the travel times between various delivery centers, including a base center (indexed as 0). In addition, each drone is assigned a delivery route specified by a sequence of center indices. Each drone starts from the base, visits the centers in the specified order, and then returns to the base.

Your task is to compute the total travel time for each drone. Mathematically, if the delivery route for a drone is given by centers \(a_1, a_2, \ldots, a_k\) and the base is center 0, then the total travel time \(T\) is:

[ T = d(0, a_1) + d(a_1, a_2) + \cdots + d(a_{k-1}, a_k) + d(a_k, 0), ]

Here, \(d(i, j)\) denotes the travel time from center \(i\) to center \(j\) as given in the distance matrix.

The input is given via standard input (stdin) and the output should appear on standard output (stdout), one result per line. Make sure your solution reads from stdin and writes to stdout.

inputFormat

The first line of input contains an integer \(n\) denoting the number of centers. The next \(n\) lines each contain \(n\) integers, representing the distance matrix between the centers. The following line contains an integer \(d\) representing the number of drones.

Each of the next \(d\) lines represents a drone's delivery schedule. Each line starts with an integer \(c\) (the number of centers in that drone's delivery route) followed by \(c\) integers indicating the sequence of centers (indices) the drone will visit. Note that the drone always starts from the base (center 0) and returns to the base after completing its deliveries.

outputFormat

For each drone, output a single line containing an integer — the total travel time calculated based on the route provided.

## sample
5
0 10 20 30 40
10 0 25 35 45
20 25 0 15 50
30 35 15 0 55
40 45 50 55 0
3
3 2 3 1
2 4 2
4 1 2 3 4
80

110 145

</p>