#include <bits/stdc++.h>
using namespace std;
const int N = 302;
int n;
long long dist[N][N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%lld", &dist[i][j]);
    }
  }
  int m;
  scanf("%d", &m);
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    dist[a][b] = min(1LL * c, dist[a][b]);
    dist[b][a] = min(1LL * c, dist[b][a]);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][a] + dist[a][b] + dist[b][j]);
        dist[i][j] = min(dist[i][j], dist[i][b] + dist[a][b] + dist[a][j]);
      }
    }
    long long sm = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        sm += dist[i][j];
      }
    }
    printf("%lld ", sm);
  }
}
