#include <bits/stdc++.h>
using namespace std;
int n;
int cost[300][300];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) scanf("%d", &cost[i][j]);
  int k;
  scanf("%d", &k);
  for (int t = 0; t < (int)(k); t++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    long long ret = 0;
    for (int i = 0; i < (int)(n); i++)
      for (int j = 0; j < (int)(i); j++)
        ret += cost[i][j] = cost[j][i] =
            min(cost[i][j],
                c + min(cost[j][a] + cost[b][i], cost[i][a] + cost[b][j]));
    cout << ret << " ";
  }
  return 0;
}
