/*
floyd算法
*/
1 #include <iostream>
2 #include <cstdlib>
3 #include <cstdio>
4 #include <cstddef>
5 #include <iterator>
6 #include <algorithm>
7 #include <string>
8 #include <locale>
9 #include <cmath>
10 #include <vector>
11 #include <cstring>
12 #include <map>
13 #include <utility>
14 #include <queue>
15 #include <stack>
16 #include <set>
17 #include <functional>
18 using namespace std;
19 const int INF = 0x3f3f3f3f;
20 const int MaxN = 105;
21 const int modPrime = 3046721;
22
23 int n;
24 int dp[MaxN][MaxN];
25
26
27 void Solve()
28 {
29 for (int k = 0; k < n; ++k)
30 {
31 for (int i = 0; i < n; ++i)
32 {
33 for (int j = 0; j < n; ++j)
34 {
35 dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
36 }
37 }
38 }
39 int Q;
40 scanf("%d", &Q);
41 int src, dsn;
42 for (int i = 0; i < Q; ++i)
43 {
44 scanf("%d %d", &src, &dsn);
45 printf("%d
", dp[src - 1][dsn - 1]);
46 }
47 }
48
49 int main()
50 {
51 #ifdef HOME
52 freopen("in", "r", stdin);
53 //freopen("out", "w", stdout);
54 #endif
55
56 scanf("%d", &n);
57 for (int i = 0; i < n; ++i)
58 {
59 for (int j = 0; j < n; ++j)
60 {
61 scanf("%d", &dp[i][j]);
62 }
63 }
64 Solve();
65
66 #ifdef HOME
67 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
68 _CrtDumpMemoryLeaks();
69 #endif
70 return 0;
71 }