#include <bits/stdc++.h>
using namespace std;
class TaskC {
 public:
  void solve(std::istream& in, std::ostream& out);
};
void TaskC::solve(std::istream& in, std::ostream& out) {
  int n;
  in >> n;
  int a[310][310];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; ++j) {
      in >> a[i][j];
    }
  }
  int k;
  in >> k;
  while (k--) {
    int x, y, c;
    in >> x >> y >> c;
    --x, --y;
    a[x][y] = min(a[x][y], c);
    a[y][x] = min(a[y][x], c);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        a[i][j] = min(a[i][j], min(a[i][x] + a[x][y] + a[y][j],
                                   a[i][y] + a[y][x] + a[x][j]));
      }
    }
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < i; ++j) ans += (long long)a[i][j];
    }
    out << ans << " ";
  }
};
int main() {
  TaskC solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
