#include <bits/stdc++.h>
using namespace std;
long long dis[500][500];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        scanf("%lld", &dis[i][j]);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          dis[j][k] = min(dis[j][i] + dis[i][k], dis[j][k]);
        }
      }
    }
    int kk;
    scanf("%d", &kk);
    int f, t;
    long long c;
    for (int i = 0; i < kk; i++) {
      scanf("%d%d%lld", &f, &t, &c);
      if (c < dis[f][t]) {
        dis[f][t] = c;
        dis[t][f] = c;
        for (int j = 1; j <= n; j++) {
          for (int k = 1; k <= n; k++) {
            if (dis[j][k] > dis[j][t] + dis[t][f] + dis[f][k])
              dis[k][j] = dis[j][k] = dis[j][t] + dis[t][f] + dis[f][k];
          }
        }
      }
      long long res = 0;
      for (int j = 1; j <= n; j++) {
        for (int k = j + 1; k <= n; k++) {
          res += dis[j][k];
        }
      }
      printf("%lld ", res);
    }
    printf("\n");
  }
}
