#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int visited[105],map[105][105],lowcost[105],n,m; int prime(int v) { int i,j,minn,k,sum=0; for(i=1;i<=n;i++) lowcost[i]=map[v][i]; visited[v]=1; for(i=2;i<=n;i++) { minn=100000000; for(j=1;j<=n;j++) if(lowcost[j]<minn&&visited[j]==0) { minn=lowcost[j]; k=j; } sum=sum+minn; visited[k]=1; for(j=1;j<=n;j++) if(lowcost[j]>map[k][j]&&visited[j]==0) lowcost[j]=map[k][j]; } return sum; } int main() { int i,j,a,b; while(scanf("%d",&n)!=EOF) { memset(visited,0,sizeof(visited)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&map[i][j]); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); map[a][b]=map[b][a]=0; } printf("%d ",prime(1)); } return 0; }