#include <bits/stdc++.h>
using namespace std;
int g[301][301];
unsigned long long sum = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> g[i][j];
      if (i < j) sum += g[i][j];
    }
  }
  int K;
  cin >> K;
  for (int i = 0; i < K; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (g[a][b] > c) {
      sum = sum - g[a][b] + c;
      g[a][b] = g[b][a] = c;
      for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
          if (u == v) continue;
          int tod =
              min(g[u][a] + g[a][b] + g[b][v], g[u][b] + g[b][a] + g[a][v]);
          if (g[u][v] > tod) {
            sum = sum - g[u][v] + tod;
            g[u][v] = g[v][u] = tod;
          }
        }
      }
    }
    cout << sum << " ";
  }
  return 0;
}
