#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ipair pair<ll,ll>
#define MAX 1e10
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vector<ll> vec;
set<ll> s;
int main()
{
	ll t;
	cin>>t;
	ll adj[t+1][t+1];
	ll n=t;
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>adj[i][j];
    }
    ll dup[n+1][n+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dup[i][j]=adj[i][j];
            }
        }
    int k;
    cin>>k;
    while(k--)
    {
        ll a,b,d;
        cin>>a>>b>>d;
        if(dup[a][b]>d)
        {
            dup[b][a]=d;
            dup[a][b]=d;
        }
       // for(int k=1;k<=n;k++)
        //{
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dup[i][b]+dup[b][j]<dup[i][j])
                        dup[i][j]=dup[i][b]+dup[b][j];
                }
            }
        //}
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            //cout<<"\n";
                for(int j=i+1;j<=n;j++)
                {
              //  cout<<dup[i][j]<<" ";
                    ans+=dup[i][j];
                }
        }
        cout<<ans<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ipair pair<ll,ll>
#define MAX 1e10
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vector<ll> vec;
set<ll> s;
int main()
{
	ll t;
	cin>>t;
	ll adj[t+1][t+1];
	ll n=t;
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>adj[i][j];
    }
    int k;
    cin>>k;
    while(k--)
    {
        ll a,b,d;
        cin>>a>>b>>d;
        if(adj[a][b]>d)
        {
            adj[b][a]=d;
            adj[a][b]=d;
        }
        ll dup[n+1][n+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dup[i][j]=adj[i][j];
            }
        }
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dup[i][k]+dup[k][j]<dup[i][j])
                        dup[i][j]=dup[i][k]+dup[k][j];
                }
            }
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            //cout<<"\n";
            for(int j=1;j<=n;j++)
            {
              //  cout<<dup[i][j]<<" ";
                if(dup[i][j]>0 && dup[i][j]!=MAX)
                {
                    ans+=dup[i][j];
                    dup[j][i]=MAX;
                }
            }
        }
        cout<<ans<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ipair pair<ll,ll>
#define MAX 1e10
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vector<ll> vec;
set<ll> s;
int main()
{
	ll t;
	cin>>t;
	ll adj[t+1][t+1];
	ll n=t;
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>adj[i][j];
    }
    ll dup[n+1][n+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dup[i][j]=adj[i][j];
            }
        }
    int k;
    cin>>k;
    while(k--)
    {
        ll a,b,d;
        cin>>a>>b>>d;
        if(dup[a][b]>d)
        {
            dup[b][a]=d;
            dup[a][b]=d;
        }
       // for(int k=1;k<=n;k++)
        //{
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dup[i][a]+dup[a][j]<dup[i][j])
                        dup[i][j]=dup[i][a]+dup[a][j];
                }
            }
        //}
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            //cout<<"\n";
                for(int j=i+1;j<=n;j++)
                {
              //  cout<<dup[i][j]<<" ";
                    ans+=dup[i][j];
                }
        }
        cout<<ans<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ipair pair<ll,ll>
#define MAX 1e10
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vector<ll> vec;
set<ll> s;
int main()
{
	ll t;
	cin>>t;
	ll adj[t+1][t+1];
	ll n=t;
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>adj[i][j];
    }
    int k;
    cin>>k;
    while(k--)
    {
        ll a,b,d;
        cin>>a>>b>>d;
        if(adj[a][b]>d)
        {
            adj[b][a]=d;
            adj[a][b]=d;
        }
        ll dup[n+1][n+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dup[i][j]=adj[i][j];
            }
        }
        for(int k=1;k<=n-1;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dup[i][k]+dup[k][j]<dup[i][j])
                        dup[i][j]=dup[i][k]+dup[k][j];
                }
            }
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(dup[i][j]>0 && dup[i][j]!=MAX)
                {
                    ans+=dup[i][j];
                    dup[j][i]=MAX;
                }
            }
        }
        cout<<ans<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ipair pair<ll,ll>
