#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
int n, k;
int c[maxn][maxn];
int x, y, d;
long long res;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> c[i][j];
    }
  }
  cin >> k;
  for (int t = 1; t <= k; t++) {
    cin >> x >> y >> d;
    res = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        c[i][j] = min(c[i][j], c[i][x] + d + c[j][y]);
        c[i][j] = min(c[i][j], c[i][y] + d + c[j][x]);
        c[j][i] = c[i][j];
        res = res + c[i][j];
      }
    }
    cout << res << " ";
  }
  return 0;
}
