#include <bits/stdc++.h>
const double EPS = 1e-9;
const int MOD = 1000003;
const int INF = 2000 * 1000 * 1000;
using namespace std;
int popcount(int n) {
  bitset<32> b(n);
  return b.count();
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) {
  int temp = gcd(a, b);
  return temp ? (a / temp * b) : 0;
}
template <typename T>
T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}
inline bool isInteger(const std::string& s) {
  if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+')))
    return false;
  char* p;
  strtol(s.c_str(), &p, 10);
  return (*p == 0);
}
int adj[305][305];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> adj[i][j];
  int k;
  cin >> k;
  int a, b, c;
  for (int i = 0; i < k; i++) {
    long long sum = 0;
    cin >> a >> b >> c;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        adj[i][j] = min(adj[i][j], min(adj[i][a] + c + adj[b][j],
                                       adj[i][b] + c + adj[a][j]));
        adj[j][i] = adj[i][j];
        sum += adj[i][j];
      }
    cout << sum / 2 << " ";
  }
  cout << endl;
  return 0;
}
