#include <bits/stdc++.h>
using namespace std;
int n, dis[1000][1000], k;
long long sum = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> dis[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++) sum += dis[i][j];
  int A, B, C;
  cin >> k;
  for (int times = 0; times < k; times++) {
    cin >> A >> B >> C;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dis[i][j] > dis[i][A] + C + dis[B][j]) {
          int temp = dis[i][A] + C + dis[B][j];
          sum -= dis[i][j] - temp;
          dis[i][j] = dis[j][i] = temp;
        }
    printf("%I64d ", sum);
  }
  return 0;
}
