1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 int road[101][101],n,Q;
6 int main()
7 {
8 memset(road,0x3f,sizeof(road));
9 scanf("%d",&n);
10 for(int i=1;i<=n;i++)
11 for(int j=1;j<=n;j++)
12 scanf("%d",&road[i][j]);
13 for(int k=1;k<=n;k++)
14 for(int i=1;i<=n;i++)
15 for(int j=1;j<=n;j++)
16 if(road[i][j]>road[i][k]+road[k][j])
17 road[i][j]=road[i][k]+road[k][j];
18 scanf("%d",&Q);
19 while(Q--)
20 {
21 int u,v;
22 scanf("%d%d",&u,&v);
23 printf("%d\n",road[u][v]);
24 }
25
26 return 0;
27 }