/** * Maria Sales Nš 41748 MIEI * Ricardo Silva Nš 37798 LEI */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { private static final int DAYS = 7; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer s; // Read first line int p = Integer.parseInt(in.readLine()); // Graph construction Graph graph = new Graph(p); for(int i = 0; i < p; i++){ s = new StringTokenizer(in.readLine()); for(int j = 0; j < DAYS; j++){ char c = (s.nextToken()).charAt(0); graph.add(c, i, j); } } // Read next line int q = Integer.parseInt(in.readLine()); graph.constructGraph(); // Process tests for(int i = 0; i < q; i++){ s = new StringTokenizer(in.readLine()); int res = graph.dijkstra(Integer.parseInt(s.nextToken()),Integer.parseInt(s.nextToken())); if(res == Integer.MAX_VALUE) System.out.println("impossible"); else System.out.println(res); } } }