#include <bits/stdc++.h>
const int maxn = 310;
int n, m;
long long sum;
int d[maxn][maxn];
void floyd(int k) {
  int res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (d[i][j] > d[i][k] + d[k][j]) {
        res += d[i][j] - d[i][k] - d[k][j];
        d[i][j] = d[i][k] + d[k][j];
      }
  sum -= res >> 1;
}
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        scanf("%d", &d[i][j]);
      }
    sum = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) sum += d[i][j];
    sum >>= 1;
    scanf("%d", &m);
    while (m--) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      if (d[u][v] > w) {
        sum -= d[u][v] - w;
        d[u][v] = d[v][u] = w;
        floyd(u);
        floyd(v);
        floyd(u);
        floyd(v);
      }
      std::cout << sum << ' ';
    }
    putchar('\n');
  }
  return 0;
}
