#include <bits/stdc++.h>
const int mod = 1e9 + 7, MAX = 1e6 + 5;
const long long inf = 1e18 + 5;
const double pi = 3.14159265358979323846;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, e, i, j;
  cin >> n;
  vector<vector<long long> > dist(n + 1, vector<long long>(n + 1));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      cin >> dist[i][j];
    }
  }
  long long res = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j < i; j++) {
      res += dist[i][j];
    }
  }
  cin >> e;
  while (e--) {
    int x, y, c;
    cin >> x >> y >> c;
    if (dist[x][y] <= c) {
      cout << res << "\n";
      continue;
    }
    res = 0;
    dist[x][y] = c;
    dist[y][x] = c;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        if (i != j) {
          if (i != y && dist[i][j] > dist[i][x] + dist[y][j] + c) {
            dist[i][j] = dist[i][x] + dist[y][j] + c;
          }
          if (i != x && dist[i][j] > dist[i][y] + dist[x][j] + c) {
            dist[i][j] = dist[i][y] + dist[x][j] + c;
          }
        }
      }
    }
    for (i = 1; i <= n; i++) {
      for (j = 1; j < i; j++) {
        res += dist[i][j];
      }
    }
    cout << res << "\n";
  }
  return 0;
}