#define MAX 1e10
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vector<ll> vec;
set<ll> s;
int main()
{
	ll t;
	cin>>t;
	ll adj[t+1][t+1];
	ll n=t;
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>adj[i][j];
    }
    ll dup[n+1][n+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dup[i][j]=adj[i][j];
            }
        }
    int k;
    cin>>k;
    while(k--)
    {
        ll a,b,d;
        cin>>a>>b>>d;
        if(dup[a][b]>d)
        {
            dup[b][a]=d;
            dup[a][b]=d;
        }
        int k=0;
        int pos[2]={a,b};
        while(k<2)
        {
            int inter=pos[k];
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dup[i][inter]+dup[inter][j]<dup[i][j])
                        dup[i][j]=dup[i][inter]+dup[inter][j];
                }
            }
            k++;
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            //cout<<"\n";
                for(int j=i+1;j<=n;j++)
                {
              //  cout<<dup[i][j]<<" ";
                    ans+=dup[i][j];
                }
        }
        cout<<ans<<"\n";
    }
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
// for iota, accumulate, partial_sum
// #include <numeric>
// #include <fstream>
// #include <deque>
// #include <cmath>
// #include <unordered_map>
// #include <utility>
// #include <unordered_set>
// #include <ctime>
// #include <stack>
// #include <bitset>
// #include <random>
// #include <cstring>
// #include <cassert>
// #include <complex>
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
#define endl "\n"
#define v vector<ll>
#define vu vector<lu>
#define vv vector<vector<ll>>
#define vvu vector<vector<lu>>
#define vld vector<ld>
#define vb vector<bool>
#define vs vector<string>
#define pll pair<ll, ll>
#define vsll vector<set<ll>>
#define vspll vector<set<pair<ll, ll>>>
#define vpll vector<pair<ll, ll>>
#define spll set<pair<ll, ll>>
#define puu pair<lu, lu>
#define vpuu vector<pair<lu, lu>>
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false)
{
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++)
    {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false)
{
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++)
    {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false)
{
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++)
    {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1)
{
    if (end == -1)
        end = a.size() - 1;
    for (ll i = begin; i <= end; i++)
    {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

void yes()
{
    cout << "YES\n";
}

void no()
{
    cout << "NO\n";
}

void yesno(int f)
{
    if (f)
        yes();
    else
        no();
}

void solve()
{
    ll n;
    cin >> n;
    vv adjList(n + 1, v(n + 1, 0));
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            if (i != j)
                adjList[i][j] = x;
        }
    }
    ll k;
    cin >> k;
    for (ll p = 0; p < k; p++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        if (a > b)
        {
            ll t = a;
            a = b;
            b = t;
        }
        amin(adjList[a][b], c);
        amin(adjList[b][a], c);
        ll s = 0;
        // recalculate all other paths based on this path
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = i + 1; j <= n; j++)
            {
                if (b == i)
                {
                    amin(adjList[i][j], adjList[a][b] + adjList[a][j]);
                }
                else
                {
                    amin(adjList[i][j], adjList[i][a] + adjList[a][b] + adjList[b][j]);
                }
                s += adjList[i][j];
            }
        }
        cout << s << " ";
    }
}

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
// for iota, accumulate, partial_sum
// #include <numeric>
// #include <fstream>
// #include <deque>
// #include <cmath>
// #include <unordered_map>
// #include <utility>
// #include <unordered_set>
// #include <ctime>
// #include <stack>
// #include <bitset>
// #include <random>
// #include <cstring>
// #include <cassert>
// #include <complex>
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
#define endl "\n"
#define v vector<ll>
#define vu vector<lu>
#define vv vector<vector<ll>>
#define vvu vector<vector<lu>>
#define vld vector<ld>
#define vb vector<bool>
#define vs vector<string>
#define pll pair<ll, ll>
#define vsll vector<set<ll>>
#define vspll vector<set<pair<ll, ll>>>
#define vpll vector<pair<ll, ll>>
#define spll set<pair<ll, ll>>
#define puu pair<lu, lu>
#define vpuu vector<pair<lu, lu>>
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false)
{
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++)
    {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false)
{
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++)
    {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false)
{
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++)
    {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1)
{
    if (end == -1)
        end = a.size() - 1;
    for (ll i = begin; i <= end; i++)
    {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

void yes()
{
    cout << "YES\n";
}

void no()
{
    cout << "NO\n";
}

void yesno(int f)
{
    if (f)
        yes();
    else
        no();
}

void solve()
{
    ll n;
    cin >> n;
    vv adjList(n + 1, v(n + 1, 0));
    ll s = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            adjList[i][j] = x;
            if (i < j)
                s += x;
        }
    }
    ll k;
    cin >> k;
    for (ll p = 0; p < k; p++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        if (a > b)
        {
            ll t = a;
            a = b;
            b = t;
        }
        if (adjList[a][b] <= c)
        {
            cout << s << " ";
            continue;
        }
        s = 0;
        adjList[a][b] = c;
        adjList[b][a] = c;
        // recalculate all other paths based on this path
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = i + 1; j <= n; j++)
            {
                if (a == j)
                {
                    amin(adjList[i][j], adjList[i][b] + adjList[a][b]);
                }
                else if (b == i)
                {
                    amin(adjList[i][j], adjList[a][b] + adjList[a][j]);
                }
                else
                {
                    amin(adjList[i][j], adjList[i][a] + adjList[a][b] + adjList[b][j]);
                }

                s += adjList[i][j];
                adjList[j][i] = adjList[i][j];
            }
        }
        cout << s << " ";
    }
}

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
// for iota, accumulate, partial_sum
// #include <numeric>
// #include <fstream>
// #include <deque>
// #include <cmath>
// #include <unordered_map>
// #include <utility>
// #include <unordered_set>
// #include <ctime>
// #include <stack>
// #include <bitset>
// #include <random>
// #include <cstring>
// #include <cassert>
// #include <complex>
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
#define endl "\n"
#define v vector<ll>
#define vu vector<lu>
#define vv vector<vector<ll>>
#define vvu vector<vector<lu>>
#define vld vector<ld>
#define vb vector<bool>
#define vs vector<string>
#define pll pair<ll, ll>
#define vsll vector<set<ll>>
#define vspll vector<set<pair<ll, ll>>>
#define vpll vector<pair<ll, ll>>
#define spll set<pair<ll, ll>>
#define puu pair<lu, lu>
#define vpuu vector<pair<lu, lu>>
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false)
{
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++)
    {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false)
{
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++)
    {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false)
{
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++)
    {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1)
{
    if (end == -1)
        end = a.size() - 1;
    for (ll i = begin; i <= end; i++)
    {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

void yes()
{
    cout << "YES\n";
}

void no()
{
    cout << "NO\n";
}

void yesno(int f)
{
    if (f)
        yes();
    else
        no();
}

void solve()
{
    ll n;
    cin >> n;
    vv adjList(n + 1, v(n + 1, 0));
    ll s = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            adjList[i][j] = x;
            if (i < j)
                s += x;
        }
    }
    ll k;
    cin >> k;
    for (ll p = 0; p < k; p++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        if (a > b)
        {
            ll t = a;
            a = b;
            b = t;
        }
        if (adjList[a][b] <= c)
        {
            cout << s << " ";
            continue;
        }
        s = 0;
        adjList[a][b] = c;
        adjList[b][a] = c;
        // recalculate all other paths based on this path
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = i + 1; j <= n; j++)
            {
                if (a == j)
                {
                    amin(adjList[i][j], adjList[i][b] + adjList[a][b]);
                }
                else if (b == i)
                {
                    amin(adjList[i][j], adjList[a][b] + adjList[a][j]);
                }
                else
                {
                    amin(adjList[i][j], adjList[i][a] + adjList[a][b] + adjList[b][j]);
                    amin(adjList[i][j], adjList[i][b] + adjList[a][b] + adjList[a][j]);
                }

                s += adjList[i][j];
                adjList[j][i] = adjList[i][j];
            }
        }
        cout << s << " ";
    }
}

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
// for iota, accumulate, partial_sum
// #include <numeric>
// #include <fstream>
// #include <deque>
// #include <cmath>
// #include <unordered_map>
// #include <utility>
// #include <unordered_set>
// #include <ctime>
// #include <stack>
// #include <bitset>
// #include <random>
// #include <cstring>
// #include <cassert>
// #include <complex>
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
#define endl "\n"
#define v vector<ll>
#define vu vector<lu>
#define vv vector<vector<ll>>
#define vvu vector<vector<lu>>
#define vld vector<ld>
#define vb vector<bool>
#define vs vector<string>
#define pll pair<ll, ll>
#define vsll vector<set<ll>>
#define vspll vector<set<pair<ll, ll>>>
#define vpll vector<pair<ll, ll>>
#define spll set<pair<ll, ll>>
#define puu pair<lu, lu>
#define vpuu vector<pair<lu, lu>>
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false)
{
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++)
    {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false)
{
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++)
    {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false)
{
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++)
    {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1)
{
    if (end == -1)
        end = a.size() - 1;
    for (ll i = begin; i <= end; i++)
    {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

void yes()
{
    cout << "YES\n";
}

void no()
{
    cout << "NO\n";
}

void yesno(int f)
{
    if (f)
        yes();
    else
        no();
}

void solve()
{
    ll n;
    cin >> n;
    vv adjList(n + 1, v(n + 1, 0));
    ll s = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            if (i != j)
            {
                adjList[i][j] = x;
            }
            if (i < j)
                s += x;
        }
    }
    ll k;
    cin >> k;
    for (ll p = 0; p < k; p++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        if (a > b)
        {
            ll t = a;
            a = b;
            b = t;
        }
        if (adjList[a][b] <= c)
        {
            cout << s << " ";
            continue;
        }
        s = 0;
        adjList[a][b] = c;
        adjList[b][a] = c;
        // recalculate all other paths based on this path
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = i + 1; j <= n; j++)
            {
                if (a == j)
                {
                    amin(adjList[i][j], adjList[i][b] + adjList[a][b]);
                }
                else if (b == i)
                {
                    amin(adjList[i][j], adjList[a][b] + adjList[a][j]);
                }
                else
                {
                    amin(adjList[i][j], adjList[i][a] + adjList[a][b] + adjList[b][j]);
                }
                s += adjList[i][j];
            }
        }
        cout << s << " ";
    }
}

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
// for iota, accumulate, partial_sum
// #include <numeric>
// #include <fstream>
// #include <deque>
// #include <cmath>
// #include <unordered_map>
// #include <utility>
// #include <unordered_set>
// #include <ctime>
// #include <stack>
// #include <bitset>
// #include <random>
// #include <cstring>
// #include <cassert>
// #include <complex>
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
#define endl "\n"
#define v vector<ll>
#define vu vector<lu>
#define vv vector<vector<ll>>
#define vvu vector<vector<lu>>
#define vld vector<ld>
#define vb vector<bool>
#define vs vector<string>
#define pll pair<ll, ll>
#define vsll vector<set<ll>>
#define vspll vector<set<pair<ll, ll>>>
#define vpll vector<pair<ll, ll>>
#define spll set<pair<ll, ll>>
#define puu pair<lu, lu>
#define vpuu vector<pair<lu, lu>>
const ll MOD = 1000000007;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))

