1. Java, Peter Schnitzler

/* Problem : 567
* Author : Peter Schnitzler
* Status : AC
* Runtime : 2.51
*
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;


public class Main {



private static boolean[][] iniMap()
{
boolean map [][] = new boolean [20][20];
for (int c1 = 0; c1 < 20; c1++)
{
for (int c2 = 0; c2 < 20; c2++)
{
map[c1][c2] = false;
}
}

return map;
}



private static void fillMap(boolean map[][], String input, BufferedReader reader) throws IOException
{
StringTokenizer tokenizer = new StringTokenizer(input);
int ways = Integer.parseInt(tokenizer.nextToken()) + 1;
for (int c2 = 1; c2 < ways; c2++)
{
int temp = Integer.parseInt(tokenizer.nextToken()) - 1;

map[0][temp] = true;
map[temp][0] = true;
}


for (int c1 = 1; c1 < 19; c1++)
{
input = reader.readLine();
tokenizer = new StringTokenizer(input);
ways = Integer.parseInt(tokenizer.nextToken()) + 1;

for (int c2 = 1; c2 < ways; c2++)
{
int temp = Integer.parseInt(tokenizer.nextToken()) - 1;

map[c1][temp] = true;
map[temp][c1] = true;
}

}






}



private static int findDistance(boolean map[][], int start, int end)
{
int distance[] = new int [20];
for (int c2 = 0; c2 < 20; c2++)
{
distance[c2] = Integer.MAX_VALUE;
}
distance[start - 1] = 0;

LinkedList<Integer> reached = new LinkedList<Integer>();
reached.add(start - 1);

while (!reached.isEmpty())
{
int temp = reached.poll();

if (distance[temp] + 1 < distance[end - 1])
{
for (int c2 = 0; c2 < 20; c2++)
{
if (map [temp][c2] && distance[c2] > (distance[temp] + 1))
{
distance[c2] = distance[temp] + 1;
reached.add(c2);
}
}
}
}

return distance[end - 1];
}



public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();

for (int masterCount = 1; input != null; masterCount++)
{


boolean map [][] = iniMap();

fillMap(map, input, reader);


//read routes
input = reader.readLine();
int routes = Integer.parseInt(input.trim());

System.out.println("Test Set #" + masterCount);

for (int c1 = 0; c1 < routes; c1++)
{
input = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(input);

int start = Integer.parseInt(tokenizer.nextToken());
int end = Integer.parseInt(tokenizer.nextToken());
int endDistance = findDistance(map, start, end);

//correct withspaces
String startSpace;
String endSpace;
String disSpace;

if (start < 10)
{
startSpace = " ";
}
else
{
startSpace = "";
}

if (end < 10)
{
endSpace = " ";
}
else
{
endSpace = "";
}

if (endDistance < 10)
{
disSpace = " ";
}
else
{
disSpace = "";
}

System.out.println(startSpace + start + " to " + endSpace + end + ":" + disSpace + endDistance);

}

System.out.println();

input = reader.readLine();
}
}
}