#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300 + 10;
long long n, w[maxn][maxn], sum, k;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) cin >> w[i][j];
  cin >> k;
  for (long long i = 0; i < k; i++) {
    long long v, u, w1;
    sum = 0;
    cin >> v >> u >> w1;
    v--;
    u--;
    w[v][u] = min(w[v][u], w1);
    w[u][v] = w[v][u];
    for (long long j = 0; j < n; j++)
      for (long long t = 0; t < n; t++)
        w[j][t] = min(w[j][t], w[j][v] + w[v][t]);
    for (long long j = 0; j < n; j++)
      for (long long t = 0; t < n; t++)
        w[j][t] = min(w[j][t], w[j][u] + w[u][t]);
    for (long long j = 0; j < n; j++)
      for (long long t = 0; t < n; t++) sum += w[j][t];
    cout << sum / 2 << endl;
  }
  return 0;
}
