#include <bits/stdc++.h>
using namespace std;
int n;
long long mat[310][310] = {0};
void floyd(int t) {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      mat[i][j] = min(mat[i][j], mat[i][t] + mat[t][j]);
      mat[j][i] = mat[i][j];
    }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> mat[i][j];
  int k = 0;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    long long d;
    cin >> x >> y >> d;
    mat[x][y] = min(d, mat[x][y]);
    mat[y][x] = mat[x][y];
    floyd(x);
    floyd(y);
    long long res = 0;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) res += mat[i][j];
    if (i == 0)
      cout << res;
    else
      cout << " " << res;
  }
  cout << endl;
  return 0;
}
