#include <bits/stdc++.h>
using namespace std;
long long dis[1010][1010];
int n, m;
inline int read() {
  int s = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
  return s * w;
}
int main() {
  n = read();
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= n; ++j) dis[i][j] = read();
  m = read();
  while (m--) {
    int x = read(), y = read(), z = read();
    if (z < dis[x][y]) {
      dis[x][y] = dis[y][x] = z;
      for (register int i = 1; i <= n; ++i)
        for (register int j = 1; j <= n; ++j)
          if (i != j) {
            dis[i][j] = min(dis[i][j],
                            min(dis[i][x] + dis[x][j], dis[i][y] + dis[y][j]));
            dis[j][i] = dis[i][j];
          }
    }
    long long ans = 0;
    for (register int i = 1; i <= n; ++i)
      for (register int j = 1; j < i; ++j) ans += dis[i][j];
    printf("%lld ", ans);
  }
  return 0;
}
