#include <bits/stdc++.h>
using namespace std;
const int N = 310;
long long g[N][N];
int n, k;
long long sum;
long long floyd(int a, int b) {
  long long sum = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      g[i][j] = g[j][i] = min(g[i][j], g[i][a] + g[a][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      g[i][j] = g[j][i] = min(g[i][j], g[i][b] + g[b][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++) sum += g[i][j];
  return sum;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> g[i][j];
      if (i < j) sum += g[i][j];
    }
  cin >> k;
  while (k--) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c < g[a][b]) {
      g[a][b] = g[b][a] = c;
      sum = floyd(a, b);
    }
    cout << sum << ' ';
  }
}
