1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS09
* Problem: 567 - Risk
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=508
*
* @author Dennis Wilfert
* @version 1.0, 12/30/2009
*
* Status : Accepted
* Runtime: 1.476
*/
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

class Main {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
StringTokenizer token;
String line;

int countrypairs, boundarys, neighbour, from, to, i, j, k;
int MAX = 20;
int[][] board;
int set = 0;

line = reader.readLine();
while (line != null) {

board = new int[20][20];
writer.printf("Test Set #%d%n", ++set);
// Werte einlesen
for (i = 0; i < MAX - 1; i++) {
token = new StringTokenizer(line);
boundarys = Integer.parseInt(token.nextToken());

for (j = 0; j < boundarys; j++) {
neighbour = Integer.parseInt(token.nextToken());

board[i][neighbour - 1] = 1;
board[neighbour - 1][i] = 1;
}
line = reader.readLine();
}
// Floyd-Warshall
for (k = 0; k < MAX; k++) {
for (i = 0; i < MAX; i++) {
if (board[i][k] != 0) {
for (j = 0; j < MAX; j++) {
if (board[k][j] != 0) {
if (board[i][j] == 0 || board[i][k] + board[k][j] < board[i][j]) {
board[i][j] = board[i][k] + board[k][j];
}
}
}
}
}
}

countrypairs = Integer.parseInt(line);
// Werte ausgeben
for (i = 0; i < countrypairs; i++) {
token = new StringTokenizer(reader.readLine());
from = Integer.parseInt(token.nextToken());
to = Integer.parseInt(token.nextToken());
writer.printf("%2d to %2d: %d%n", from, to, board[from - 1][to - 1]);
}


writer.println("");
line = reader.readLine();

}
writer.flush();

}
}