#include <bits/stdc++.h>
using namespace std;
struct tp {
  int dinh, v;
  tp() {}
  tp(int _dinh, int _v) { dinh = _dinh, v = _v; }
};
bool operator>(const tp& a, const tp& b) {
  if (a.v > b.v) return true;
  if (a.v == b.v)
    if (a.dinh < b.dinh) return true;
  return false;
}
int n, q;
int adjList[310][310];
long long d[310][310];
int dau[310];
int ret[310][310];
int cnt = 0;
void Dijsktra(int start, int endd) {
  priority_queue<tp, vector<tp>, greater<tp> > open;
  memset(dau, 0, sizeof(dau));
  dau[start] = 1;
  open.push(tp(start, 0));
  while (!open.empty()) {
    tp cur = open.top();
    open.pop();
    if (cur.dinh == endd) return;
    int i;
    for (i = 0; i < n; i++) {
      if (i != cur.dinh) {
        int moi = i;
        if (!dau[moi]) {
          dau[moi] = 1;
          d[start][moi] = d[start][cur.dinh] + adjList[cur.dinh][i];
          d[moi][start] = d[start][moi];
          open.push(tp(moi, d[start][moi]));
        } else if (dau[moi] &&
                   d[start][moi] > d[start][cur.dinh] + adjList[cur.dinh][i]) {
          dau[moi] = 1;
          d[start][moi] = d[start][cur.dinh] + adjList[cur.dinh][i];
          d[moi][start] = d[start][moi];
          open.push(tp(moi, d[start][moi]));
        }
      }
    }
  }
}
void Solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> d[i][j];
  scanf("%d", &q);
  long long sum = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) sum += d[i][j];
  for (int i = 0; i < q; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    sum = 0;
    for (int j = 0; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        long long v1 = d[j][a - 1] + d[b - 1][k] + (long long)c;
        long long v2 = d[j][b - 1] + d[a - 1][k] + (long long)c;
        v1 = min(v1, v2);
        if (v1 < d[j][k]) {
          d[j][k] = d[k][j] = v1;
          sum += d[j][k];
        } else
          sum += d[j][k];
      }
    }
    cout << sum << " ";
  }
  printf("\n");
}
int main() {
  Solve();
  return 0;
}
