#include<iostream>
using namespace std;
int main(){
    int dp[301][301];
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            cin>>dp[i][j];
    for(int k=0;k<n;++k)
        for(int i=0;i<n;++i)
            for(int j=0;j<n;++j)
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
    int m,a,b,c;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>a>>b>>c;
        long long s=0;
        --a;
        --b;
        dp[a][b]=dp[b][a]=min(c,dp[a][b]);
            for(int i=0;i<n;++i)
                for(int j=0;j<n;++j){
                    if(a==i)
                        dp[j][i]=dp[i][j]=min(dp[i][j],dp[i][b]+dp[b][j]);
                    if(b==j)
                        dp[j][i]=dp[i][j]=min(dp[i][j],dp[i][a]+dp[a][j]);
                }
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                s+=dp[i][j];
        cout<<s<<" ";
    }

}


#include<iostream>
using namespace std;
int main(){
    int dp[301][301];
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            cin>>dp[i][j];
    int m,a,b,c;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>a>>b>>c;
        long long s=0;
        --a;
        --b;
        dp[a][b]=dp[b][a]=min(c,dp[a][b]);
        for(int i=0;i<n;++i)
            for(int j=0;j<n;++j)
                dp[i][j]=dp[i][j]=min(dp[i][j],min(dp[i][a]+dp[a][b]+dp[b][j],dp[i][b]+dp[b][a]+dp[a][j]));
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                s+=dp[i][j];
        cout<<s<<" ";
    }

}


#include<iostream>
using namespace std;
int main(){
    int dp[301][301];
    int f[301][301];
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            cin>>dp[i][j];
    int m,a,b,c;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>a>>b>>c;
        long long s=0;
        --a;
        --b;
        for(int j=0;j<n;++j)
            for(int k=0;k<n;++k)
                f[j][k]=dp[j][k];
        f[a][b]=f[b][a]=c;
        for(int k=0;k<n;++k)
            for(int i=0;i<n;++i)
                for(int j=0;j<n;++j)
                    f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                s+=f[i][j];
        cout<<s<<" ";
    }

}


#include<iostream>
using namespace std;
int main(){
    int dp[301][301];
    int f[301][301];
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            cin>>dp[i][j];
    int m,a,b,c;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>a>>b>>c;
        long long s=0;
        --a;
        --b;
        dp[a][b]=dp[b][a]=min(c,dp[a][b]);
        for(int k=0;k<n;++k)
            for(int i=0;i<n;++i)
                for(int j=0;j<n;++j)
                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                s+=dp[i][j];
        cout<<s<<" ";
    }

}


#include<iostream>
using namespace std;
int main(){
    int dp[301][301];
    int f[301][301];
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            cin>>dp[i][j];
    int m,a,b,c;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>a>>b>>c;
        long long s=0;
        --a;
        --b;
        dp[a][b]=dp[b][a]=min(c,dp[a][b]);
            for(int i=0;i<n;++i)
                for(int j=0;j<n;++j){
                    if(a!=i && a!=j)
                    dp[i][j]=min(dp[i][j],dp[i][a]+dp[a][j]);
                    if(b!=i && b!=j)
                    dp[i][j]=min(dp[i][j],dp[i][b]+dp[b][j]);
                }
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                s+=dp[i][j];
        cout<<s<<" ";
    }

}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        if (towns[a[i]][b[i]] > c[i])
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = z + 1; j < N; j++)
                {
                    d[j][z] = d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
                }
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int **towns = new int*[N];
    for (int i = 0; i < N; i++)
    {
        towns[i] = new int[N];
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];
    }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int z = 0; z < K; z++)
    {
        cin >> a >> b >> c;
        a--, b--;

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][a] + c + towns[b][j]);
        sum = 0;
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                sum += towns[i][j];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 1000*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int i = 0; i < N; i++)
            d[i].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    d[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        if (towns[a[i]][b[i]] > c[i])
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = 0; j < N; j++)
                    d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                for (int k = 0; k < N; k++)
                    towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector<vector<int>> d(N), d2;
        d2 = towns;
        for (int i = 0; i < N; i++)
            d[i].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    d[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        int sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int k = 0; k < N; k++)
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = 0; j < N; j++)
                    d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;
        for (int z = 0; z < N; z++)
            towns[z][z] = 0;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <numeric>
#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int **towns = new int*[N];
    for (int i = 0; i < N; i++)
    {
        towns[i] = new int[N];
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];
    }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int z = 0; z < K; z++)
    {
        cin >> a >> b >> c;
        a--, b--;

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][a] + c + towns[b][j]);
        sum = 0;
        for (int i = 0; i < N; i++)
            sum = accumulate(towns[i], towns[i] + N, sum);
            //for (int j = i + 1; j < N; j++)
            //  sum += towns[i][j];
        sum /= 2;
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long INF = 10*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long> > towns (N, vector<long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    vector< vector<long> > d(N), d2;
    for (int i = 0; i < K; i++)
    {
        if (towns[a[i]][b[i]] > c[i])
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = z + 1; j < N; j++)
                {
                    d[j][z] = d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
                }
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<int> > d(N), d2;
        d2 = towns;
        for (int i = 0; i < N; i++)
            d[i].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    d[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        int sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = 0; j < N; j++)
                    d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long INF = 10*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long> > towns (N, vector<long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    vector < vector<long> > d(N, vector<long> (N));
    vector < vector<long> > d2;

    int a, b, c;
    /*int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];*/
    for (int i = 0; i < K; i++)
    {
        /*cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;*/
        cin >> a >> b >> c;
        a--, b--;

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

        d2 = towns;
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = z + 1; j < N; j++)
                {
                    d[j][z] = d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
                }
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int towns[300][300];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int z = 0; z < K; z++)
    {
        cin >> a >> b >> c;
        a--, b--;

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][a] + c + towns[b][j]);
        sum = 0;
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                sum += towns[i][j];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int i = 0; i < N; i++)
            d[i].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    d[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                for (int k = 0; k < N; k++)
                    towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int **towns = new int*[N];
    for (int i = 0; i < N; i++)
    {
        towns[i] = new int[N];
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];
    }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int k = a; k < b; k++)
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const long long INF = 100*1000*1000*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<long long> > towns (N, vector<long long> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;

    int *a = new int[K];
    int *b = new int[K];
    int *c = new int[K];
    for (int i = 0; i < K; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
    }

    for (int i = 0; i < K; i++)
    {
        if (towns[a[i]][b[i]] > c[i])
        towns[a[i]][b[i]] = towns[b[i]][a[i]] = c[i];

        for (int z = 0; z < N; z++)
        {
            for (int j = z + 1; j < N; j++)
            {
                for (int k = 0; k < N; k++)
                {
                    if (towns[z][k] + towns[k][j] < towns[z][j])
                    {
                        towns[j][z] = towns[z][j] = towns[z][k] + towns[k][j];
                    }
                }
            }
        }

/*
        vector< vector<long long> > d(N), d2;
        d2 = towns;
        for (int z = 0; z < N; z++)
            d[z].resize(N + 1);
        for (int k = 0; k < N; k++)
        {
            for (int z = 0; z < N; z++)
                for (int j = z + 1; j < N; j++)
                {
                    d[j][z] = d[z][j] = min(d2[z][j], d2[z][k] + d2[k][j]);
                }
            d.swap(d2);
        }
        d.swap(d2);
        towns = d;*/

        long long sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <string.h>
#include <math.h>

using namespace std;

int main()
{
    int N;
    scanf("%d", &N);

    int towns[300][300];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            scanf("%d", &towns[i][j]);

    int K;
    scanf("%d", &K);

    long long sum = 0;
    int a, b, c;
    for (int z = 0; z < K; z++)
    {
        scanf("%d %d %d", &a, &b, &c);
        a--, b--;

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][a] + c + towns[b][j]);
        sum = 0;
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                sum += towns[i][j];
        printf("%I64d ", sum);
    }
    printf("\n");
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int **towns = new int*[N];
    for (int i = 0; i < N; i++)
    {
        towns[i] = new int[N];
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];
    }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int k = 0; k < N; k++)
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int **towns = new int*[N];
    for (int i = 0; i < N; i++)
    {
        towns[i] = new int[N];
        for (int j = 0; j < N; j++)
            cin >> towns[i][j];
    }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int k = a > 0 ? a - 1 : 0; k < b < N ? b + 1 : N; k++)
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                    towns[j][i] = towns[i][j] = min(min(towns[i][j], towns[i][a] + towns[a][j]), min(towns[i][j], towns[i][b] + towns[b][j]));

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

