#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int a[301][301];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  int q;
  scanf("%d", &q);
  for (int k = 0; k < q; ++k) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    x--;
    y--;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        a[i][j] = min(a[i][j], a[i][x] + z + a[y][j]);
        a[i][j] = min(a[i][j], a[i][y] + z + a[x][j]);
      }
    }
    long long sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        sum += a[i][j];
      }
    }
    printf("%lld ", sum);
  }
  return 0;
}