#define all(x) x.begin(), x.end()

v ga(ll n, bool oneIndexed = false)
{
    v a;
    if (oneIndexed)
        a.push_back(0ll);
    for (ll i = 0; i < n; i++)
    {
        ll p;
        // scanf("%lld", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

vu gau(lu n, bool oneIndexed = false)
{
    vu a;
    if (oneIndexed)
        a.push_back(0llu);
    for (lu i = 0; i < n; i++)
    {
        lu p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}
vs gas(lu n, bool oneIndexed = false)
{
    vs a;
    if (oneIndexed)
        a.push_back("");
    for (lu i = 0; i < n; i++)
    {
        string p;
        // scanf("%lu", &p);
        cin >> p;
        a.push_back(p);
    }
    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1)
{
    if (end == -1)
        end = a.size() - 1;
    for (ll i = begin; i <= end; i++)
    {
        // printf("%lu ", a[i]);
        cout << a[i] << " ";
    }
    cout << "\n";
}

void yes()
{
    cout << "YES\n";
}

void no()
{
    cout << "NO\n";
}

void yesno(int f)
{
    if (f)
        yes();
    else
        no();
}

void solve()
{
    ll n;
    cin >> n;
    vv adjList(n + 1, v(n + 1, 0));
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            ll x;
            cin >> x;
            if (i != j)
                adjList[i][j] = x;
        }
    }
    ll k;
    cin >> k;
    for (ll p = 0; p < k; p++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        amin(adjList[a][b], c);
        ll s = 0;
        // recalculate all other paths based on this path
        for (ll i = 1; i <= n; i++)
        {
            for (ll j = i + 1; j <= n; j++)
            {
                if (adjList[i][j] > adjList[i][a] + adjList[a][b] + adjList[b][j])
                    adjList[i][j] = adjList[i][a] + adjList[a][b] + adjList[b][j];
                s += adjList[i][j];
            }
        }
        cout << s << " ";
    }
}

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 100005
#define ll long long

int n ;
int mat[305][305];

void update(int x , int y , int d){
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            mat[i][j] = min(mat[i][j] , mat[i][x] + d + mat[y][j]);
        }
    }
}

ll sum(){
    ll ans = 0;
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            ans += mat[i][j];
        }
    }
    return ans;
}
int main(){
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++)
            cin >> mat[i][j];
    }

    int k ;
    cin >> k;
    int x, y , d ;
    for(int i = 0 ; i < k ; i++){
        cin >> x >> y >> d;
        //if(mat[x][y] > d){
            update(x , y , d);

        cout << sum() << ' ' ;
    }
    cout << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 100005
#define ll long long

int n ;
int mat[305][305];

void update(int x , int y , int d){
    for(int i = 1 ; i <= n ; i++){
        for(int j =  1 ; j <= n ; j++){
            mat[i][j] = min(mat[i][j] , mat[i][x] + d + mat[y][j]);
        }
    }
}

ll sum(){
    ll ans = 0;
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            ans += mat[i][j];
        }
    }
    return ans;
}
int main(){
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++)
            cin >> mat[i][j];
    }

    int k ;
    cin >> k;
    int x, y , d ;
    for(int i = 0 ; i < k ; i++){
        cin >> x >> y >> d;
        //if(mat[x][y] > d){
        update(x , y , d);
        update(y , x , d);

        cout << sum() << ' ' ;
    }
    cout << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 100005
#define ll long long

int n ;
int mat[305][305];

void update(int x , int y , int d){
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            mat[i][j] = min(mat[i][j] , mat[i][x] + d + mat[y][j]);
        }
    }
}

ll sum(){
    ll ans = 0;
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            ans += mat[i][j];
        }
    }
    return ans;
}
int main(){
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++)
            cin >> mat[i][j];
    }

    int k ;
    cin >> k;
    int x, y , d ;
    for(int i = 0 ; i < k ; i++){
        cin >> x >> y >> d;
        if(mat[x][y] > d){
            update(x , y , d);
        }
        cout << sum() << ' ' ;
    }
    cout << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 100005
#define ll long long

int n ;
int mat[305][305];

void update(int x , int y , int d){
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            mat[i][j] = min(mat[i][j] , mat[i][x] + d + mat[y][j]);
        }
    }
}

ll sum(){
    ll ans = 0;
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            ans += mat[i][j];
        }
    }
    return ans;
}
int main(){
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++)
            cin >> mat[i][j];
    }

    int k ;
    cin >> k;
    int x, y , d ;
    for(int i = 0 ; i < k ; i++){
        cin >> x >> y >> d;
        //if(mat[x][y] > d){
        update(x , y , d);
        update(y , x , d);

        cout << sum() << ' ' ;
    }
    cout << endl;
}


import java.util.Scanner;

