#include <bits/stdc++.h>
using namespace std;
int matrix[305][305], n;
void update(int a, int b, int c) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int ret = matrix[i][j];
      ret = min(ret, matrix[i][a] + c + matrix[b][j]);
      ret = min(ret, matrix[i][b] + c + matrix[a][j]);
      matrix[i][j] = ret;
    }
}
long long sum() {
  long long ret = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) ret += matrix[i][j];
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> matrix[i][j];
  int k, a, b, c;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    update(a, b, c);
    cout << sum() / 2LL;
    if (i == k - 1)
      cout << "\n";
    else
      cout << " ";
  }
}
