#include <bits/stdc++.h>
using namespace std;
int n, m;
int dist[301][301];
long long ans[501];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> dist[i][j];
  cin >> m;
  for (int c = 1; c <= m; c++) {
    int a, b, len;
    long long sum = 0;
    cin >> a >> b >> len;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][a] + dist[b][j] + len);
        dist[i][j] = min(dist[i][j], dist[i][b] + dist[a][j] + len);
        sum += dist[i][j];
      }
    ans[c] = sum / 2;
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i];
    if (i == m)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}
