#include <bits/stdc++.h>
using namespace std;
const int maxN = 301;
const int INF = 0x1fffffff;
int M[maxN][maxN];
void relax(int n, int a, int b);
unsigned long long getSum(int n);
void Print(int n);
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n, k, a, b, c, s;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> M[i][j];
    }
  }
  cin >> k;
  for (int l = 1; l <= k; l++) {
    cin >> a >> b >> c;
    if (c < M[a][b]) {
      M[a][b] = c;
      M[b][a] = c;
      relax(n, a, b);
    }
    cout << getSum(n);
    if (l < k) cout << " ";
  }
  cout << endl;
  return 0;
}
void relax(int n, int a, int b) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int dist1 = M[i][a] + M[a][b] + M[b][j];
      int dist2 = M[i][b] + M[a][b] + M[a][j];
      int m = min(dist1, dist2);
      if (m < M[i][j]) {
        M[i][j] = m;
      }
    }
  }
}
unsigned long long getSum(int n) {
  unsigned long long s = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      s += M[i][j];
    }
  }
  return s;
}
void Print(int n) {
  cout << "Aux is  : : : " << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cout << M[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
