Social Resistance

Given a connected undirected graph, compute the resistance distance between query pairs, treating edges as 1 ohm resistors and solving the resulting electrical network.

Medium7GraphMathMatrixImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Many networks need a measure of how close one node is to another. The classic measure is link distance. Represent the network as a connected undirected graph with an edge between every pair of nodes that have a direct link. The link distance between two nodes is then the number of edges on a shortest path joining them. The Kevin Bacon number of an actor is the link distance from that actor to Kevin Bacon in a graph whose nodes are actors and whose edges join two actors who appeared in the same movie. The Erdos number of a mathematician is the link distance to Paul Erdos in a graph whose nodes are mathematicians and whose edges join two mathematicians who co-authored a published paper. In the graph drawn below, the link distance from ALEX to JORDAN is 2, and the link distance from ALEX to SAM and from ALEX to DYLAN is 3.

In that graph the link distance from JORDAN to ALEX and the link distance from JORDAN to DYLAN are both 2, but in some sense JORDAN and DYLAN are more connected than JORDAN and ALEX. Resistance distance is an attempt to reflect that difference in a distance measurement. The same idea applies to friendships in an online social network. There the nodes are people, and the resistance distance between two of them says how close the friendship is.

The resistance distance between two nodes is the resistance between them when the graph is read as an electrical network with a 1 ohm resistor on every edge. Hold node uu at voltage VuV_u and node vv at voltage VvV_v with VuVvV_u \ne V_v, and let the voltage at every other node float. The resistance is (VuVv)(V_u - V_v) divided by the current flowing from uu to vv. Put another way, the resistance is (VuVv)(V_u - V_v) when the current from uu to vv is one ampere.

Recall:

  1. At every node, the currents on the edges into that node sum to the current the node exchanges with the outside world. That value is 1-1 at uu, 11 at vv, and 00 at every other node.
  2. The voltage drop across an edge from node aa to node bb is the current in that edge times the resistance of that edge, which is 1 here.

Write a program that reads a graph given as nodes and edges together with a list of node pairs, and computes the resistance distance for each pair.

Input

The first line contains one integer PP (1P100001 \le P \le 10\,000), the number of datasets. Process every dataset in the same way and independently of the others.

The first line of each dataset contains the dataset number KK, the number of nodes NN (2N202 \le N \le 20), the number of queries QQ (1Q101 \le Q \le 10), and the number of edges EE (1EN(N1)/21 \le E \le N(N-1)/2). Dataset numbers run from 1 in input order. Lines describing the edges follow, then lines listing the node pairs whose resistance distance you must compute.

Each edge line contains a node number nn (1nN1 \le n \le N), a count cc, and the cc nodes joined to nn by an edge. Edge lines continue until exactly EE edges have been given. The graph has no self loop and no parallel edge, and the graph is connected.

Each of the next QQ lines contains a query number qq (1qQ1 \le q \le Q) and two node numbers n1n_1 and n2n_2 (n1n2n_1 \ne n_2). Find the resistance distance from n1n_1 to n2n_2.

Output

Print one line for each dataset. The line contains the dataset number KK, then a single space, then the QQ resistance distances in input order, each rounded to 3 decimal places and separated by single spaces. Always print exactly three digits after the decimal point. No answer sits exactly on a rounding boundary.