#include <bits/stdc++.h>
using namespace std;
int M[301][301];
int main() {
  int N;
  while (cin >> N) {
    long long sum = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        scanf("%d", M[i] + j);
        sum += (long long)M[i][j];
      }
    }
    int K;
    cin >> K;
    while (K--) {
      int a, b, w;
      scanf("%d %d %d\n", &a, &b, &w);
      a--;
      b--;
      if (M[a][b] <= w) {
        cout << (sum / 2LL) << endl;
      } else {
        sum -= 2LL * (M[a][b] - w);
        M[a][b] = w;
        M[b][a] = w;
        for (int i = 0; i < N; i++) {
          for (int j = i + 1; j < N; j++) {
            long long nuevo =
                min(M[i][a] + M[a][b] + M[b][j], M[i][b] + M[b][a] + M[a][j]);
            if (nuevo < M[i][j]) {
              sum -= 2LL * (M[i][j] - nuevo);
              M[i][j] = nuevo;
              M[j][i] = nuevo;
            }
          }
        }
        cout << (sum / 2LL);
      }
      if (K) printf(" ");
    }
    cout << endl;
  }
  return 0;
}
