#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, a, b, c;
  long long mat[330][330];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%I64d", &mat[i][j]);
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    scanf("%d %d %d", &a, &b, &c);
    if (i > 0) printf(" ");
    long long sum = 0;
    if (mat[a][b] > c) mat[a][b] = mat[b][a] = c;
    for (int j = 1; j <= n; j++)
      for (int x = 1; x <= n; x++) {
        if (mat[j][x] > mat[j][a] + mat[a][b] + mat[b][x])
          mat[j][x] = mat[x][j] = mat[j][a] + mat[a][b] + mat[b][x];
        if (mat[j][x] > mat[j][b] + mat[b][a] + mat[a][x])
          mat[j][x] = mat[x][j] = mat[j][b] + mat[b][a] + mat[a][x];
        if (x < j) sum += mat[j][x];
      }
    printf("%I64d", sum);
  }
  return 0;
}