public class C25 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[][] dist = new int[n][n];
		long sum = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				dist[i][j] = in.nextInt();
				sum += dist[i][j];
			}
		}
		sum = sum / 2;
		int k = in.nextInt();
		for (int i = 0; i < k; i++) {
			int from = in.nextInt() - 1;
			int to = in.nextInt() - 1;
			int cost = in.nextInt();
			System.out.println();
			if (cost >= dist[from][to]) {
				System.out.println(sum);
				continue;
			}
			for (int x = 0; x < n; x++) {
				for (int y = 0; y < n; y++) {
					if (dist[x][y] > dist[x][from] + dist[to][y] + cost) {
						sum -= (dist[x][y]);
						dist[x][y] = dist[x][from] + dist[to][y] + cost;
						dist[y][x] = dist[x][from] + dist[to][y] + cost;
						sum += (dist[x][y]);
					} else if (dist[x][y] > dist[y][from] + dist[to][x] + cost) {
						sum -= (dist[x][y]);
						dist[x][y] = dist[y][from] + dist[to][x] + cost;
						dist[y][x] = dist[y][from] + dist[to][x] + cost;
						sum += (dist[x][y]);
					}
				}
			}
			System.out.println(sum);
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}

		int k = scan.nextInt();

		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;

			int ret = 0;
			for(int from = 0; from < n; from++){
				int dp[] = new int[n+1];
				boolean visited[] = new boolean[n+1];
				Arrays.fill(dp, Integer.MAX_VALUE);

				Queue<Integer> q = new LinkedList<Integer>();
				q.add(from);
				dp[from] = 0;
				visited[from] = true;
				while(!q.isEmpty()){
					int cur = q.poll();
					for(int next = 0; next < n; next++){
						if(next == cur) continue;
						if(dp[cur] + roads[cur][next] <= dp[next]){
							dp[next] = dp[cur] + roads[cur][next];
							q.add(next);

						}
					}
				}

				for(int j = from + 1; j < n; j++)
					ret += dp[j];
			}
			out.print(ret + " ");
		}
		out.close();
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		double roads[][] = new double[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before +" ");
				continue;
			}
			int ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					roads[u][v] = Math.min(roads[u][v], roads[u][a] + roads[a][b] +roads[b][v]);
					roads[u][v] = Math.min(roads[u][v], roads[u][b] +roads[b][a] +roads[a][v]);
				}
			}

			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before + " ");
				continue;
			}
			int ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					for(int t = 0; t < n; t++){
						roads[u][v] = Math.min(roads[u][v], roads[u][t] + roads[t][v]);
					}
					if(v > u) ret += roads[u][v];
				}
			}
			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;

			int ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					for(int t = 0; t < n; t++){
						roads[u][v] = Math.min(roads[u][v], roads[u][t] + roads[t][v]);
					}
					if(v > u) ret += roads[u][v];
				}
			}
			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		long roads[][] = new long[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		long before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			long c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before +" ");
				continue;
			}
			long ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					roads[u][v] = Math.min(roads[u][v], roads[u][a] + roads[a][b] +roads[b][v]);
					roads[u][v] = Math.min(roads[u][v], roads[u][b] +roads[b][a] +roads[a][v]);
				}
			}

			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;

			int dp[][] = new int[n][n];
			for(int u = 0; u < n; u++)
				for(int v = 0; v < n; v++)
					dp[u][v] = roads[u][v];
			for(int t = 0; t < n; t++){
				for(int u = 0; u < n; u++){
					for(int v = 0; v < n; v++){
						dp[u][v] = Math.min(dp[u][v], dp[u][t] +dp[t][v]);
					}
				}
			}
			int ret = 0;
			for(int u = 0; u < n; u++)
				for(int v = u + 1; v < n; v++)
					ret += dp[u][v];

			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else {
				out.print(before + " ");
				continue;
			}
			int ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					for(int t = 0; t < n; t++){
						roads[u][v] = Math.min(roads[u][v], roads[u][t] + roads[t][v]);
					}
					if(v > u) ret += roads[u][v];
				}
			}
			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before +" ");
				continue;
			}
			int ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					roads[u][v] = Math.min(roads[u][v], roads[u][a] + roads[a][b] +roads[b][v]);
					roads[u][v] = Math.min(roads[u][v], roads[u][b] +roads[b][a] +roads[a][v]);
				}
			}

			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before +" ");
				continue;
			}
			int ret = 0;

			for(int t = 0; t < n; t++){
				for(int u = 0; u < n; u++){
					for(int v = 0; v < n; v++){
						roads[u][v] = Math.min(roads[u][v], roads[u][t] + roads[t][v]);
					}
				}
			}
			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		int before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;

			int ret = 0;

			for(int t = 0; t < n; t++){
				for(int u = 0; u < n; u++){
					for(int v = 0; v < n; v++){
						roads[u][v] = Math.min(roads[u][v], roads[u][t] + roads[t][v]);
					}
				}
			}
			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];
		List<Integer> g[] = new ArrayList[n+1];
		for(int i = 0; i < n; i++)
			g[i] = new ArrayList<Integer>();

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
				g[i].add(j);
				g[j].add(i);
			}
		}

		int k = scan.nextInt();

		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			roads[a][b] = roads[b][a] = c;

			int ret = 0;
			for(int from = 0; from < n; from++){
				int dp[] = new int[n+1];
				boolean visited[] = new boolean[n+1];
				Arrays.fill(dp, Integer.MAX_VALUE);

				Queue<Integer> q = new LinkedList<Integer>();
				q.add(from);
				dp[from] = 0;
				visited[from] = true;
				while(!q.isEmpty()){
					int cur = q.poll();
					for(int next = 0; next < n; next++){
						if(next == cur) continue;
						if(dp[cur] + roads[cur][next] <= dp[next]){
							dp[next] = dp[cur] + roads[cur][next];
							q.add(next);

						}
					}
				}

				for(int j = from + 1; j < n; j++)
					ret += dp[j];
			}
			out.print(ret + " ");
		}
		out.close();
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		double roads[][] = new double[n+1][n+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
			}
		}
		int k = scan.nextInt();
		double before = -1;
		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			double c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;
			else if(before != -1){
				out.print(before +" ");
				continue;
			}
			double ret = 0;

			for(int u = 0; u < n; u++){
				for(int v = 0; v < n; v++){
					roads[u][v] = Math.min(roads[u][v], roads[u][a] + roads[a][b] +roads[b][v]);
					roads[u][v] = Math.min(roads[u][v], roads[u][b] +roads[b][a] +roads[a][v]);
				}
			}

			for(int u = 0; u < n; u++)
				for(int v = u +1; v < n; v++)
					ret += roads[u][v];

			before = ret;
			out.print(ret + " ");
		}
		out.close();
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] arg) {
		FastScanner scan = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = scan.nextInt();
		int roads[][] = new int[n+1][n+1];
		List<Integer> g[] = new ArrayList[n+1];
		for(int i = 0; i < n; i++)
			g[i] = new ArrayList<Integer>();

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				roads[i][j] = scan.nextInt();
				g[i].add(j);
				g[j].add(i);
			}
		}

		int k = scan.nextInt();

		for(int i = 0; i < k; i++){
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();

			if(roads[a][b] > c)
				roads[a][b] = roads[b][a] = c;

			int ret = 0;
			for(int from = 0; from < n; from++){
				int dp[] = new int[n+1];
				boolean visited[] = new boolean[n+1];
				Arrays.fill(dp, Integer.MAX_VALUE);

				Queue<Integer> q = new LinkedList<Integer>();
				q.add(from);
				dp[from] = 0;
				visited[from] = true;
				while(!q.isEmpty()){
					int cur = q.poll();
					for(int next = 0; next < n; next++){
						if(next == cur) continue;
						if(dp[cur] + roads[cur][next] <= dp[next]){
							dp[next] = dp[cur] + roads[cur][next];
							q.add(next);

						}
					}
				}

				for(int j = from + 1; j < n; j++)
					ret += dp[j];
			}
			out.print(ret + " ");
		}
		out.close();
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int [][] graph= new int[n][n];
        for(int i=0 ; i<n ; i++)
            for(int j=0 ; j<n ; j++)
                graph[i][j]=sc.nextInt();
        int q=sc.nextInt();
        int [] arr=new int[q];
        int num=0;
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            graph[from][to]=weight;
            graph[to][from]=weight;
            for(int k=0 ; k<n ; k++){
                for(int i=0 ; i<n ; i++){
                    for(int j=0 ; j<n ; j++){
                        graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);
                    }
                }
            }
            int collect=0;
            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                }
            }
            arr[num]=collect;
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int [][] graph= new int[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                int x=sc.nextInt();
                graph[i][j]=x;
            }
        }
        int collect2=0;
        for(int i=0 ; i<n ; i++){
            for(int j=i ; j<n ; j++){
                collect2+=graph[i][j];
            }
        }
        int q=sc.nextInt();
        int [] arr=new int[q];
        int num=0;
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            int lastw=graph[to][from];
            graph[from][to]=weight;
            graph[to][from]=weight;
            for(int k=0 ; k<n ; k++){
                for(int i=0 ; i<n ; i++){
                    for(int j=0 ; j<n ; j++){
                        graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);
                    }
                }
            }
            int collect=0;
            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                }
            }
            if(collect<=collect2){
                arr[num]=collect;
                collect2=collect;
            }
            else{
                arr[num]=collect2;
                graph[to][from]=lastw;
                graph[from][to]=lastw;
            }
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long [][] graph= new long[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                graph[i][j]=sc.nextLong();
            }
        }
        int q=sc.nextInt();
        long [] arr=new long[q];
        int num=0;
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            long lastw=graph[to][from];
            graph[from][to]=weight;
            graph[to][from]=weight;
            long collect=0;
            if(lastw>weight){
                for(int k=0 ; k<n ; k++){
                    for(int i=0 ;i<n ; i++){
                        for(int j=0 ; j<n ; j++){
                            graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);

                        }
                    }
                }
            }
            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                }
            }
            arr[num]=collect;
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.util.*;

