#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[301][301], k, a1, b, c, sum = 0;
  cin >> n;
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%I64d", &a[i][j]);
    }
  }
  cin >> k;
  while (k--) {
    cin >> a1 >> b >> c;
    sum = 0;
    if (a[a1][b] > c)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          long long value = a[i][a1] + a[b][j] + c;
          long long value1 = a[i][b] + a[a1][j] + c;
          a[i][j] = min(min(value, value1), a[i][j]);
        }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j < i; ++j) {
        sum += a[i][j];
      }
    }
    printf("%I64d\t", sum);
  }
}
