#include"iostream" using namespace std; int n,a[101][101],m,x,y,b[101],e[101][101],x1,y1,sun,c[101][101]; int see() { int min1=1000000; int i,j; for(i=2;i<=n;i++) { for(j=1;j<i;j++) { if(e[i][j]==0&&a[i][j]<=min1) { // cout<<i<<" "<<j<<" "<<a[i][j]<<endl; x1=i; y1=j; min1=a[i][j]; } } } e[x1][y1]=1; // cout<<a[x1][y1]<<endl<<endl; return 0; } int main() { while(cin>>n) { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { cin>>a[i][j]; if(a[i][j]==0) a[i][j]=100000; } memset(e,0,sizeof(e)); memset(c,0,sizeof(c)); for(i=1;i<=n;i++) b[i]=i; cin>>m; for(i=1;i<=m;i++) { cin>>x>>y; // a[x][y]=a[y][x]=0; // c[x][y]=c[y][x]=1; /* if(b[x]==x) { b[x]=x; b[y]=x; } else { b[y]=b[x]; } */ } sun=0; i=1; while(i<=n-1) { see(); if(b[x1]!=b[y1]) { // cout<<x1<<" "<<y1<<" "<<a[x1][y1]<<endl; int min2=x1<y1?x1:y1; int max2=x1>y1?x1:y1; int key=b[max2]; for(j=1;j<=n;j++) if(b[j]==key) { b[j]=b[min2]; } //if(c[x1][y1]==0) // cout<<x1<<" "<<y1<<endl; // int ii; // for(ii=1;ii<=n;ii++) // cout<<b[ii]<<" "; // cout<<endl; /* int iii,jjj; for(iii=1;iii<=n;iii++) { for(jjj=1;jjj<=n;jjj++) cout<<e[iii][jjj]<<" "; cout<<endl; } */ sun=sun+a[x1][y1]; i++; } } // for(i=1;i<=n;i++) // cout<<b[i]<<" "; cout<<sun<<endl; } return 0; }