public class RoadsInBerland {
    static void equalize(int [][]first , int [][] second){
        for(int i=0 ; i<first.length ; i++){
            for(int j=0 ; j<first.length ; j++){
                second[i][j]=first[i][j];
            }
        }
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int [][] graph= new int[n][n];
        int [][] graph2= new int[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                int x=sc.nextInt();
                graph[i][j]=x;
                graph2[i][j]=x;
            }
        }
        int q=sc.nextInt();
        int [] arr=new int[q];
        int num=0;
    //  int last=100000000;
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            graph[from][to]=weight;
            graph[to][from]=weight;
            for(int k=0 ; k<n ; k++){
                for(int i=0 ; i<n ; i++){
                    for(int j=0 ; j<n ; j++){
                        graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);
                    }
                }
            }
    /*      for(int i=0 ; i<n ; i++){
                for(int j=0 ; j<n ; j++){
                    System.out.printf("%d ", graph[i][j]);

                }
                System.out.println();
            } */
            int collect=0 , collect2=0;
            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                    collect2+=graph2[i][j];
                }
            }
            if(collect>=collect2){
                arr[num]=collect2;
                equalize(graph2 , graph);
            }
            else{
                arr[num]=collect;
                equalize(graph , graph2);
            }
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int [][] graph= new int[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                graph[i][j]=sc.nextInt();
            }
        }
    /*  for(int k=0 ; k<n ; k++){
            for(int i=0 ;i<n ; i++){
                for(int j=0 ; j<n ; j++){
                    graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);

                }
            }
        } */
/*      for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                System.out.printf(" %d ", graph[i][j]);
            }
            System.out.println();
        } */
        int collect2=0;
        for(int i=0 ; i<n ; i++){
            for(int j=i ; j<n ; j++){
                collect2+=graph[i][j];
            }
        }
        int q=sc.nextInt();
        int [] arr=new int[q];
        int num=0;
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            int lastw=graph[to][from];
            graph[from][to]=weight;
            graph[to][from]=weight;
            int collect=0;
            for(int k=0 ; k<n ; k++){
                for(int i=0 ;i<n ; i++){
                    for(int j=i ; j<n ; j++){
                        graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);

                    }
                }
            }
    /*      for(int i=0 ; i<n ; i++){
                for(int j=0 ; j<n ; j++){
                    System.out.printf(" %d ", graph[i][j]);
                }
                System.out.println();
            } */

            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                }
            }
            if(collect<=collect2){
                arr[num]=collect;
                collect2=collect;
            }
            else{
                arr[num]=collect2;
                graph[to][from]=lastw;
                graph[from][to]=lastw;
            }
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long [][] graph= new long[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                graph[i][j]=sc.nextLong();
            }
        }
        int q=sc.nextInt();
        long [] arr=new long[q];
        int num=0;
        long lastc=0;
        for(int i=0 ; i<n ; i++){
            for(int j=i ; j<n ; j++){
                lastc+=graph[i][j];
            }
        }
        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            long lastw=graph[to][from];
            long collect=0;
            if(lastw>=weight){
                graph[from][to]=weight;
                graph[to][from]=weight;
                for(int i=0 ;i<n ; i++){
                    for(int j=0 ; j<n ; j++){
                        long w1=Math.min(graph[i][j], graph[i][to]+graph[from][j]+weight);
                        long w2=Math.min(graph[i][j], graph[i][from]+graph[to][j]+weight);
                        graph[i][j]=Math.min(Math.min(w1 ,  w2) , graph[i][j]);
                    }
                }
            }
            if(lastw>weight){
                for(int i=0 ; i<n ; i++){
                    for(int j=i ; j<n ; j++){
                        collect+=graph[i][j];
                    }
                }
                arr[num]=collect;
                lastc=collect;
            }
            else{
                arr[num]=lastc;
            }
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }
    }
}


