#include <bits/stdc++.h>
using namespace std;
const int maxn = 303;
int n, m;
int d[maxn][maxn];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) scanf("%d", &d[i][j]);
  scanf("%d", &m);
  while (m--) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    a--, b--;
    d[a][b] = d[b][a] = min(d[a][b], w);
    for (int i = 0; i < (int)(n); i++)
      for (int j = 0; j < (int)(n); j++)
        for (int t = 0; t < (int)(2); t++)
          d[i][j] = min(d[i][j], d[i][a] + d[a][b] + d[b][j]), swap(a, b);
    long long sum = 0;
    for (int i = 0; i < (int)(n); i++)
      for (int j = 0; j < (int)(i); j++) sum += d[i][j];
    cout << sum << " ";
  }
  return 0;
}