const int INF = 100*1000*1000;

int main()
{
    int N;
    cin >> N;

    vector < vector<int> > towns (N, vector<int> (N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            int t;
            cin >> t;
            towns[i][j] = t ? t : INF;
        }

    int K;
    cin >> K;
    cin.ignore();

    long long sum = 0;
    int a, b, c;
    for (int m = 0; m < K; m++)
    {
        cin >> a >> b >> c;
        a--, b--;

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

        for (int z = 0; z < N; z++)
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    for (int k = 0; k < N; k++)
                        towns[j][i] = towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);

        sum = 0;
        for (int j = 0; j < N; j++)
            for (int k = j + 1; k < N; k++)
                sum += towns[j][k];
        cout << sum << " ";
    }
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	long long ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		if(c > x[a][b]) {
			printf("%lld\n", ans);
			continue;
		}
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = x[j][i] = min(x[i][j], x[j][i]);

		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%lld ", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	int ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));

		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%d ", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp(i, n) for(int i=1 ; i<=n ; ++i)
#define FOR(i, a, n) for(int i=a ; i<n ; ++i)

using namespace std;

int n, k, x, y, c ;
long long g[305][305], sum=0;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	lp(i, n) lp(j, n)
			scanf("%lld", &g[i][j]);
	FOR(i, 1, n) FOR(j, i+1, n+1)
			sum += g[i][j];
	scanf("%d", &k);
	lp(i, k) {
		scanf("%d%d%d", &x, &y, &c);
		int tmp = g[x][y];
		g[x][y] = g[y][x] = min(g[x][y], (long long)c);
		if (i > 1) 	putchar(' ');
		if (g[x][y] == tmp)
			printf("%lld", sum);
		else {
			sum = 0;
			FOR(i, 1, n) FOR(j, i+1, n+1)
				g[i][j] = g[j][i] = min(g[i][j],
						min(g[i][x] + g[x][y] + g[y][j], g[i][y] + g[y][x] + g[x][j]));

			FOR(i, 1, n) FOR(j, i+1, n+1) sum += g[i][j];
			printf("%lld", sum);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	int ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = x[j][i] = min(x[i][j], x[j][i]);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%d ", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp(i, n) for(int i=1 ; i<=n ; ++i)
#define FOR(i, a, n) for(int i=a ; i<n ; ++i)

using namespace std;

int n, k, x, y, c ;
int g[305][305];
long long sum=0;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	lp(i, n) lp(j, n)
		scanf("%d", &g[i][j]);
	FOR(i, 1, n) FOR(j, i+1, n+1)
		sum += g[i][j];
	scanf("%d", &k);
	lp(i, k) {
		scanf("%d%d%d", &x, &y, &c);
		if(x > y) swap(x,y);
		int tmp = g[x][y];
		g[x][y] = min(g[x][y], c);
		if (i > 1) 	putchar(' ');
		if (g[x][y] == tmp)
			printf("%lld", sum);
		else {
			sum = 0;
			FOR(i, 1, n) FOR(j, i+1, n+1)
				g[i][j] = min(g[i][j], min(g[i][x] + g[x][y] + g[y][j], g[i][y] + g[x][y] + g[x][j]));
			FOR(i, 1, n) FOR(j, i+1, n+1) sum += g[i][j];
			printf("%lld", sum);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	long long ans = 0;
	for(int i=0 ; i<n ; ++i)
		for(int j=i+1 ; j<n ; ++j)
			ans += x[i][j];
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		if(c >= x[a][b]) {
			printf("%lld\n", ans);
			continue;
		}
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = x[j][i] = min(x[i][j], x[j][i]);

		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%lld\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp(i, n) for(int i=1 ; i<=n ; ++i)
#define FOR(i, a, n) for(int i=a ; i<n ; ++i)

using namespace std;

int n, k, x, y, c ;
int g[305][305];
long long sum=0;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	lp(i, n) lp(j, n)
			scanf("%d", &g[i][j]);
	FOR(i, 1, n) FOR(j, i+1, n+1)
			sum += g[i][j];
	scanf("%d", &k);
	lp(i, k) {
		scanf("%d%d%d", &x, &y, &c);
		int tmp = g[x][y];
		g[x][y] = g[y][x] = min(g[x][y], c);
		if (i > 1) 	putchar(' ');
		if (g[x][y] == tmp)
			printf("%lld", sum);
		else {
			sum = 0;
			FOR(i, 1, n) FOR(j, i+1, n+1)
				g[i][j] = g[j][i] = min(g[i][j],
						min(g[i][x] + g[x][y] + g[y][j], g[i][y] + g[y][x] + g[x][j]));

			FOR(i, 1, n) FOR(j, i+1, n+1) sum += g[i][j];
			printf("%lld", sum);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp(i, n) for(int i=1 ; i<=n ; ++i)
#define FOR(i, a, n) for(int i=a ; i<n ; ++i)

using namespace std;

int n, k, x, y, c, sum = 0;
int g[305][305];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	lp(i, n) lp(j, n)
			scanf("%d", &g[i][j]);
	FOR(i, 1, n) FOR(j, i+1, n+1)
			sum += g[i][j];
	scanf("%d", &k);
	lp(i, k) {
		scanf("%d%d%d", &x, &y, &c);
		int tmp = g[x][y];
		g[x][y] = g[y][x] = min(g[x][y], c);
		if (i > 1) 	putchar(' ');
		if (g[x][y] == tmp)
			printf("%d", sum);
		else {
			sum = 0;
			FOR(i, 1, n) FOR(j, i+1, n+1)
				g[i][j] = g[j][i] = min(g[i][j],
						min(g[i][x] + g[x][y] + g[y][j], g[i][y] + g[y][x] + g[x][j]));

			FOR(i, 1, n) FOR(j, i+1, n+1) sum += g[i][j];
			printf("%d", sum);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define lp(i, n) for(int i=1 ; i<=n ; ++i)
#define FOR(i, a, n) for(int i=a ; i<n ; ++i)

using namespace std;

int n, k, x, y, c ;
int g[305][305];
long long sum=0;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	lp(i, n) lp(j, n)
		scanf("%d", &g[i][j]);
	FOR(i, 1, n) FOR(j, i+1, n+1)
		sum += g[i][j];
	scanf("%d", &k);
	lp(i, k) {
		scanf("%d%d%d", &x, &y, &c);
		if(x > y) swap(x,y);
		int tmp = g[x][y];
		g[x][y] = min(g[x][y], c);
		if (i > 1) 	putchar(' ');
		if (g[x][y] == tmp)
			printf("%lld", sum);
		else {
			sum = 0;
			FOR(i, 1, n) FOR(j, i+1, n+1){
				int lx = min(i, x), ly = min(j,y);
				int mx = max(i, x), my = max(j, y);
				int ix = min(j, x), iy = min(i, y);
				int rx = max(j, x), ry = max(i, y);
				g[i][j] = min(g[i][j], min(g[lx][mx] + g[x][y] + g[ly][my], g[iy][ry] + g[x][y] + g[ix][rx]));
			}
			FOR(i, 1, n) FOR(j, i+1, n+1) sum += g[i][j];
			printf("%lld", sum);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	long long ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = x[j][i] = min(x[i][j], x[j][i]);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%lld ", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	long long ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		if(c > x[a][b]) {
			printf("%lld\n", ans);
			continue;
		}
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=i+1 ; j<n ; ++j)
				x[i][j] = x[j][i] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));

		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += x[i][j];
		printf("%lld ", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a, b, c, t, x[303][303];

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i=0 ; i<n ; ++i)
		for(int j=0 ; j<n ; ++j)
			scanf("%d", &x[i][j]);
	for(int k=0 ; k<n ; ++k)
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[i][k] + x[k][j]);

	int ans = 0;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d%d", &a, &b, &c);
		--a, --b;
		ans = 0;
		x[a][b] = x[b][a] = min(x[a][b], c);
		for(int i=0 ; i<n ; ++i)
			for(int j=0 ; j<n ; ++j)
				x[i][j] = min(x[i][j], x[a][b] + min(x[i][a] + x[b][j], x[i][b] + x[a][j]));

		for(int i=0 ; i<n ; ++i)
			for(int j=i+1; j<n ; ++j)
				ans += min(x[i][j], x[j][i]);
		printf("%d ", ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;

void floyd(int i){
  for(int j = 1; j <= n; j++){
    for(int s = 1; s <= n; s++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(x);
    tool();
    cout << p <<' ';
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int n ,m ,a[310][310] ,k ,x ,y ,z;
long long p;

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    p = 0;
    cin >> x >> y >> z;
    a[x][y] = a[y][x] = min(a[x][y] , z);
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    a[j][s] = min(a[j][s] , a[j][x] + a[x][s]);
      }
    }
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    a[j][s] = min(a[j][s] , a[j][y] + a[y][s]);
      }
    }
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    p += a[j][s];
      }
    }
    cout << p / 2;
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int j = 1; j <= n; j++){
    for(int s = 1; s <= n; s++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  for(int i = 1; i <= n; i++){
    floyd(i);
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(y);
    tool();
    cout << p;
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int s = 1; s <= n; s++){
    for(int j = 1; j <= n; j++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    for(int j=1;j<=n;j++){
      floyd(j);}
    tool();
    cout << p <<' ';
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int j = 1; j <= n; j++){
    for(int s = 1; s <= n; s++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  for(int i = 1; i <= n; i++){
    floyd(i);
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(y);
    tool();
    cout << p <<' ';
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int j = 1; j <= n; j++){
    for(int s = 1; s <= n; s++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  return p/2;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  for(int i = 1; i <= n; i++){
    floyd(i);
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(y);
    tool();
    cout << p;
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int n ,m ,a[310][310] ,k ,x ,y ,z;
long long p;

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    p = 0;
    cin >> x >> y >> z;
    a[x][y] = a[y][x] = min(a[x][y] , z);
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    a[j][s] = min(a[j][s] , a[j][x] + a[x][s]);
      }
    }
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    a[j][s] = min(a[j][s] , a[j][y] + a[y][s]);
      }
    }
    for(int j = 1; j <= n; j++){
      for(int s = 1; s <= n; s++){
    p += a[j][s];
      }
    }
    cout << p / 2 <<' ';
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int s = 1; s <= n; s++){
    for(int j = 1; j <= n; j++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(y);
    tool();
    cout << p <<' ';
  }
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[310][310] ,n, m, x, y, k, p = 0, q = 0;;

void floyd(int i){
  for(int s = 1; s <= n; s++){
    for(int j = 1; j <= n; j++){
      a[i][j] = a[j][i] = min(a[i][j], a[i][s] + a[s][j]);
    }
  }
}

int tool(){
  p = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      p += a[i][j];
    }
  }
  p /= 2;
  return p;
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> x >> y >> k;
    a[x][y] = a[y][x] = min(a[x][y], k);
    floyd(y);
    floyd(x);
    tool();
    cout << p <<' ';
  }
  return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                              map[u][v]=map[v][u]=c;
                              for(j=1;j<=n;j++)
                              if(map[j][u]+map[u][v]<map[j][v])
                              map[j][v]=map[v][j]=map[j][u]+map[u][v];
                              for(j=1;j<=n;j++)
                              if(map[v][j]+map[u][v]<map[j][u])
                              map[j][u]=map[u][j]=map[j][v]+map[u][v];
               }
               long long ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=map[u][v];
               if(i!=m)
               printf("%d ",ans);
               else
               printf("%lld\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                              map[u][v]=map[v][u]=c;
                              for(j=1;j<=n;j++)
                              if(map[j][u]+map[u][v]<map[j][v])
                              map[j][v]=map[v][j]=map[j][u]+map[u][v];
                              for(j=1;j<=n;j++)
                              if(map[v][j]+map[u][v]<map[j][u])
                              map[j][u]=map[u][j]=map[j][v]+map[u][v];
               }
               int ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=map[u][v];
               if(i!=m)
               printf("%d ",ans);
               else
               printf("%d\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c,k;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                   map[u][v]=map[v][u]=c;
                   for(j=1;j<=n;j++)
                   for(k=1;k<=n;k++)
                   if(map[j][u]+map[u][v]+map[v][k]<map[j][k])
                   map[j][k]=map[k][j]=map[j][u]+map[u][v]+map[v][k];
               }
               long long ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=(long long)map[u][v];
               if(i!=m)
               cout<<ans<<" ";
               else
               cout<<ans<<endl;
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c,k;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                   map[u][v]=map[v][u]=c;
                   for(j=1;j<=n;j++)
                   for(k=1;k<=n;k++)
                   if(map[j][u]+map[u][v]+map[v][k]<map[j][k])
                   map[j][k]=map[k][j]=map[j][u]+map[u][v]+map[v][k];
               }
               long long ans=0;
               for(u=1;u<=n;u++)
               for(v=1;v<=n;v++)
               ans+=(long long)map[u][v];
               ans/=2;
               if(i!=m)
               printf("%lld ",ans);
               else
               printf("%lld\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               map[u][v]=map[v][u]=c;
               for(j=1;j<=n;j++)
               if(map[i][u]+map[u][v]<map[j][v])
               map[j][v]=map[v][j]=map[j][u]+map[u][v];
               for(j=1;j<=n;j++)
               if(map[v][j]+map[u][v]<map[j][u])
               map[j][u]=map[u][j]=map[j][v]+map[u][v];
               int ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=map[u][v];
               if(i!=m)
               printf("%d ",ans);
               else
               printf("%d\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c,k,l;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                   map[u][v]=map[v][u]=c;
                   for(j=1;j<=n;j++)
                   for(k=1;k<=n;k++)
                   for(l=1;l<=n;l++)
                   if(map[k][j]+map[j][l]<map[k][l])
                   map[k][l]=map[k][j]+map[j][l];
               }
               long long ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=map[u][v];
               if(i!=m)
               printf("%lld ",ans);
               else
               printf("%lld\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c,k;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                   map[u][v]=map[v][u]=c;
                   for(j=1;j<=n;j++)
                   for(k=1;k<=n;k++)
                   if(map[j][u]+map[u][v]+map[v][k]<map[j][k])
                   map[j][k]=map[k][j]=map[j][u]+map[u][v]+map[v][k];
               }
               long long ans=0;
               for(u=1;u<n;u++)
               for(v=u+1;v<=n;v++)
               ans+=map[u][v];
               if(i!=m)
               printf("%lld ",ans);
               else
               printf("%lld\n",ans);
         }
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
using namespace std;
int map[310][310];
int main()
{
    int n,m;
    int i,j,u,v,c,k;
    while(scanf("%d",&n)!=EOF)
    {
         for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         scanf("%d",&map[i][j]);
         scanf("%d",&m);
         for(i=1;i<=m;i++)
         {
               scanf("%d%d%d",&u,&v,&c);
               if(map[u][v]>c)
               {
                   map[u][v]=map[v][u]=c;
                   for(j=1;j<=n;j++)
                   for(k=1;k<=n;k++)
                   if(map[j][u]+map[u][v]+map[v][k]<map[j][k])
                   map[j][k]=map[k][j]=map[j][u]+map[u][v]+map[v][k];
               }
               long long ans=0;
               for(u=1;u<=n;u++)
               for(v=u+1;v<=n;v++)
               ans+=(long long)map[u][v];
               if(i!=m)
               printf("%lld ",ans);
               else
               printf("%lld\n",ans);
         }
    }
    return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int n;

void fl()
{
//	cout << "flo" << endl;
	for (int k=0; k<n; k++)
	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl();
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

/*		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < n; y++)
				cout << a[x][y] << " ";
			cout << endl;
		}*/
	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
		was[x] = true;
		for (int i=0; i<n; i++)
			d[i] = min(d[i], d[x] + a[x][i]);
		int y = 333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
		x = y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;

/*	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;*/

	for (int k=0; k<n; k++)
	for (int i=0; i<n; i++)
	for (int j=i+1; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x+1; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
		was[x] = true;
		for (int i=0; i<n; i++)
			d[i] = min(d[i], d[x] + a[x][i]);
		int y = 333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
		x = y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;

/*	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;*/

	for (int k=0; k<n; k++)
	for (int i=k+1; i<n; i++)
	for (int j=i+1; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x+1; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	long long s=0;

	for (int alpha=0; alpha<m; alpha++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		s=0;
		for (int i=0; i<n; i++)
		for (int j=0; j<n; j++)
		{
			a[i][j] = min(a[i][j], a[i][q] + a[j][w] + e);
			a[i][j] = min(a[i][j], a[j][q] + a[i][w] + e);
			a[j][i] = a[i][j];
			s += a[i][j];
		}
		s = s / 2;

		printf("%I64d ",s);

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
		was[x] = true;
		for (int i=0; i<n; i++)
			d[i] = min(d[i], d[x] + a[x][i]);
		int y = 333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
		x = y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;

/*	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;*/

	for (int k=0; k<n; k++)
	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x+1; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
//		cout << x << endl;
		was[x]=true;
		for (int i=0; i<n; i++)
			d[i]=min(d[i],d[x]+a[x][i]);
//		cout << x << " " << was[x] << endl;
		int y=333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
//		cout << x << " " << was[x] << endl;
		x=y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;
//	for (int k=0; k<n; k++)
	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;

	for (int k=0; k<n; k++)
	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
		was[x]=true;
		for (int i=0; i<n; i++)
			d[i]=min(d[i],d[x]+a[x][i]);
		int y=333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
		x=y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;
//	for (int k=0; k<n; k++)
	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;

	for (int k=0; k<n; k++)
	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>
#include <memory.h>

#include <time.h>
#include <sys/time.h>

using namespace std;

int a[300][300];
int d[350];
bool was[300];
int n;

void dei(int x)
{
	memset(d,125,sizeof(d));
	d[x]=0;
	memset(was,false,sizeof(was));
        while (true)
	{
		was[x] = true;
		for (int i=0; i<n; i++)
			d[i] = min(d[i], d[x] + a[x][i]);
		int y = 333;
		for (int i=0; i<n; i++)
			if (!was[i])
			{
				if (d[y] > d[i])
				{
					y=i;
				}
			}
		if (y == 333) break;
		x = y;
	}
	return;
}

void fl(int q,int w)
{
//	cout << "flo" << endl;

/*	dei(q);
	for (int i=0; i<n; i++)
	{
		a[q][i] = d[i];
		a[i][q] = d[i];
	}
	dei(w);

	for (int i=0; i<n; i++)
	{
		a[w][i] = d[i];
		a[i][w] = d[i];
	}

	return;*/

	for (int k=0; k<n; k++)
	for (int i=k+1; i<n; i++)
	for (int j=j+1; j<n; j++)
	{
//		cout << k << " " << i << " " << j << endl;
//		if (i == 1 && j == 2)
//			cout << a[i][j] << " & " << a[i][k] << "  " << a[k][j] << endl;
		if (a[i][j] > a[i][k]+a[k][j])
		{
//			cout << i << " " << j << "  -  " << k << endl;
			a[i][j] = a[i][k] + a[k][j];
			a[j][i] = a[i][j];
		}
	}
};

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
	{
		scanf("%d",&a[i][j]);
	}

	int m;
	scanf("%d",&m);
	int s=0;

	for (int x = 0; x < n; x++)
	for (int y = x; y < n; y++)
		s += a[x][y];

	for (int i=0; i<m; i++)
	{
		int q,w,e;
		scanf("%d%d%d",&q,&w,&e);
		q--;
		w--;
		if (a[q][w] > e)
		{
			a[q][w] = e;
			a[w][q] = e;
			fl(q,w);
			s=0;
			for (int x = 0; x < n; x++)
			for (int y = x+1; y < n; y++)
				s += a[x][y];
		}
		cout << s << " ";

	}

//	in.getline(s);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;

int main ()
{
    long long int n, g[310][310], k, a, b, c;
    scanf("%lld", &n);
    for (long long int i = 0; i < n; i++)
        for (long long int j = 0; j < n; j++)
            scanf("%lld", &g[i][j]);
    scanf("%lld", &k);
    for (long long int i = 0; i < k; i++)
    {
        scanf("%lld%lld%lld", &a, &b, &c);
		--a, --b;
		long long int t = min (c, g[a][b]);
        g[a][b] = g[b][a] = t;
        long long int ans = 0;
        for (long long int ii = 0; ii < n; ii++)
            for (long long int jj = 0; jj < n; jj++)
            {
                g[ii][jj] = min (g[ii][jj], g[ii][a] + g[a][b] + g[b][jj]);
                g[ii][jj] = min (g[ii][jj], g[ii][b] + g[b][a] + g[a][jj]);
            }
		for (long long int ii = 0; ii < n; ii++)
            for (long long int jj = 0; jj < n; jj++)
                ans += g[ii][jj];
		printf("%lld ", ans / 2);
    }
	printf("\n");
}


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		min_right = -1
		max_left = 0
		for _ in range(n):
			left, right = list(map(int, input().split()))
			if left > max_left:
				max_left = left
			if min_right > right or min_right == -1:
				min_right = right
		if(min_right < max_left):
			print(max_left - min_right)
		else:
			print(0)

main()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		visited = set()
		arr = list(map(int, input().split()))
		rv = []
		prevmax = 0
		next_min = 1
		exit_early = False
		for i in range(len(arr)):
			if(arr[i] <= 0):
				print(-1)
				exit_early = True
				break
			if(arr[i] > prevmax):
				rv.append(arr[i])
				visited.add(arr[i])
				prevmax = arr[i]
			elif next_min not in visited:
				if(next_min >= prevmax):
					print(-1)
					exit_early = True
					break
				rv.append(next_min)
				visited.add(next_min)
				next_min += 1
			else:
				while(next_min in visited):
					next_min += 1
					if next_min >= prevmax:
						print(-1)
						exit_early = True
						break
				if exit_early:
					break
				rv.append(next_min)
				visited.add(next_min)
				next_min += 1

		if exit_early:
			continue

		for i in range(len(rv)):
			print(rv[i], end=' ')
		print()

main()


t=int(input())
while t>0:
	n=int(input())
	a=[int(x) for x in input().split()]
	b=[0 for i in a]
	flag=False;
	for i in range(1,n):
		if a[i]<a[i-1]:
			flag=True;
	if not flag:
		for i in range(n):
			b[a[i]-1]=1
		rem=[]
		for i in range(n):
			if b[i]==0:
				rem.append(i+1)
		st=0;
		b[0]=a[0]
		for i in range(1,n):
			if a[i]==a[i-1]:
				if rem[st]<=a[i]:
					b[i]=rem[st]
					st-=-1
				else:
					flag=True
			else:
				b[i]=a[i]
		if flag:
			print(-1)
		else:
			for i in b:
				print("{}".format(i),sep=' ',end=' ')
			print()
	else:
		print(-1)
	t-=1


t = int(input())
for _ in range(t):
    n = int(input())
    q = [int(x) for x in input().split()]
    pos = [True] * (n+1)
    p = [0] * n
    b = [0]
    flag = True
    for i in range(1, n):
        if q[i] > q[i-1]:
            b.append(i)

        if q[i] < q[i-1]:
            flag = False
            break

    if flag == False:
        print(-1)
        continue

    b.append(n)
    lenb = len(b)
    # print(b)
    k = 1
    for i in range(1, lenb):
        if b[i] - 1 < q[b[i-1]]:
            p[b[i-1]] = q[b[i-1]]
            pos[q[b[i-1]]] = False
            for j in range(b[i-1]+1, b[i]):
                while pos[k] == False:
                    k += 1
                p[j] = k
                pos[k] = False
        else:
#             print(i)
            flag = False
            break

    if flag == False:
        print(-1)
        continue
    else:
        print(*p, sep = ' ')


t = int(input())
for _ in range(t):
    n = int(input())
    q = [int(x) for x in input().split()]
    pos = [True] * (n+1)
    p = [0] * n
    b = [0]
    flag = True
    for i in range(1, n):
        if q[i] > q[i-1]:
            b.append(i)

        if q[i] < q[i-1]:
            flag = False
            break

    if flag == False:
        print(-1)
        continue

    b.append(n)
    lenb = len(b)
#     print(b)
    k = 1
    for i in range(1, lenb):
        if b[i] - 1 <= q[b[i-1]]:
            p[b[i-1]] = q[b[i-1]]
            pos[q[b[i-1]]] = False
            for j in range(b[i-1]+1, b[i]):
                while pos[k] == False:
                    k += 1
                p[j] = k
                pos[k] = False
        else:
#             print(i)
            flag = False
            break

    if flag == False:
        print(-1)
        continue
    else:
        print(*p, sep = ' ')


for _ in range(int(input())):
    n = int(input())
    pmax = list(map(int, input().split()))
    rem = set([i+1 for i in range(n)])
    upper = []
    ind = []
    p = [-1 for i in range(n)]; p[0] = pmax[0]; rem.remove(pmax[0])
    for i in range(1,n):
        if pmax[i] == pmax[i-1]:
            upper.append(pmax[i])
            ind.append(i)
        else:
            p[i] = pmax[i]
            rem.remove(pmax[i])

    fail = False
    rem = list(rem)
    rem.sort(reverse = True)
    for k in range(len(upper)):
        a = rem.pop()
        i = ind[k]; u = upper[k]
        if a >= u:
            fail = True
            break
        else:
            p[i] = a
    if fail:
        print(-1)
    else:
        print(' '.join([str(x) for x in p]))


for _ in range(int(input())):
    n = int(input())
    pmax = list(map(int, input().split()))
    rem = set([i+1 for i in range(n)])
    upper = []
    p = [-1 for i in range(n)]; p[0] = pmax[0]; rem.remove(pmax[0])
    for i in range(1,n):
        if pmax[i] == pmax[i-1]:
            upper.append((i,pmax[i]))
        else:
            p[i] = pmax[i]
            rem.remove(pmax[i])

    fail = False
    for (i, u) in upper:
        a = min(rem); rem.remove(a)
        if a >= u:
            fail = True
            break
        else:
            p[i] = a

    if fail:
        print(-1)
    else:
        print(' '.join([str(x) for x in p]))


for _ in range(int(input())):
    n = int(input())
    pmax = list(map(int, input().split()))
    rem = set([i+1 for i in range(n)])
    upper = []
    ind = []
    p = [-1 for i in range(n)]; p[0] = pmax[0]; rem.remove(pmax[0])
    for i in range(1,n):
        if pmax[i] == pmax[i-1]:
            upper.append(pmax[i])
            ind.append(i)
        else:
            p[i] = pmax[i]
            rem.remove(pmax[i])

    fail = False
    for k in range(len(upper)):
        a = min(rem); rem.remove(a)
        i = ind[k]; u = upper[k]
        if a >= u:
            fail = True
            break
        else:
            p[i] = a
    if fail:
        print(-1)
    else:
        print(' '.join([str(x) for x in p]))


t=int(input())
for q in range(t):
	n=int(input())
	a=list(map(int,input().split()))
	y=[i for i in range(1,n+1)]
	p=[]
	s=set(a)
	for i in y:
		if i not in s:
			p.append(i)
	p=p[::-1]
	l=[a[0]]
	m=a[0]
	for i in range(1,n):
		x=p[-1]
		if a[i]==m and a[i]>=(x):
			l.append(x)
			p.pop()
		elif a[i]>m and a[i]<=n:
			l.append(a[i])
			m=a[i]
		else:
			l=[]
			break

	if l:
		print(*l)
	else:
		print(-1)


t=int(input())
for q in range(t):
	n=int(input())
	a=list(map(int,input().split()))
	p=[0]*n
	l=[a[0]]
	m=a[0]
	p[a[0]-1]=1
	for i in range(1,n):
		if i==1:
			x=p.index(0)
		if a[i]==m and a[i]>=(x+1):
			l.append(x+1)
			p[x]=1
			if i!=n-1:
				x=p.index(0)
		elif a[i]>m and a[i]<=n:
			l.append(a[i])
			p[a[i]-1]=1
			m=a[i]
		else:
			l=[]
			break

	if l:
		print(*l)
	else:
		print(-1)


t=int(input())
for q in range(t):
	n=int(input())
	a=list(map(int,input().split()))
	y=[i for i in range(1,n+1)]
	p=[]
	s=set(a)
	for i in y:
		if i not in s:
			p.append(i)
	p=p[::-1]
	l=[a[0]]
	m=a[0]
	for i in range(1,n):
		if p:
			x=p[-1]
		if a[i]==m and a[i]>=x:
			l.append(x)
			p.pop()
		elif a[i]>m and a[i]<=n:
			l.append(a[i])
			m=a[i]
		else:
			l=[]
			break

	if l:
		print(*l)
	else:
		print(-1)


t=int(input())
for q in range(t):
	n=int(input())
	a=list(map(int,input().split()))
	p=[0]*n
	l=[a[0]]
	m=a[0]
	p[a[0]-1]=1
	for i in range(1,n):
		x=p.index(0)
		if a[i]==m and a[i]>=(x+1):
			l.append(x+1)
			p[x]=1
		elif a[i]>m and a[i]<=n:
			l.append(a[i])
			p[a[i]-1]=1
			m=a[i]
		else:
			l=[]
			break

	if l:
		print(*l)
	else:
		print(-1)


import sys
input = sys.stdin.readline

Q = int(input())
Query = []
for _ in range(Q):
    N = int(input())
    A = list(map(int, input().split()))
    Query.append((N, A))

for N, A in Query:
    ok = True
    tmp = 0
    used = [False]*(N+1)
    now = 1
    ans = []
    for a in A:
        if tmp < a:
            ans.append(a)
            used[a] = True
            tmp = a
        else:
            while used[now]:
                now += 1
            if now > tmp:
                ok = False
                break
            ans.append(now)
            used[now] = True
    if not ok:
        print(-1)
    else:
        print(*ans)


import math
q=int(input())
#q=1
for _ in range(q):
    n=int(input())
    l=list(map(int,input().split()))
    l1=l.copy()
    maxx=max(l)
    a=[0]*(n+1)
    for i in range(n):
        a[l[i]]+=1
    flag=True
    for i in range(n+1):
        if a[i]>i:
            print(-1)
            flag=False
            break
    if flag:
        fill=[]
        for i in range(1,n+1):
            if a[i]==0:
                fill.append(i)
        i=n-1
        while(i>=0):
            if a[l[i]]>1:
                a[l[i]]-=1
                l[i]=fill[-1]
                fill.pop()
            i-=1
        for i in range(n):
            if l[i]>l1[i]:
                print(-1)
                flag=False
                break
        if flag:
            print(*l)


tc = int(input())
for i in range(tc):
    n = int(input())
    l = list(map(int, input().split()))
    res = [q+1 for q in range(n)]
    error = False
    for (a,b) in enumerate(l):
        if b<a+1:
            error = True
            break
        else:
            res[b-1],res[a] = res[a],res[b-1]
    if not error:
        print(" ".join(map(str, res)))
    else:
        print(-1)


tc = int(input())
for i in range(tc):
    n = int(input())
    l = list(map(int, input().split()))
    if all([b>=a+1 for (a,b) in enumerate(l)]):
        res = []
        unused = [q+1 for q in range(n)]
        curmax=0
        used = set()
        for (a,b) in enumerate(l):
            if b > curmax:
                res.append(str(b))
                curmax = b
                used.add(b)
            else:
                while True:
                    tmp = unused[0]
                    unused = unused[1:]
                    if tmp not in used:
                        res.append(str(tmp))
                        break
        print(" ".join(res))
    else:
        print(-1)


tc = int(input())
for i in range(tc):
    n = int(input())
    l = list(map(int, input().split()))
    if all([b>=a+1 for (a,b) in enumerate(l)]):
        res = []
        unused = [q+1 for q in range(n)]
        curmax=0
        for (a,b) in enumerate(l):
            if b > curmax:
                res.append(str(b))
                curmax = b
                unused.remove(b)
            else:
                res.append(str(unused[0]))
                unused = unused[1:]
        print(" ".join(res))
    else:
        print(-1)


for _ in range(int(input())):
    n = int(input())
    lst = list(map(int, input().split()))
    s = set([i for i in range(1,n+1)])
    main = []
    p = 0
    for i in range(n):
        if lst[i]!=p:
            main.append(lst[i])
        else:
            main.append(-1)
        p = lst[i]
        try:
            s.remove(lst[i])
        except:
            pass
    s = list(s)
    j = 0
    flag = True
    for i in range(n):
        if main[i]==-1:
            if s[j]<lst[i]:
                main[i] = s[j]
                j+=1
            else:
                flag = False
                break
    if not flag:
        print(-1)
    else:
        print(*main)


import sys
input = sys.stdin.readline

for _ in range(int(input())):
    N = int(input())
    L = list(map(int,input().split()))
    vst = [False] * N
    ans = 1
    for i in range(N):
        if L[i] < i+1:
            ans = 0

    if ans:
        z = []
        cnt = 0
        for i in range(N):
            if not vst[L[i]-1]:
                z.append(L[i])
                vst[L[i]-1] = True
            else:
                while vst[cnt]:
                    cnt += 1
                z.append(cnt+1)
                vst[cnt] = True
        print(*z)
    else:
        print(-1)


#B
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    ri = list(range(1,n+1))
    rv = list(range(1,n+1))
    q = list(map(int,input().split()))
    p = {1:q[0]}
    ri.remove(1)
    rv.remove(q[0])
    for j in range(1,n):
        if q[j] <= j:
            bad = True
            break
        if q[j] > q[j-1]:
            p[j+1] = q[j]
            ri.remove(j+1)
            rv.remove(q[j])
    if bad:
        print(-1)
    else:
        i = 0
        for h in ri:
            p[h] = rv[i]
            i += 1
        for k in range(1,n+1):
            print(p[k], end = ' ')
        print()


#B
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    ri = list(range(2,n+1))
    rv = list(range(1,n+1))
    q = list(map(int,input().split()))
    p = {1:q[0]}
    rv.remove(q[0])
    for j in range(1,n):
        if q[j] > q[j-1]:
            p[j+1] = q[j]
            ri.remove(j+1)
            rv.remove(q[j])
        elif q[j] <= j:
            bad = True
            break
    if bad:
        print(-1)
    else:
        for i in range(len(ri)):
            p[ri[i]] = rv[i]
        for k in range(1,n+1):
            print(p[k], end = ' ')
        print()


#B
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    rv = list(range(1,n+1)) #rest values
    q = list(map(int,input().split()))
    p = {1:q[0]}
    rv.remove(q[0])
    for j in range(1,n):
        if q[j] > q[j-1]:
            p[j+1] = q[j]
            rv.remove(q[j])
        elif q[j] <= j:
            bad = True
            break
    if bad:
        print(-1)
    else:
        j = 0
        for k in range(1,n+1):
            if k in p.keys():
                print(p[k], end = ' ')
            else:
                print(rv[j], end = ' ')
                j += 1
        print()


#B
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    q = list(map(int,input().split()))
    for j in range(1,n):
        if q[j] <= j:
            bad = True
            break
    if bad:
        print(-1)
    else:
        rv = list(range(1,n+1)) #rest values
        print(q[0], end = ' ')
        c = 0 # how many are waiting
        rv.remove(q[0])
        for j in range(1,n):
            if q[j] > q[j-1]:
                for k in range(c): #printing previous waiting
                    print(rv[k], end = ' ')
                del rv[0:c]
                rv.remove(q[j])
                print(q[j], end = ' ')
                c = 0
            else:
                c += 1 #increase the number of waiting
        for k in range(len(rv)):
            print(rv[k], end=' ')
        print()


#B
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    q = list(map(int,input().split()))
    for j in range(1,n):
        if q[j] <= j:
            bad = True
            break
    if bad:
        print(-1)
    else:
        rv = list(range(1,n+1)) #rest values
        print(q[0], end = ' ')
        c = 0 # how many are waiting
        rv.remove(q[0])
        for j in range(1,n):
            if q[j] > q[j-1]:
                for k in range(c): #printing previous waiting
                    print(rv[0], end = ' ')
                    rv.remove(rv[0])
                rv.remove(q[j])
                print(q[j], end = ' ')
                c = 0
            else:
                c += 1 #increase the number of waiting
        for k in range(len(rv)):
            print(rv[k], end=' ')
        print()


#B
t = int(input())
for test in range(t):
    bad = False
    res = -1
    n = int(input())
    ri = list(range(1,n+1))
    rv = list(range(1,n+1))
    q = list(map(int,input().split()))
    p = {1:q[0]}
    ri.remove(1)
    rv.remove(q[0])
    for j in range(1,n):
        if q[j] <= j:
            bad = True
            break
        if q[j] > q[j-1]:
            p[j+1] = q[j]
            ri.remove(j+1)
            rv.remove(q[j])
    if bad:
        print(-1)
    else:
        i = 0
        for h in ri:
            p[h] = rv[i]
            i += 1
        for k in p.keys():
            print(p[k], end = ' ')
        print()


#B time
t = int(input())
for test in range(t):
    bad = False
    n = int(input())
    q = list(map(int,input().split()))
    qq = set(q)
    for j in range(1,n):
        if q[j] <= j:
            bad = True
            break
    if bad:
        print(-1)
    else:
        rv = []
        for j in range(1,n+1):
            if not j in qq:
                rv.append(j)
        print(q[0], end = ' ')
        c = 0 # how many are waiting
        for j in range(1,n):
            if q[j] > q[j-1]:
                for k in range(c): #printing previous waiting
                    print(rv[k], end = ' ')
                del rv[0:c]
                print(q[j], end = ' ')
                c = 0
            else:
                c += 1 #increase the number of waiting
        for k in range(len(rv)):
            print(rv[k], end=' ')
        print()


from collections import Counter

for _ in range(int(input())):
	n = int(input())
	q = [*map(int, input().split())]
	ans = [-1] * n
	seen = set()
	rem = sorted([*set([*range(1, n + 1)]) - set(q)], reverse=True)
	c = Counter(q)

	if any(c[i] > i for i in range(1, n + 1)):
		print(-1)
		continue

	for i in range(n):
		if q[i] not in seen:
			seen.add(q[i])
			ans[i] = q[i]

	for i in range(n):
		if ans[i] == -1:
			ans[i] = rem[-1]
			del rem[-1]

	print(*ans)


for _ in range(int(input())):
	n = int(input())
	q = [*map(int, input().split())]
	ans = [-1] * n
	seen = set()
	rem = sorted([*set([*range(1, n + 1)]) - set(q)], reverse=True)

	for i in range(n):
		if q[i] not in seen:
			seen.add(q[i])
			ans[i] = q[i]

	for i in range(n):
		if ans[i] == -1:
			ans[i] = rem[-1]
			del rem[-1]

	m = [ans[0]]

	for i in ans[1:]:
		m.append(max(m[-1], i))

	if m == q:
		print(*ans)
	else:
		print(-1)


import sys,math,itertools
from collections import Counter,deque,defaultdict
from bisect import bisect_left,bisect_right
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

for _ in range(inp()):
    n = inp()
    q = inpl()
    se = set()
    res = [-1] * n
    now = 1
    f = True
    for i in range(n):
        if i == 0:
            res[i] = q[i]
            if q[i] == 1:
                se.add(1)
            else:
                se.add(q[i])
            continue
        if q[i] < q[i-1]:
            print(-1)
            break
        elif q[i] > q[i-1]:
            se.add(q[i])
            res[i] = q[i]
        else:
            while True:
                if now > q[i]:
                    print(-1)
                    f = False
                    break
                if not now in se:
                    se.add(now)
                    res[i] = now
                    break
                se.add(now)
                now += 1
            if not f: break
    else:
        print(*res)


t = int(input())
for _ in range(t):
    n = int(input());
    v = list(map(int,input().split()))
    tmp = v
    tmp.sort()
    if tmp != v:
        print(-1)
        continue
    u = [0 for _ in range(n+1)]
    l = 1
    ans = []
    last = 0
    for i in v:
        if i == last:
            while u[l] == 1:
                l += 1
            if l > i:
                break
            else:
                ans.append(l)
                l += 1
        else:
            ans.append(i)
            u[i] = 1
        last = i
    if len(ans) != n:
        print(-1)
    else:
        for i in ans:
            print(i,end=" ")
        print()


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
INF = 10**20
I = lambda:list(map(int,input().split()))
from collections import defaultdict as dd
from math import gcd

def check(l, q):
	cur = [l[0]]
	m = l[0]
	for i in range(1, len(l)):
		m = max(l[i], m)
		cur.append(m)
	# print(l, q)
	return cur == q
t, = I()
while t:
	t -= 1
	n, = I()
	q = I()
	ans = [0]*n
	v = [0]*(n+10)
	for i in range(n-2, -1, -1):
		if q[i] != q[i+1]:
			ans[i+1] = q[i+1]
			v[ans[i+1]] = 1
	ans[0] = q[0]
	v[q[0]] = 1
	j = 1
	for i in range(n):
		if ans[i] != 0: continue
		while j < n and v[j] == 1:
			j += 1
		if j > n:
			break
		ans[i] = j
	if check(ans, q):
		print(*ans)
	else:
		print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
INF = 10**20
I = lambda:list(map(int,input().split()))
from collections import defaultdict as dd
from math import gcd

def check(l, q):
	cur = [l[0]]
	m = l[0]
	for i in range(1, len(l)):
		m = max(l[i], m)
		cur.append(m)
	# print(l, q)
	return cur == q
t, = I()
while t:
	t -= 1
	n, = I()
	q = I()
	ans = [0]*n
	v = [0]*(n+10)
	for i in range(n-2, -1, -1):
		if q[i] != q[i+1]:
			ans[i+1] = q[i+1]
			v[ans[i+1]] = 1
	ans[0] = q[0]
	v[q[0]] = 1
	j = 1
	for i in range(n):
		if ans[i] != 0: continue
		while j < n and v[j] == 1:
			j += 1

		if j > n:
			break
		ans[i] = j
		v[j] = 1
	if check(ans, q):
		print(*ans)
	else:
		print(-1)


t = int(input())
for j in range(t):
    n = int(input())
    q = list(map(int, input().split()))
    qset = set()
    truelist = list()
    for i in q:
        qset.add(i)

    for i in range(n, 0, -1):
        if i not in qset:
            truelist.append(i)

    ans = list()
    flag = False
    ans.append(q[0])
    k = -1
    for i in range(1, n):
        if q[i] > q[i-1]:
            ans.append(q[i])
        elif (truelist[k]) < q[i]:
            ans.append(truelist[k])
            k -= 1
        else:
            flag = True
            break
    if flag:
        print(-1)
    else:
        print(*ans)


t = int(input())
for j in range(t):
    n = int(input())
    q = list(map(int, input().split()))
    qset = set()
    truelist = list()
    for i in q:
        qset.add(i)

    for i in range(n, 0, -1):
        if i not in q:
            truelist.append(i)

    ans = list()
    flag = False
    ans.append(q[0])
    j = -1
    for i in range(1, n):
        if q[i] > q[i-1]:
            ans.append(q[i])
        elif (truelist[j]) < q[i]:
            ans.append(truelist[j])
            j -= 1
        else:
            flag = True
            break
    if flag:
        print(-1)
    else:
        print(*ans)


t = int(input())
for j in range(t):
    n = int(input())
    q = list(map(int, input().split()))
    qset = set()
    truelist = list()
    for i in q:
        qset.add(i)

    for i in reversed(range(1, n+1)):
        if i not in q:
            truelist.append(i)

    ans = list()
    flag = False
    ans.append(q[0])
    qset.remove(q[0])

    for i in range(1, n):
        if q[i] in qset:
            ans.append(q[i])
            qset.remove(q[i])
        elif (truelist[-1]) < q[i]:
                ans.append(truelist[-1])
                truelist.pop()
        else:
            flag = True
            break
    if flag:
        print(-1)
    else:
        print(*ans)


t = int(input())
for j in range(t):
    n = int(input())
    q = list(map(int, input().split()))
    qset = set()
    truelist = list()
    for i in q:
        qset.add(i)

    for i in reversed(range(1, n+1)):
        if i not in q:
            truelist.append(i)

    ans = list()
    flag = False
    ans.append(q[0])
    qset.remove(q[0])

    for i in range(1, n):
        if flag:
            break
        if q[i] in qset:
            ans.append(q[i])
            qset.remove(q[i])
        else:
            if len(truelist) == 0:
                flag = True
                break
            if (truelist[-1]) < q[i]:
                ans.append(truelist[-1])
                truelist.pop()
            else:
                flag = True
                break
    if flag:
        print(-1)
    else:
        print(*ans)


#------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#-------------------game starts now-----------------------------------------------------
for i in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    l1=[i for i in range(1,n+1)]
    l1=set(l1)
    #print(l1)
    a=[0]*n
    a[0]=l[0]
    l1.remove(l[0])
    f=0
    for j in range(1,n):
        if l[j]<=j:
            f=1
            print(-1)
            break
        if l[j]>l[j-1]:
            a[j]=l[j]
            l1.remove(l[j])

    t=0
    l1=list(l1)
    for j in range(1,n):
         if a[j]==0:
             a[j]=l1[t]
             t+=1
    if f==0:
        print(*a,sep=" ")


#------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#-------------------game starts now-----------------------------------------------------
for i in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    a=[0]*n
    a[0]=l[0]
    low=1
    f=0
    for j in range(1,n):
        if l[j]<=j:
            f=1
            print(-1)
            break
        if l[j]==l[j-1]:
            a[j]=low
            low+=1
        else:
            a[j]=l[j]
        while(low in set(l)):
            low+=1
    if f==0:
        print(*a,sep=" ")


for i in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    a=[0]*n
    a[0]=l[0]
    low=1
    f=0
    for j in range(1,n):
        if l[j]<=j:
            f=1
            print(-1)
            break
        if l[j]==l[j-1]:
            a[j]=low
            low+=1
        else:
            a[j]=l[j]
        while(low in set(l)):
            low+=1
    if f==0:
        print(*a,sep=" ")