import java.util.*;

public class RoadsInBerland {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long [][] graph= new long[n][n];
        for(int i=0 ; i<n ; i++){
            for(int j=0 ; j<n ; j++){
                graph[i][j]=sc.nextLong();
            }
        }
        int q=sc.nextInt();
        long [] arr=new long[q];
        int num=0;

        for(int v=0 ; v<q ; v++){
            int to=sc.nextInt()-1;
            int from=sc.nextInt()-1;
            int weight=sc.nextInt();
            long lastw=graph[to][from];
            long collect=0;
            if(lastw>=weight){
                graph[from][to]=weight;
                graph[to][from]=weight;
        //      System.out.println("hi");
                for(int k=0 ; k<n ; k++){
                    for(int i=0 ;i<n ; i++){
                        for(int j=0 ; j<n ; j++){
                            graph[i][j]=Math.min(graph[i][j], graph[i][k]+graph[k][j]);

                        }
                    }
                }
            }
/*          for(int i=0 ; i<n ; i++){
                for(int j=0 ; j<n ; j++){
                    System.out.printf(" %d ", graph[i][j]);
                }
                System.out.println();
            } */
            for(int i=0 ; i<n ; i++){
                for(int j=i ; j<n ; j++){
                    collect+=graph[i][j];
                }
            }
            arr[num]=collect;
            num++;
        }
        System.out.printf("%d", arr[0]);
        for(int i=1 ; i<q ; i++){
            System.out.printf(" %d", arr[i]);
        }

    }
}


import java.io.*;
import java.util.*;
public class RoadsinBerlandDiv2 {
    private static int V;
    private static int newE;
    private static long[][] graph = new long[301][301] ;
    public static void main(String[] args){
        PrintWriter pw = new PrintWriter(System.out, true);
        Scanner sc = new Scanner(System.in);
        int a , b ;
        long c , result;
        V = sc.nextInt();
        for(int i=0;i<V;i++){
            for(int j=0;j<V;j++){
                graph[i][j] = sc.nextLong();
            }
        }
        newE = sc.nextInt();
        for(int i=0;i<newE;i++){
            a = sc.nextInt()-1;
            b = sc.nextInt()-1;
            c = sc.nextLong();
            for(int j=0;j<V;j++){
                for(int k=0;k<V;k++){
                    graph[j][k] = Math.min(graph[j][k], Math.min(graph[j][a]+c+graph[b][k], graph[j][b]+c+graph[a][k]));
                }
            }
            result = 0;
            for(int j=0;j<V;j++){
                for(int k=j+1;k<V;k++){
                    result +=graph[j][k];
                }
            }
            if(i!=newE-1){
                pw.print(result+" ");
            }else{
                pw.print(result+"\n");

            }
        }
        pw.close();
    }
}


import java.io.*;
import java.util.*;
public class RoadsinBerlandDiv2 {
    private static int V;
    private static int newE;
    private static int[][] graph = new int[301][301] ;
    public static void main(String[] args){
        PrintWriter pw = new PrintWriter(System.out, true);
        Scanner sc = new Scanner(System.in);
        int a , b , c , result;
        V = sc.nextInt();
        for(int i=0;i<V;i++){
            for(int j=0;j<V;j++){
                graph[i][j] = sc.nextInt();
            }
        }
        newE = sc.nextInt();
        for(int i=0;i<newE;i++){
            a = sc.nextInt()-1;
            b = sc.nextInt()-1;
            c = sc.nextInt();
            for(int j=0;j<V;j++){
                for(int k=0;k<V;k++){
                    graph[j][k] = Math.min(graph[j][k], Math.min(graph[j][a]+c+graph[b][k], graph[j][b]+c+graph[a][k]));
                }
            }
            result = 0;
            for(int j=0;j<V;j++){
                for(int k=j+1;k<V;k++){
                    result +=graph[j][k];
                }
            }
            if(i!=newE-1){
                pw.print(result+" ");
            }else{
                pw.print(result+"\n");

            }
        }
        pw.close();
    }
}


import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;

public class RoadsInBerland_25 {

    public static void main(String[] args) throws IOException {
        // ----------------------------------------------------
        start();
        // ----------------------------------------------------
        int n = nextInt();
//      int[][] a = new int[n][n];
        int[][] s = new int[n][n];

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s.length; j++) {
                s[i][j] = nextInt();
            }
        }
        int m = nextInt();
//      for (int i = 0; i < s.length; i++) {
//          s[i] = Arrays.copyOf(a[i], a.length);
//      }
        for (int t = 0; t < m; t++) {
            int z = nextInt();
            int x = nextInt();
            int c = nextInt();
            z--;
            x--;
            if (c < s[z][x]) {
                s[z][x] = c;
                s[x][z] = c;
            }

            for (int k = 0; k < s.length; k++) {
                for (int i = 0; i < s.length; i++) {
                    for (int j = 0; j < s.length; j++) {
                        if (s[i][k] + s[k][j] < s[i][j]) {
                            s[i][j] = s[i][k] + s[k][j];
                        }
                    }
                }
            }
            int sum = 0;
            for (int i = 0; i < s.length; i++) {
                for (int j = i + 1; j < s.length; j++) {
                    sum += s[i][j];
                }
            }
            if (t != m - 1) {
                out.print(sum + " ");
            } else {
                out.println(sum);
            }

        }

        // ----------------------------------------------------
        end();
        // ----------------------------------------------------
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer sc;

    static void start() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        sc = new StringTokenizer("");
    }

    static void end() throws IOException {
        br.close();
        out.close();
    }

    static String nextString() throws IOException {
        while (!sc.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            sc = new StringTokenizer(s.trim());
        }
        return sc.nextToken();
    }

    static String nextLine() throws IOException {
        String s = br.readLine();
        if (s == null) {
            return null;
        }
        return s;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextString());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    static long[] nextLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    static char[] nextCharArray() throws IOException {
        return nextString().toCharArray();
    }

    static String[] nextStringArray(int n) throws IOException {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = nextString();
        return a;
    }

}


import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;

public class RoadsInBerland_25 {

