#include <bits/stdc++.h>
using namespace std;
long long a[310][310];
int main() {
  long long n, m, sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      sum += a[i][j];
    }
  }
  sum = sum / 2;
  cin >> m;
  int x, y, s;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> s;
    if (a[x][y] < s) {
      cout << sum << " ";
      continue;
    }
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (a[j][x] + s + a[y][k] < a[j][k]) {
          sum = sum - a[j][k] + a[j][x] + s + a[y][k];
          a[k][j] = a[j][k] = a[j][x] + s + a[y][k];
        }
      }
    }
    cout << sum << " " << endl;
  }
  return 0;
}