	public static void main(String[] args) throws IOException {
		// ----------------------------------------------------
		start();
		// ----------------------------------------------------
		int n = nextInt();
//		int[][] a = new int[n][n];
		int[][] s = new int[n][n];

		for (int i = 0; i < s.length; i++) {
			for (int j = 0; j < s.length; j++) {
				s[i][j] = nextInt();
			}
		}
		int m = nextInt();
//		for (int i = 0; i < s.length; i++) {
//			s[i] = Arrays.copyOf(a[i], a.length);
//		}
		for (int t = 0; t < m; t++) {
			int z = nextInt();
			int x = nextInt();
			int c = nextInt();
			z--;
			x--;
//			if (c < s[z][x]) {
				s[z][x] = c;
				s[x][z] = c;
//			}

			for (int k = 0; k < s.length; k++) {
				for (int i = 0; i < s.length; i++) {
					for (int j = 0; j < s.length; j++) {
						if (s[i][k] + s[k][j] < s[i][j]) {
							s[i][j] = s[i][k] + s[k][j];
						}
					}
				}
			}
			int sum = 0;
			for (int i = 0; i < s.length; i++) {
				for (int j = i + 1; j < s.length; j++) {
					sum += s[i][j];
				}
			}
			if (t != m - 1) {
				out.print(sum + " ");
			} else {
				out.println(sum);
			}

		}

		// ----------------------------------------------------
		end();
		// ----------------------------------------------------
	}

	static BufferedReader br;
	static PrintWriter out;
	static StringTokenizer sc;

	static void start() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		sc = new StringTokenizer("");
	}

	static void end() throws IOException {
		br.close();
		out.close();
	}

	static String nextString() throws IOException {
		while (!sc.hasMoreTokens()) {
			String s = br.readLine();
			if (s == null) {
				return null;
			}
			sc = new StringTokenizer(s.trim());
		}
		return sc.nextToken();
	}

	static String nextLine() throws IOException {
		String s = br.readLine();
		if (s == null) {
			return null;
		}
		return s;
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(nextString());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(nextString());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(nextString());
	}

	BigInteger nextBigInteger() throws IOException {
		return new BigInteger(nextString());
	}

	static int[] nextIntArray(int n) throws IOException {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	static long[] nextLongArray(int n) throws IOException {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}

	static char[] nextCharArray() throws IOException {
		return nextString().toCharArray();
	}

	static String[] nextStringArray(int n) throws IOException {
		String[] a = new String[n];
		for (int i = 0; i < n; i++)
			a[i] = nextString();
		return a;
	}

}


import java.io.*;
import java.math.*;
import java.util.*;

public class RoadsInBerland_25 {

    public static void main(String[] args) throws IOException {
        // ----------------------------------------------------
        start();
        // ----------------------------------------------------
        int n = nextInt();
        int[][] s = new int[n][n];

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s.length; j++) {
                s[i][j] = nextInt();
            }
        }
        int m = nextInt();
        long sum = 0;
        for (int i = 0; i < s.length; i++) {
            for (int j = i + 1; j < s.length; j++) {
                sum += s[i][j];
            }
        }
        for (int t = 0; t < m; t++) {
            int z = nextInt();
            int x = nextInt();
            int c = nextInt();
            z--;
            x--;
//          if (c < s[z][x]) {
                sum = 0;
                for (int i = 0; i < s.length; i++) {
                    for (int j = 0; j < s.length; j++) {
                        s[i][j] = Math.min(s[i][j], Math.min(s[i][z] + c
                                + s[x][j], s[i][x] + c + s[z][j]));
                        sum += s[i][j];
                    }
                }
//          }
            if (t != m - 1) {
                out.print(sum / 2 + " ");
            } else {
                out.println(sum / 2);
            }

        }

        // ----------------------------------------------------
        end();
        // ----------------------------------------------------
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer sc;

    static void start() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        sc = new StringTokenizer("");
    }

    static void end() throws IOException {
        br.close();
        out.close();
    }

    static String nextString() throws IOException {
        while (!sc.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            sc = new StringTokenizer(s.trim());
        }
        return sc.nextToken();
    }

    static String nextLine() throws IOException {
        String s = br.readLine();
        if (s == null) {
            return null;
        }
        return s;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextString());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    static long[] nextLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    static char[] nextCharArray() throws IOException {
        return nextString().toCharArray();
    }

    static String[] nextStringArray(int n) throws IOException {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = nextString();
        return a;
    }

}


import java.io.*;
import java.math.*;
import java.util.*;

public class RoadsInBerland_25 {

    public static void main(String[] args) throws IOException {
        // ----------------------------------------------------
        start();
        // ----------------------------------------------------
        int n = nextInt();
        int[][] s = new int[n][n];

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s.length; j++) {
                s[i][j] = nextInt();
            }
        }
        int m = nextInt();
        long sum = 0;
        for (int t = 0; t < m; t++) {
            int z = nextInt();
            int x = nextInt();
            int c = nextInt();
            z--;
            x--;
                sum = 0;
                for (int i = 0; i < s.length; i++) {
                    for (int j = 0; j < s.length; j++) {
                        s[i][j] = Math.min(s[i][j], Math.min(s[i][z] + c
                                + s[x][j], s[i][x] + c + s[z][j]));
                        sum += s[i][j];
                    }
                }
            if (t != m - 1) {
                out.print(sum / 2 + " ");
            } else {
                out.println(sum / 2);
            }

        }

        // ----------------------------------------------------
        end();
        // ----------------------------------------------------
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer sc;

    static void start() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        sc = new StringTokenizer("");
    }

    static void end() throws IOException {
        br.close();
        out.close();
    }

    static String nextString() throws IOException {
        while (!sc.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            sc = new StringTokenizer(s.trim());
        }
        return sc.nextToken();
    }

    static String nextLine() throws IOException {
        String s = br.readLine();
        if (s == null) {
            return null;
        }
        return s;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextString());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    static long[] nextLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    static char[] nextCharArray() throws IOException {
        return nextString().toCharArray();
    }

    static String[] nextStringArray(int n) throws IOException {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = nextString();
        return a;
    }

}


import java.io.*;
import java.math.*;
import java.util.*;

public class RoadsInBerland_25 {

	public static void main(String[] args) throws IOException {
		// ----------------------------------------------------
		start();
		// ----------------------------------------------------
		int n = nextInt();
		int[][] s = new int[n][n];

		for (int i = 0; i < s.length; i++) {
			for (int j = 0; j < s.length; j++) {
				s[i][j] = nextInt();
			}
		}
		int m = nextInt();
		long sum = 0;
		for (int i = 0; i < s.length; i++) {
			for (int j = i + 1; j < s.length; j++) {
				sum += s[i][j];
			}
		}
		for (int t = 0; t < m; t++) {
			int z = nextInt();
			int x = nextInt();
			int c = nextInt();
			z--;
			x--;
			if (c < s[z][x]) {
				sum = 0;
				for (int i = 0; i < s.length; i++) {
					for (int j = 0; j < s.length; j++) {
						s[i][j] = Math.min(s[i][j], Math.min(s[i][z] + c
								+ s[x][j], s[i][x] + c + s[z][j]));
						sum += s[i][j];
					}
				}
			}
			if (t != m - 1) {
				out.print(sum / 2 + " ");
			} else {
				out.println(sum / 2);
			}

		}

		// ----------------------------------------------------
		end();
		// ----------------------------------------------------
	}

	static BufferedReader br;
	static PrintWriter out;
	static StringTokenizer sc;

	static void start() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		sc = new StringTokenizer("");
	}

	static void end() throws IOException {
		br.close();
		out.close();
	}

	static String nextString() throws IOException {
		while (!sc.hasMoreTokens()) {
			String s = br.readLine();
			if (s == null) {
				return null;
			}
			sc = new StringTokenizer(s.trim());
		}
		return sc.nextToken();
	}

	static String nextLine() throws IOException {
		String s = br.readLine();
		if (s == null) {
			return null;
		}
		return s;
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(nextString());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(nextString());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(nextString());
	}

	BigInteger nextBigInteger() throws IOException {
		return new BigInteger(nextString());
	}

	static int[] nextIntArray(int n) throws IOException {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	static long[] nextLongArray(int n) throws IOException {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}

	static char[] nextCharArray() throws IOException {
		return nextString().toCharArray();
	}

	static String[] nextStringArray(int n) throws IOException {
		String[] a = new String[n];
		for (int i = 0; i < n; i++)
			a[i] = nextString();
		return a;
	}

}


import java.io.*;
import java.math.*;
import java.util.*;

public class RoadsInBerland_25 {

    public static void main(String[] args) throws IOException {
        // ----------------------------------------------------
        start();
        // ----------------------------------------------------
        int n = nextInt();
        int[][] a = new int[n][n];

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                a[i][j] = nextInt();
            }
        }
        int m = nextInt();
        for (int t = 0; t < m; t++) {
            int z = nextInt();
            int x = nextInt();
            int c = nextInt();
            z--;
            x--;
            a[z][x] = c;
            a[x][z] = c;

            for (int k = 0; k < a.length; k++) {
                for (int i = 0; i < a.length; i++) {
                    for (int j = 0; j < a.length; j++) {
                        if (a[i][k] + a[k][j] < a[i][j]) {
                            a[i][j] = a[i][k] + a[k][j];
                        }
                    }
                }
            }
            int sum = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = i + 1; j < a.length; j++) {
                    sum += a[i][j];
                }
            }
            if (t!=m-1) {
                out.print(sum+" ");
            }else{
                out.println(sum);
            }

        }

        // ----------------------------------------------------
        end();
        // ----------------------------------------------------
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer sc;

    static void start() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        sc = new StringTokenizer("");
    }

    static void end() throws IOException {
        br.close();
        out.close();
    }

    static String nextString() throws IOException {
        while (!sc.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            sc = new StringTokenizer(s.trim());
        }
        return sc.nextToken();
    }

    static String nextLine() throws IOException {
        String s = br.readLine();
        if (s == null) {
            return null;
        }
        return s;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextString());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    static long[] nextLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    static char[] nextCharArray() throws IOException {
        return nextString().toCharArray();
    }

    static String[] nextStringArray(int n) throws IOException {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = nextString();
        return a;
    }

}


import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;

public class RoadsInBerland_25 {

    public static void main(String[] args) throws IOException {
        // ----------------------------------------------------
        start();
        // ----------------------------------------------------
        int n = nextInt();
        // int[][] a = new int[n][n];
        int[][] s = new int[n][n];

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s.length; j++) {
                s[i][j] = nextInt();
            }
        }
        int m = nextInt();
        // for (int i = 0; i < s.length; i++) {
        // s[i] = Arrays.copyOf(a[i], a.length);
        // }
        int sum = 0;
        // sum = 0;
        for (int i = 0; i < s.length; i++) {
            for (int j = i + 1; j < s.length; j++) {
                sum += s[i][j];
            }
        }
        for (int t = 0; t < m; t++) {
            int z = nextInt();
            int x = nextInt();
            int c = nextInt();
            z--;
            x--;
            if (c > s[z][x]) {
                if (t != m - 1) {
                    out.print(sum + " ");
                } else {
                    out.println(sum);
                }
                continue;
            }
            s[z][x] = c;
            s[x][z] = c;

            for (int k = 0; k < s.length; k++) {
                for (int i = 0; i < s.length; i++) {
                    for (int j = 0; j < s.length; j++) {
                        if (s[i][k] + s[k][j] < s[i][j]) {
                            s[i][j] = s[i][k] + s[k][j];
                        }
                    }
                }
            }
            sum = 0;
            for (int i = 0; i < s.length; i++) {
                for (int j = i + 1; j < s.length; j++) {
                    sum += s[i][j];
                }
            }
            if (t != m - 1) {
                out.print(sum + " ");
            } else {
                out.println(sum);
            }

        }

        // ----------------------------------------------------
        end();
        // ----------------------------------------------------
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer sc;

    static void start() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        sc = new StringTokenizer("");
    }

    static void end() throws IOException {
        br.close();
        out.close();
    }

    static String nextString() throws IOException {
        while (!sc.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            sc = new StringTokenizer(s.trim());
        }
        return sc.nextToken();
    }

    static String nextLine() throws IOException {
        String s = br.readLine();
        if (s == null) {
            return null;
        }
        return s;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextString());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    static long[] nextLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    static char[] nextCharArray() throws IOException {
        return nextString().toCharArray();
    }

    static String[] nextStringArray(int n) throws IOException {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = nextString();
        return a;
    }

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Erasyl Abenov
 *
 *
 */
public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
            try (PrintWriter out = new PrintWriter(outputStream)) {
                TaskB solver = new TaskB();
                solver.solve(1, in, out);
            }
    }
}
class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{
        int n = in.nextInt();
        int a[][] = new int[n][n];
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < n; ++j){
                a[i][j] = in.nextInt();
            }
        }
        int k = in.nextInt();
        for(int t = 0; t < k; ++t){
            int x = in.nextInt();
            int y = in.nextInt();
            x--; y--;
            int dist = in.nextInt();
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < n; ++j){
                    a[i][j] = Math.min(a[i][j], Math.min(a[i][y] + a[x][j], a[i][x] + a[y][j]) + dist);
                }
            }
            long cnt = 0;
            for(int i = 0; i < n; ++i){
                for(int j = i + 1; j < n; ++j){
                    cnt += a[i][j];
                }
            }
            out.print(cnt);
            out.print(" ");
        }
    }
}
class InputReader {
    private final BufferedReader reader;
    private StringTokenizer tokenizer;
    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }
    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(nextLine());
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
    public BigInteger nextBigInteger(){
        return new BigInteger(next());
    }
    public long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }
}