#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define FFF 100005
int set[FFF];
int a[FFF];
int sw[505][505];
int cnt[505];
int vis[FFF];
int n,k,m;

int find(int x)
{
    if(x==set[x])
        return x;
    else return set[x]=find(set[x]);
}

void uion(int x,int y)
{
    int l1=find(x);
    int l2=find(y);
    if(l1==l2)
        return;
    else
        set[l2]=l1;
    return;
}

bool judge()
{
    for(int l=1;l<=k;l++)
    {
        int tmp=-1;
        for(int i=cnt[l-1]+1;i<=cnt[l];i++)
        {
            if(tmp==-1)
                tmp=find(i);
            else
            {
                if(tmp!=find(i))
                    return false;
            }
        }
    }
    return true;
}

void solve()
{
    for(int l=1;l<=k;l++)
    {
        for(int i=1;i<=k;i++)
        {
            if(i!=l)
            {
                for(int j=1;j<=k;j++)
                {
                    if(j!=l&&j!=i)
                    {
                        if(sw[i][l]+sw[l][j]<sw[i][j]&&sw[i][l]>=0&&sw[l][j]>=0)
                            sw[i][j]=sw[i][l]+sw[l][j];
                    }
                }
            }
        }
    }
    return;
}

void print()
{
    for(int i=1;i<=k;i++)
    {
        sw[i][i]=0;
        for(int j=1;j<=k;j++)
        {
            if(j==1)
                printf("%d",sw[i][j]);
            else
                printf(" %d",sw[i][j]);
        }
        cout<<endl;
    }
    return;
}

int main()
{
    int i,j,now;
    scanf("%d%d%d",&n,&m,&k);
    for(i=1,now=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        for( j=0;j<x;j++)
        {
            a[j+now]=i;
            set[j+now]=j+now;
        }
        now+=x;
        cnt[i]=now-1;
    }
    memset(sw,-1,sizeof(sw));
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(a[x]==a[y])
        {
            if(z==0)
                uion(x,y);
        }
        else
        {
            if(sw[a[x]][a[y]]==-1)
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
            else if(z<sw[a[x]][a[y]])
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
        }
    }
    if(!judge())
        printf("No\n");
    else
    {
    	printf("Yes\n");
        solve();
        print();
    }
    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define FFF 1000005

int set[FFF];
int a[FFF];
int cnt[505];
int n,m,k;
int sw[505][505];

int find(int x)
{
    if(x==set[x])return x;
    else return set[x]=find(set[x]);
}

void uoin(int x,int y)
{
    int l1=find(x);
    int l2=find(y);
    if(l1==l2)
        return;
    else
        set[l2]=l1;
}

bool judge()
{
    for(int i=1;i<=k;i++)
    {
        int tmp=-1;
        for(int j=cnt[i-1]+1;j<=cnt[i];j++)
        {
            if(tmp==-1)
                tmp=find(j);
            else if(tmp!=find(i))
                return false;
        }
    }
    return true;
}

void solve()
{
    int i,j,l;
    for(l=1;l<=k;l++)
    {
        for(i=1;i<=k;i++)
        {
            if(i!=l)
            {
                for(j=1;j<=k;j++)
                {
                    if(j!=l&&j!=i)
                    {
                        if((sw[i][j]==-1||sw[i][j]>sw[i][l]+sw[l][j])&&sw[i][l]>=0&&sw[l][j]>=0)
                            sw[i][j]=sw[i][l]+sw[l][j];
                    }
                }
            }
        }
    }
    return;
}
void print()
{
    for(int i=1;i<=k;i++)
    {
        sw[i][i]=0;
        for(int j=1;j<=k;j++)
        {
            if(j==1)
                printf("%d",sw[i][j]);
            else
                printf(" %d",sw[i][j]);
        }
        cout<<endl;
    }
    return ;
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1,now=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        for(int j=0;j<x;j++)
        {
            a[now+j]=i;
            set[now+j]=now+j;
        }
        cnt[i]=now+x-1;
        now+=x;
    }
    memset(sw,-1,sizeof(sw));
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(z==0)
        {
            uoin(x,y);
        }
        if(a[x]!=a[y])
        {
            if(sw[a[x]][a[y]]==-1)
                sw[a[y]][a[x]]=sw[a[x]][a[y]]=z;
            else if(z<sw[a[x]][a[y]])
                sw[a[y]][a[x]]=sw[a[x]][a[y]]=z;
        }
        // cout<<a[x]<<' '<<a[y]<<' '<<sw[a[x]][a[y]]<<endl;
    }
    if(!judge())
        printf("No\n");
    else
    {
        printf("Yes\n");
        solve();
        print();
    }
    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define FFF 100005
int set[FFF];
int a[FFF];
int sw[505][505];
int cnt[505];
int vis[FFF];
int n,k,m;

int find(int x)
{
    if(x==set[x])
        return x;
    else return set[x]=find(set[x]);
}

void uion(int x,int y)
{
    int l1=find(x);
    int l2=find(y);
    if(l1==l2)
        return;
    else
        set[l2]=l1;
    return;
}

bool judge()
{
    for(int l=1;l<=k;l++)
    {
        int tmp=-1;
        for(int i=cnt[l-1]+1;i<=cnt[l];i++)
        {
            if(tmp==-1)
                tmp=find(i);
            else
            {
                if(tmp!=find(i))
                    return false;
            }
        }
    }
    return true;
}

void solve()
{
    for(int l=1;l<=k;l++)
    {
        for(int i=1;i<=k;i++)
        {
            if(i!=l)
            {
                for(int j=1;j<=k;j++)
                {
                    if(j!=l&&j!=i)
                    {
                        if((sw[i][l]+sw[l][j]<sw[i][j]||sw[i][j]<0)&&sw[i][l]>=0&&sw[l][j]>=0)
                            sw[i][j]=sw[i][l]+sw[l][j];
                    }
                }
            }
        }
    }
    return;
}

void print()
{
    for(int i=1;i<=k;i++)
    {
        sw[i][i]=0;
        for(int j=1;j<=k;j++)
        {
            if(j==1)
                printf("%d",sw[i][j]);
            else
                printf(" %d",sw[i][j]);
        }
        cout<<endl;
    }
    return;
}

int main()
{
    int i,j,now;
    scanf("%d%d%d",&n,&m,&k);
    for(i=1,now=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        for( j=0;j<x;j++)
        {
            a[j+now]=i;
            set[j+now]=j+now;
        }
        now+=x;
        cnt[i]=now-1;
    }
    memset(sw,-1,sizeof(sw));
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(a[x]==a[y])
        {
            if(z==0)
                uion(x,y);
        }
        else
        {
        	if(z==0)
        		uion(x,y);
            if(sw[a[x]][a[y]]==-1)
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
            else if(z<sw[a[x]][a[y]])
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
        }
    }
    if(!judge())
        printf("No\n");
    else
    {
    	printf("Yes\n");
        solve();
        print();
    }
    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define FFF 1000005

int set[FFF];
int a[FFF];
int cnt[505];
int n,m,k;
int sw[505][505];

int find(int x)
{
    if(x==set[x])return x;
    else return set[x]=find(set[x]);
}

void uoin(int x,int y)
{
    int l1=find(x);
    int l2=find(y);
    if(l1==l2)
        return;
    else
        set[l2]=l1;
}

bool judge()
{
    for(int i=1;i<=k;i++)
    {
        int tmp=-1;
        for(int j=cnt[i-1]+1;j<=cnt[i];j++)
        {
            if(tmp==-1)
                tmp=find(j);
            else if(tmp!=find(j))
                return false;
        }
    }
    return true;
}

void solve()
{
    int i,j,l;
    for(l=1;l<=k;l++)
    {
        for(i=1;i<=k;i++)
        {
            if(i!=l)
            {
                for(j=1;j<=k;j++)
                {
                    if(j!=l&&j!=i)
                    {
                        if((sw[i][j]==-1||sw[i][j]>sw[i][l]+sw[l][j])&&sw[i][l]>=0&&sw[l][j]>=0)
                            sw[i][j]=sw[i][l]+sw[l][j];
                    }
                }
            }
        }
    }
    return;
}
void print()
{
    for(int i=1;i<=k;i++)
    {
        sw[i][i]=0;
        for(int j=1;j<=k;j++)
        {
            if(j==1)
                printf("%d",sw[i][j]);
            else
                printf(" %d",sw[i][j]);
        }
        cout<<endl;
    }
    return ;
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1,now=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        for(int j=0;j<x;j++)
        {
            a[now+j]=i;
            set[now+j]=now+j;
        }
        cnt[i]=now+x-1;
        now+=x;
    }
    memset(sw,-1,sizeof(sw));
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(z==0)
        {
            uoin(x,y);
        }
        if(a[x]!=a[y])
        {
            if(sw[a[x]][a[y]]==-1)
                sw[a[y]][a[x]]=sw[a[x]][a[y]]=z;
            else if(z<sw[a[x]][a[y]])
                sw[a[y]][a[x]]=sw[a[x]][a[y]]=z;
        }
        // cout<<a[x]<<' '<<a[y]<<' '<<sw[a[x]][a[y]]<<endl;
    }
    if(!judge())
        printf("No\n");
    else
    {
        printf("Yes\n");
        solve();
        print();
    }
    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define FFF 100005
int set[FFF];
int a[FFF];
int sw[505][505];
int cnt[505];
int vis[FFF];
int n,k,m;

int find(int x)
{
    if(x==set[x])
        return x;
    else return set[x]=find(set[x]);
}

void uion(int x,int y)
{
    int l1=find(x);
    int l2=find(y);
    if(l1==l2)
        return;
    else
        set[l2]=l1;
    return;
}

bool judge()
{
    for(int l=1;l<=k;l++)
    {
        int tmp=-1;
        for(int i=cnt[l-1]+1;i<=cnt[l];i++)
        {
            if(tmp==-1)
                tmp=find(i);
            else
            {
                if(tmp!=find(i))
                    return false;
            }
        }
    }
    return true;
}

void solve()
{
    for(int l=1;l<=k;l++)
    {
        for(int i=1;i<=k;i++)
        {
            if(i!=l)
            {
                for(int j=1;j<=k;j++)
                {
                    if(j!=l&&j!=i)
                    {
                        if((sw[i][l]+sw[l][j]<sw[i][j]||sw[i][j]<0)&&sw[i][l]>=0&&sw[l][j]>=0)
                            sw[i][j]=sw[i][l]+sw[l][j];
                    }
                }
            }
        }
    }
    return;
}

void print()
{
    for(int i=1;i<=k;i++)
    {
        sw[i][i]=0;
        for(int j=1;j<=k;j++)
        {
            if(j==1)
                printf("%d",sw[i][j]);
            else
                printf(" %d",sw[i][j]);
        }
        cout<<endl;
    }
    return;
}

int main()
{
    int i,j,now;
    scanf("%d%d%d",&n,&m,&k);
    for(i=1,now=1;i<=k;i++)
    {
        int x;
        scanf("%d",&x);
        for( j=0;j<x;j++)
        {
            a[j+now]=i;
            set[j+now]=j+now;
        }
        now+=x;
        cnt[i]=now-1;
    }
    memset(sw,-1,sizeof(sw));
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(a[x]==a[y])
        {
            if(z==0)
                uion(x,y);
        }
        else
        {
            if(sw[a[x]][a[y]]==-1)
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
            else if(z<sw[a[x]][a[y]])
                sw[a[x]][a[y]]=sw[a[y]][a[x]]=z;
        }
    }
    if(!judge())
        printf("No\n");
    else
    {
    	printf("Yes\n");
        solve();
        print();
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, cnt, t[N], d[K][K], f[N];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    cnt = n;
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++) f[i] = i;

    int p = 1, c, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c);
        for (int j = 0; j < c; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        if (t[u] == t[v] && x == 0) {
            int ui = getfar(u);
            int vi = getfar(v);
            if (ui != vi) {
                cnt--;
                f[vi] = ui;
            }
        }

        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }
}

void solve () {
    for (int i = 1; i <= k; i++) d[i][i] = 0;

    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++) {
            for (int x = 1; x <= k; x++) {
                d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
            }
        }
    }
}

int main () {
    init ();
    if (cnt == k) {
        solve ();
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e3;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, cnt, t[N], d[K][K], f[N];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    cnt = n;
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++) f[i] = i;

    int p = 1, c, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c);
        for (int j = 0; j < c; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        if (t[u] == t[v]) {
            int ui = getfar(u);
            int vi = getfar(v);
            if (ui != vi) {
                cnt--;
                f[vi] = ui;
            }
        }

        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }

    for (int i = 1; i <= k; i++) d[i][i] = 0;
}

void solve () {

    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++) {
            for (int x = 1; x <= k; x++) {
                d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
            }
        }
    }
}

int main () {
    init ();
    solve ();
    if (cnt == k) {
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e3;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, cnt, t[N], d[K][K], f[N];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    cnt = n;
    memset(d, INF, sizeof(d));
    for (int i = 0; i <= n; i++) f[i] = i;

    int p = 1, c, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c);
        for (int j = 0; j < c; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        if (t[u] == t[v]) {
            int ui = getfar(u);
            int vi = getfar(v);
            if (ui != vi) {
                cnt--;
                f[vi] = ui;
            }
        }

        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }

    for (int i = 1; i <= k; i++) d[i][i] = 0;
}

int main () {
    init ();
    if (cnt == k) {
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int N = 1e5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, t[N], d[K][K];

void init () {
    scanf("%d%d%d", &n, &m, &k);
    memset(d, INF, sizeof(d));

    int p = 1, c, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c);
        for (int j = 0; j < c; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);
        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }

    for (int i = 1; i <= k; i++) d[i][i] = 0;
}

int main () {
    init ();
    if (m) {
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, cnt, c[N], t[N], d[K][K], f[N];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    cnt = n;
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++) f[i] = i;

    int p = 1, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c[i]);
        for (int j = 0; j < c[i]; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        if (x == 0) {
            int ui = getfar(u);
            int vi = getfar(v);
            if (ui != vi) f[vi] = ui;
        }

        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }
}

bool judge () {

    int p = 1;
    for (int i = 1; i <= k; i++) {
        int u = getfar(p);
        for (int j = 0; j < c[i]; j++) {
            int v = getfar(p);
            if (v != u) return false;
            p++;
        }
    }
    return true;
}

void solve () {
    for (int i = 1; i <= k; i++) d[i][i] = 0;

    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++) {
            for (int x = 1; x <= k; x++) {
                d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
            }
        }
    }
}

int main () {
    init ();
    if (judge()) {
        solve ();
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, c[N], t[N], d[K][K], f[N];

int getfar(int x) {
	return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
	scanf("%d%d%d", &n, &m, &k);
	memset(d, -1, sizeof(d));
	for (int i = 1; i <= n; i++) f[i] = i;

	int p = 1, u, v, x;
	for (int i = 1; i <= k; i++) {
		scanf("%d", &c[i]);
		for (int j = 0; j < c[i]; j++) t[p++] = i;
	}

	for (int i = 0; i < m; i++) {
		scanf("%d%d%d", &u, &v, &x);

		int ui, vi;
		if (x == 0) {
			ui = getfar(u);
			vi = getfar(v);
			if (ui != vi) f[vi] = ui;
		}

		ui = t[u]; vi = t[v];

		if (d[ui][vi] == -1)
			d[ui][vi] = d[vi][ui] = x;
		else
			d[ui][vi] = d[vi][ui] = min(d[vi][ui], x);
	}
}

bool judge () {

	int p = 1;
	for (int i = 1; i <= k; i++) {
		int u = getfar(p);
		for (int j = 0; j < c[i]; j++) {
			int v = getfar(p);
			if (v != u) return false;
			p++;
		}
	}
	return true;
}

void solve () {
	for (int i = 1; i <= k; i++) d[i][i] = 0;

	for (int i = 1; i <= k; i++) {
		for (int j = 1; j <= k; j++) {
			for (int x = 1; x <= k; x++) {
				d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
			}
		}
	}
}

int main () {
	init ();
	if (judge()) {
		solve ();
		printf("Yes\n");
		for (int i = 1; i <= k; i++) {
			for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
			printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
		}
	} else
		printf("No\n");
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, cnt, t[N], d[K][K], f[N];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    cnt = n;
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++) f[i] = i;

    int p = 1, c, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c);
        for (int j = 0; j < c; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        if (t[u] == t[v]) {
            int ui = getfar(u);
            int vi = getfar(v);
            if (ui != vi) {
                cnt--;
                f[vi] = ui;
            }
        }

        u = t[u]; v = t[v];
        d[u][v] = d[v][u] = min(d[v][u], x);
    }

    for (int i = 1; i <= k; i++) d[i][i] = 0;
}

void solve () {

    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++) {
            for (int x = 1; x <= k; x++) {
                d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
            }
        }
    }
}

int main () {
    init ();
    solve ();
    if (cnt == k) {
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, t[N], f[N];
int c[K], d[K][K];

int getfar(int x) {
	return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
	scanf("%d%d%d", &n, &m, &k);
	memset(d, INF, sizeof(d));
	for (int i = 1; i <= n; i++) f[i] = i;

	int p = 1, u, v, x;
	for (int i = 1; i <= k; i++) {
		scanf("%d", &c[i]);
		for (int j = 0; j < c[i]; j++) t[p++] = i;
	}

	for (int i = 0; i < m; i++) {
		scanf("%d%d%d", &u, &v, &x);

		int ui, vi;
		if (x == 0) {
			ui = getfar(u);
			vi = getfar(v);
			if (ui != vi) f[vi] = ui;
		}

		ui = t[u]; vi = t[v];

		d[ui][vi] = d[vi][ui] = min(min(d[vi][ui], d[ui][vi]), x);
	}
}

bool judge () {

	int p = 1;
	for (int i = 1; i <= k; i++) {
		int u = getfar(p);
		for (int j = 0; j < c[i]; j++) {
			int v = getfar(p);
			if (v != u) return false;
			p++;
		}
	}
	return true;
}

void solve () {
	for (int i = 1; i <= k; i++) d[i][i] = 0;

	for (int i = 1; i <= k; i++) {
		for (int j = 1; j <= k; j++) {
			for (int x = 1; x <= k; x++) {
				d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
			}
		}
	}
}

int main () {
	init ();
	if (judge()) {
		solve ();
		printf("Yes\n");
		for (int i = 1; i <= k; i++) {
			for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
			printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
		}
	} else
		printf("No\n");
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int N = 1e5+5;
const int K = 505;
const int INF = 0x3f3f3f3f;

int n, m, k, t[N], f[N];
int c[K], d[K][K];

int getfar(int x) {
    return x == f[x] ? x : f[x] = getfar(f[x]);
}

void init () {
    scanf("%d%d%d", &n, &m, &k);
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= n; i++) f[i] = i;

    int p = 1, u, v, x;
    for (int i = 1; i <= k; i++) {
        scanf("%d", &c[i]);
        for (int j = 0; j < c[i]; j++) t[p++] = i;
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &x);

        int ui, vi;
        if (x == 0) {
            ui = getfar(u);
            vi = getfar(v);
            if (ui != vi) f[vi] = ui;
        }

        ui = t[u]; vi = t[v];
        d[ui][vi] = d[vi][ui] = min(d[ui][vi], x);
    }
}

bool judge () {

    int p = 1;
    for (int i = 1; i <= k; i++) {
        int u = getfar(p);
        for (int j = 0; j < c[i]; j++) {
            int v = getfar(p);
            if (v != u) return false;
            p++;
        }
    }
    return true;
}

void solve () {
    for (int i = 1; i <= k; i++) d[i][i] = 0;

    for (int x = 1; x <= k; x++) {
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
            }
        }
    }
}

int main () {
    init ();
    if (judge()) {
        solve ();
        printf("Yes\n");
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j < k; j++) printf("%d ", d[i][j] != INF ? d[i][j] : -1);
            printf("%d\n", d[i][k] != INF ? d[i][k] : -1);
        }
    } else
        printf("No\n");
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
ll GETi() { ll i;scanf("%lld",&i); return i;}
//-------------------------------------------------------

const int ufmax=1000001;
int ufpar[ufmax],ufrank[ufmax];
void UF_init(){int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; } }
int UF_find(int x) {    return (ufpar[x]==x)?(x):(ufpar[x] = UF_find(ufpar[x]));}
void UF_unite(int x,int y) {
    x = UF_find(x); y = UF_find(y);
    if(x==y) return;
    if(ufrank[x]<ufrank[y]) ufpar[x]=y;
    else {ufpar[y]=x; if(ufrank[x]==ufrank[y]) ufrank[x]++;}
}

int N,M,K;
int C[1000],S[1000],E[1000];
int T[100001];
vector<int> SE[100001];
ll mat[501][501];
int vis[100001];

void solve() {
    int f,i,j,k,l,x,y;

    cin>>N>>M>>K;
    l=0;
    FOR(i,K) {
        cin>>C[i];
        S[i]=l;
        FOR(j,C[i]) T[l++]=i;
        E[i]=l;
    }
    FOR(x,K) FOR(y,K) mat[x][y]=1LL<<50;
    FOR(x,K) mat[x][x]=0;
    UF_init();

    FOR(i,M) {
        cin>>x>>y>>j;
        if(j==0) UF_unite(x-1,y-1);
        if(T[x-1]!=T[y-1]) {
            mat[T[x-1]][T[y-1]] = min(mat[T[x-1]][T[y-1]],(ll)j);
            mat[T[y-1]][T[x-1]] = min(mat[T[y-1]][T[x-1]],(ll)j);
        }
    }

    FOR(i,K) {
        for(j=S[i];j<E[i];j++) if(UF_find(j)!=UF_find(S[i])) return _P("No\n");
    }

    FOR(j,K) FOR(x,K) FOR(y,K) mat[x][y]=min(mat[x][y],mat[x][j]+mat[j][y]);
    FOR(x,K) FOR(y,K) if(mat[x][y]==1LL<<50) mat[x][y]=-1;
    _P("Yes\n");
    FOR(x,K) {
        FOR(y,K) _P("%lld ",mat[x][y]);
        _P("\n");
    }

}

int main(int argc,char** argv){
    string s;
    if(argc==1) ios::sync_with_stdio(false);
    for(int i=1;i<argc;i++) s+=argv[i],s+='\n';
    for(int i=s.size()-1;i>=0;i--) ungetc(s[i],stdin);
    solve();
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
ll GETi() { ll i;scanf("%lld",&i); return i;}
//-------------------------------------------------------

int N,M,K;
int C[1000],S[1000],E[1000];
int T[100001];
vector<int> SE[100001];
ll mat[501][501];
int vis[100001];

void dfs(int cur) {
    ITR(it,SE[cur]) if(vis[*it]==0) vis[*it]=1,dfs(*it);
}

void solve() {
    int f,i,j,k,l,x,y;

    cin>>N>>M>>K;
    l=0;
    FOR(i,K) {
        cin>>C[i];
        S[i]=l;
        FOR(j,C[i]) T[l++]=i;
        E[i]=l;
    }
    FOR(x,K) FOR(y,K) mat[x][y]=1LL<<50;
    FOR(x,K) mat[x][x]=0;

    FOR(i,M) {
        cin>>x>>y>>j;
        if(T[x-1]==T[y-1]) {
            if(j==0) SE[x-1].push_back(y-1);
        }
        else {
            mat[T[x-1]][T[y-1]] = min(mat[T[x-1]][T[y-1]],(ll)j);
            mat[T[y-1]][T[x-1]] = min(mat[T[y-1]][T[x-1]],(ll)j);
        }
    }

    FOR(i,K) {
        vis[S[i]]=1;
        dfs(S[i]);
        if(count(vis+S[i],vis+E[i],1)!=C[i]) return _P("No\n");
    }

    FOR(j,K) FOR(x,K) FOR(y,K) mat[x][y]=min(mat[x][y],mat[x][j]+mat[j][y]);
    FOR(x,K) FOR(y,K) if(mat[x][y]==1LL<<50) mat[x][y]=-1;
    _P("Yes\n");
    FOR(x,K) {
        FOR(y,K) _P("%lld ",mat[x][y]);
        _P("\n");
    }

}

int main(int argc,char** argv){
    string s;
    if(argc==1) ios::sync_with_stdio(false);
    for(int i=1;i<argc;i++) s+=argv[i],s+='\n';
    for(int i=s.size()-1;i>=0;i--) ungetc(s[i],stdin);
    solve();
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
ll GETi() { ll i;scanf("%lld",&i); return i;}
//-------------------------------------------------------

int N,M,K;
int C[1000],S[1000],E[1000];
int T[100001];
vector<int> SE[100001];
ll mat[501][501];
int vis[100001];

void dfs(int cur) {
    ITR(it,SE[cur]) if(vis[*it]==0) vis[*it]=1,dfs(*it);
}

void solve() {
    int f,i,j,k,l,x,y;

    cin>>N>>M>>K;
    l=0;
    FOR(i,K) {
        cin>>C[i];
        S[i]=l;
        FOR(j,C[i]) T[l++]=i;
        E[i]=l;
    }
    FOR(x,K) FOR(y,K) mat[x][y]=1LL<<50;
    FOR(x,K) mat[x][x]=0;

    FOR(i,M) {
        cin>>x>>y>>j;
        if(T[x-1]==T[y-1]) {
            if(j==0) SE[x-1].push_back(y-1);
            if(j==0) SE[y-1].push_back(x-1);
        }
        else {
            mat[T[x-1]][T[y-1]] = min(mat[T[x-1]][T[y-1]],(ll)j);
            mat[T[y-1]][T[x-1]] = min(mat[T[y-1]][T[x-1]],(ll)j);
        }
    }

    FOR(i,K) {
        vis[S[i]]=1;
        dfs(S[i]);
        if(count(vis+S[i],vis+E[i],1)!=C[i]) return _P("No\n");
    }

    FOR(j,K) FOR(x,K) FOR(y,K) mat[x][y]=min(mat[x][y],mat[x][j]+mat[j][y]);
    FOR(x,K) FOR(y,K) if(mat[x][y]==1LL<<50) mat[x][y]=-1;
    _P("Yes\n");
    FOR(x,K) {
        FOR(y,K) _P("%lld ",mat[x][y]);
        _P("\n");
    }

}

int main(int argc,char** argv){
    string s;
    if(argc==1) ios::sync_with_stdio(false);
    for(int i=1;i<argc;i++) s+=argv[i],s+='\n';
    for(int i=s.size()-1;i>=0;i--) ungetc(s[i],stdin);
    solve();
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
ll GETi() { ll i;scanf("%lld",&i); return i;}
//-------------------------------------------------------

const int ufmax=1000001;
int ufpar[ufmax],ufrank[ufmax];
void UF_init(){int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; } }
int UF_find(int x) {    return (ufpar[x]==x)?(x):(ufpar[x] = UF_find(ufpar[x]));}
void UF_unite(int x,int y) {
    x = UF_find(x); y = UF_find(y);
    if(x==y) return;
    if(ufrank[x]<ufrank[y]) ufpar[x]=y;
    else {ufpar[y]=x; if(ufrank[x]==ufrank[y]) ufrank[x]++;}
}

int N,M,K;
int C[1000],S[1000],E[1000];
int T[100001];
vector<int> SE[100001];
ll mat[501][501];
int vis[100001];

void solve() {
    int f,i,j,k,l,x,y;

    cin>>N>>M>>K;
    l=0;
    FOR(i,K) {
        cin>>C[i];
        S[i]=l;
        FOR(j,C[i]) T[l++]=i;
        E[i]=l;
    }
    FOR(x,K) FOR(y,K) mat[x][y]=1LL<<50;
    FOR(x,K) mat[x][x]=0;

    FOR(i,M) {
        cin>>x>>y>>j;
        if(j==0) UF_unite(x-1,y-1);
        if(T[x-1]!=T[y-1]) {
            mat[T[x-1]][T[y-1]] = min(mat[T[x-1]][T[y-1]],(ll)j);
            mat[T[y-1]][T[x-1]] = min(mat[T[y-1]][T[x-1]],(ll)j);
        }
    }

    FOR(i,K) {
        for(j=S[i];j<E[i];j++) if(UF_find(j)!=UF_find(S[i])) return _P("No\n");
    }

    FOR(j,K) FOR(x,K) FOR(y,K) mat[x][y]=min(mat[x][y],mat[x][j]+mat[j][y]);
    FOR(x,K) FOR(y,K) if(mat[x][y]==1LL<<50) mat[x][y]=-1;
    _P("Yes\n");
    FOR(x,K) {
        FOR(y,K) _P("%lld ",mat[x][y]);
        _P("\n");
    }

}

int main(int argc,char** argv){
    string s;
    if(argc==1) ios::sync_with_stdio(false);
    for(int i=1;i<argc;i++) s+=argv[i],s+='\n';
    for(int i=s.size()-1;i>=0;i--) ungetc(s[i],stdin);
    solve();
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
ll GETi() { ll i;scanf("%lld",&i); return i;}
//-------------------------------------------------------

int N,M,K;
int C[1000],S[1000],E[1000];
int T[100001];
vector<int> SE[100001];
ll mat[501][501];
int vis[100001];

void dfs(int cur) {
    ITR(it,SE[cur]) if(vis[*it]==0) vis[*it]=1,dfs(*it);
}

void solve() {
    int f,i,j,k,l,x,y;

    cin>>N>>M>>K;
    l=0;
    FOR(i,K) {
        cin>>C[i];
        S[i]=l;
        FOR(j,C[i]) T[l++]=i;
        E[i]=l;
    }
    FOR(x,K) FOR(y,K) mat[x][y]=1LL<<50;
    FOR(x,K) mat[x][x]=0;

    FOR(i,M) {
        cin>>x>>y>>j;
        if(T[x-1]==T[y-1]) {
            if(j==0) SE[x-1].push_back(y-1);
        }
        else {
            mat[T[x-1]][T[y-1]] = min(mat[T[x-1]][T[y-1]],(ll)j);
            mat[T[y-1]][T[x-1]] = min(mat[T[y-1]][T[x-1]],(ll)j);
        }
    }

    FOR(i,K) {
        if(T[i]==1) continue;
        dfs(S[i]);
        if(count(vis+S[i],vis+E[i],1)!=C[i]) return _P("No\n");
    }

    FOR(j,K) FOR(x,K) FOR(y,K) mat[x][y]=min(mat[x][y],mat[x][j]+mat[j][y]);
    FOR(x,K) FOR(y,K) if(mat[x][y]==1LL<<50) mat[x][y]=-1;
    _P("Yes\n");
    FOR(x,K) {
        FOR(y,K) _P("%lld ",mat[x][y]);
        _P("\n");
    }

}

int main(int argc,char** argv){
    string s;
    if(argc==1) ios::sync_with_stdio(false);
    for(int i=1;i<argc;i++) s+=argv[i],s+='\n';
    for(int i=s.size()-1;i>=0;i--) ungetc(s[i],stdin);
    solve();
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
int find(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) vel[i] = 1, cnt ++;
	}
	return 0;
}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	/*repf(i, 1, k) cout << vel[i] << ' ' ;
	cout << endl << cnt << endl;*/
	if(cnt == k) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	//cout << dis[0][0] << endl << oo << endl;
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, k) cin >> a[i];
	find();
	/*cout << "############" << endl;
	repf(i, 1, n) cout << chg[i] << ' ' ;
	cout << endl;*/
	repf(i, 1,m) {
		cin >> u >> v >> x;
		u = chg[u];
		v = chg[v];
		if(u == v && x == 0) cnt ++;
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
void bui(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) f[N]=i;
	}
}
inline int find(int x){	return f[x] == x ? x : f[x] = find( f[x] );}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	int sum = a[1], w = 1;
	int ans = 1, temp = find(1);
	repf(i, 1, n) {
		if(sum < i) sum += a[++w], temp = find(i);
		//cout << f[i] << ' ' << temp << endl;
		if(f[i] != temp) ans = 0;

	}
	if(ans) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, n) f[i] = i;
	repf(i, 1, k) cin >> a[i];
	bui();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(x == 0) {
			int c = find(u);
			int d = find(v);
			c > d? f[c] = d: f[d] = c;
		}
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, cnt, i, j;
class DJset {
	int *f;
	public : DJset(int n){
			f = new int[n];
			for(int i = 0; i <= n; i ++) f[i] = i;
	}
	void insert(int i, int j){
		int a = getFather(i);
		int b = getFather(j);
		a > b ? f[a] = b : f[b] = a;
	}
	int getFather(int i){	return f[i] == i ? i : getFather(f[i]);}

};
DJset *ds;
void build(){
	int N = 0;
	repf(i, 1, k) {
		int num = a[i];
		if(a[i] == 1) dis[i][i] = 0;
		while( num --){
			chg[++N] = i;
		}
	}
}
void floyd(){
	int a, b, c;
	repf(a, 1, k)	repf(b, 1, k)	repf(c, 1, k)
	dis[b][c] = min( dis[b][c], dis[b][a] + dis[a][c]);
	repf(a, 1, k) {
		repf(b, 1, k){
			if(b != 1) cout << ' ';
			if(dis[a][b] < 100000) cout << dis[a][b];
			else cout << -1 ;
		}
		cout << endl;
	}
}
int solve(){
	int u, v, x, N = 1;
	int ans = 1;

	repf(i, 1, m) {
		cin >> u >> v >> x;
		if(x == 0 ) 	ds->insert(u, v);
		dis[chg[u]][chg[v]] = dis[chg[v]][chg[u]] = min(dis[chg[v]][chg[u]], x);
	}
	repf(i, 1, k){
		int w = a[i];
		int temp = ds->getFather(N);
		while( w --)	{
			//cout << temp << ' ' << ds->getFather(N) << endl ;
			if(temp != ds->getFather(N++))  ans = 0;
		}
	}
	if(ans) cout << "Yes" << endl, floyd();
	else cout << "No" << endl;
	return 0;
}
int main(){
clr(dis, 0xf);
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	ds = new DJset(n);
	repf(i, 1, k) cin >> a[i];
	build();
	//repf(i, 1, n) cout << chg[i] << ' ' ; cout << endl;
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
void bui(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) f[N]=i;
	}
}
inline int find(int x){	return f[x] == x ? x : f[x] = find( f[x] );}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	int sum = a[1], w = 1;
	int ans = 1, temp = find(1);
	repf(i, 1, n) {
		if(sum < i) sum += a[++w], temp = find(i);
		//cout << f[i] << ' ' << temp << endl;
		if(f[i] != temp) ans = 0;

	}
	if(ans) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, n) f[i] = i;
	repf(i, 1, k) cin >> a[i];
	bui();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(x == 0) {
			int c = find(u);
			f[c] = find(v);
		}
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
void bui(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
	}
}
int find(int x){	return f[x]=f[x] == x ? x : find( f[x] );}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	int sum = a[1], w = 1;
	int ans = 1;
	repf(i, 1, n) {
		//cout << f[i] << ' ' << w << endl;
		if(f[i] != w) ans = 0;
		if(sum == i) sum += a[++w];
	}
	if(ans) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, n) f[i] = i;
	repf(i, 1, k) cin >> a[i];
	bui();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(x == 0) {
			f[find(u)] = chg[u];
			f[find(v)] = chg[v];
		}
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
int find(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) vel[i] = 1, cnt ++;
	}
	return 0;
}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	repf(i, 1, k)	{
		int x = dis[i][i];
		//cout << x << endl;
		if(x == 0) {
			if(vel[i] == 0) vel[i] = 1 , cnt ++;
		}
	}
	/*repf(i, 1, k) cout << vel[i] << ' ' ;
	cout << endl << cnt << endl;*/
	if(cnt == k) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	//cout << dis[0][0] << endl << oo << endl;
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, k) cin >> a[i];
	find();
	/*cout << "############" << endl;
	repf(i, 1, n) cout << chg[i] << ' ' ;
	cout << endl;*/
	repf(i, 1,m) {
		cin >> u >> v >> x;
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
int find(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) vel[i] = 1, cnt ++;
	}
	return 0;
}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	/*repf(i, 1, k) cout << vel[i] << ' ' ;
	cout << endl << cnt << endl;*/
	if(cnt == k) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	//cout << dis[0][0] << endl << oo << endl;
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, k) cin >> a[i];
	find();
	/*cout << "############" << endl;
	repf(i, 1, n) cout << chg[i] << ' ' ;
	cout << endl;*/
	repf(i, 1,m) {
		cin >> u >> v >> x;
		u = chg[u];
		v = chg[v];
		if(u == v && x == 0 && vel[u] == 0) vel[u] = 1, cnt ++;
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
void bui(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) f[N]=i;
	}
}
inline int find(int x){	return f[x] == x ? x : f[x] = find( f[x] );}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	int sum = a[1], w = 1;
	int ans = 1, temp = f[1];
	repf(i, 1, n) {
		//cout << f[i] << ' ' << w << endl;
		if(sum < i) sum += a[++w], temp = find(i);
		if(f[i] != temp) ans = 0;

	}
	if(ans) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, n) f[i] = i;
	repf(i, 1, k) cin >> a[i];
	bui();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(x == 0) {
			int c = f[find(u)];
			f[c] = f[find(v)];
		}
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], f[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
void bui(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) f[N]=i;
	}
}
int find(int x){	return f[x]=f[x] == x ? x : find( f[x] );}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	int sum = a[1], w = 1;
	int ans = 1;
	repf(i, 1, n) {
		//cout << f[i] << ' ' << w << endl;
		if(f[i] != w) ans = 0;
		if(sum == i) sum += a[++w];
	}
	if(ans) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, n) f[i] = i;
	repf(i, 1, k) cin >> a[i];
	bui();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(x == 0) {
			f[find(u)] = chg[u];
			f[find(v)] = chg[v];
		}
		u = chg[u];
		v = chg[v];
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define long long long
#define rep(i,n) for(i=0;i<n;i++)
#define repf(i,a,b) for(i=a;i<=b;i++)
#define repd(i,a,b) for(i=a;i>=b;i--)
#define clr(a,vel) memset(a,vel,sizeof(a))
#define pb PUSH_BACK
#define oo 0x7fffffff
int a[510],  chg[100010], vel[510], dis[600][600];
int n, m, k, N, cnt, i, j;
int find(){
	repf(i, 1,k){
		int w = a[i];
		while(w--) chg[++N] = i;
		if(a[i] == 1) vel[i] = 1, cnt ++;
	}
	return 0;
}
int floyd(int n){
	int k, i, j;
	repf(i, 1, n) dis[i][i] = 0;
	repf(k, 1, n)	repf(i, 1, n)	repf(j, 1, n)
		dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
	repf(i, 1, n){
		repf(j, 1, n) {
			if(j != 1) cout << ' ' ;
			if(dis[i][j] > 10000)	cout << -1 ;
			else cout << dis[i][j] ;
		}
		cout << endl;
	}
	return 0;
}
int solve(){
	if(cnt == k) cout << "Yes" << endl, floyd(k);
	else cout << "No" << endl;
	return 0;
}
int main(){
	int u, v, x;
	clr(dis, 0x7);
	clr(vel,0);
	N = 0, cnt = 0;
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	repf(i, 1, k) cin >> a[i];
	find();
	repf(i, 1,m) {
		cin >> u >> v >> x;
		if(u == v) continue;
		u = chg[u];
		v = chg[v];
		if(u == v && x == 0 && vel[u] == 0) vel[u] = 1, cnt ++;
		dis[u][v] = dis[v][u] = min(dis[u][v], x);
	}
	solve();
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = 1;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		if (c[to] == tp)
			res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j ? 0 : inf);
	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		else
			d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	bool ans = true;
	for (int i = 0; i < n; ++i)
	{
		if (used[i]) continue;
		int tp = c[i];
		int res = dfs(i, tp);
		if (res != cnt[tp])
			ans = false;
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = 1;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		if (c[to] == tp)
			res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j) ? 0 : inf;

	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			for (int z = 0; z < k; ++z)
				d[j][z] = min(d[j][z], d[j][i] + d[i][z]);
	bool ans = true;
	for (int i = 0; i < n; ++i)
	{
		if (used[i]) continue;
		int tp = c[i];
		int res = dfs(i, tp);
		if (res != cnt[tp])
			ans = false;
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");

	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = c[v] == tp;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j) ? 0 : inf;

	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			for (int z = 0; z < k; ++z)
				d[j][z] = min(d[j][z], d[j][i] + d[i][z]);
	bool ans = true;
	for (int i = 0, id = 0; i < k; ++i)
	{
		memset(used, 0, sizeof(used));
		int tp = i;
		int res = dfs(id, tp);
		if (res != cnt[tp])
			ans = false;
		id += cnt[i];
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");

	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = 1;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		if (c[to] == tp)
			res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j ? 0 : inf);
	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		else
			d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	bool ans = true;
	for (int i = 0; i < n; ++i)
	{
		if (used[i]) continue;
		int tp = c[i];
		int res = dfs(i, tp);
		if (res != cnt[tp])
			ans = false;
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = c[v] == tp;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j) ? 0 : inf;

	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (x == 0)
		{
			g[u].push_back(v);
			g[v].push_back(u);
		}
		d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			for (int z = 0; z < k; ++z)
				d[j][z] = min(d[j][z], d[j][i] + d[i][z]);
	bool ans = true;
	for (int i = 0, id = 0; i < k; ++i)
	{
		memset(used, 0, sizeof(used));
		int tp = i;
		int res = dfs(id, tp);
		if (res != cnt[tp])
			ans = false;
		id += cnt[i];
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");

	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = 1;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		if (c[to] == tp)
			res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = inf;

	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	bool ans = false;
	for (int i = 0; i < n; ++i)
	{
		if (used[i]) continue;
		int tp = c[i];
		int res = dfs(i, tp);
		if (res != cnt[tp])
			ans = true;
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");

	return 0;
}


#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;

const int MAXN = 100*1000 + 7;
const int inf = 1e9 + 7;

int n, m, k;
int c[MAXN], cnt[507];
int d[507][507];
vector<int> g[MAXN];

char used[MAXN];
int dfs(int v, int tp)
{
	if (used[v]) return 0;
	int res = 1;
	used[v] = true;
	for (size_t i = 0; i < g[v].size(); ++i)
	{
		int to = g[v][i];
		if (c[to] == tp)
			res += dfs(to, tp);
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	int id = 0;
	for (int i = 0; i < k; ++i)
	{
		int x; scanf("%d", &x);
		cnt[i] = x;
		for (int j = 0; j < x; ++j)
			c[j + id] = i;
		id += x;
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			d[i][j] = (i == j) ? 0 : inf;

	for (int i = 0; i < m; ++i)
	{
		int u, v, x; scanf("%d%d%d", &u, &v, &x);
		--u, --v;
		if (c[u] == c[v])
		{
			if (x == 0)
			{
				g[u].push_back(v);
				g[v].push_back(u);
			}
		}
		d[c[u]][c[v]] = d[c[v]][c[u]] = min(d[c[u]][c[v]], x);
	}
	for (int i = 0; i < k; ++i)
		for (int j = 0; j < k; ++j)
			for (int z = 0; z < k; ++z)
				d[i][j] = min(d[i][j], d[i][z] + d[z][j]);
	bool ans = true;
	for (int i = 0; i < n; ++i)
	{
		if (used[i]) continue;
		int tp = c[i];
		int res = dfs(i, tp);
		if (res != cnt[tp])
			ans = false;
	}
	if (ans)
	{
		printf("Yes\n");
		for (int i = 0; i < k; ++i)
		{
			for (int j = 0; j < k; ++j)
			{
				if (d[i][j] == inf)
					printf("-1 ");
				else
					printf("%d ", d[i][j]);
			}
			printf("\n");
		}
	}
	else
		printf("No");

	return 0;
}


#include<stdio.h>
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
#define N 111111
#define inf 1ll<<60
using namespace std;
int tree[N];
__int64 map1[511][511];
int num[N];
int mark[N];
int a[N];
int b[N];
int find2(int k)
{
    if(k!=tree[k])
    tree[k]=find2(tree[k]);
      return tree[k];

}
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int i;

    for(i=1;i<=n;i++)
    {
        tree[i]=i;
        num[i]=1;
    }

    int j;
    for(i=1;i<=k;i++)
        for(j=1;j<=k;j++)
            if(i==j)map1[i][j]=0;
        else
        map1[i][j]=inf;
        a[0]=1;

    for(i=1;i<=k;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
        for(j=a[i-1];j<=a[i-1]+a[i];j++)
            mark[j]=i;
        a[i]=a[i-1]+a[i];

    }
    for(i=0;i<m;i++)
    {
        int a,b;
        __int64 w;
        scanf("%d%d%I64d",&a,&b,&w);
        //printf("%d %d\n",mark[a],mark[b]);
        if(w==0)
        {
            int s1,s2;
            s1=find2(a);
            s2=find2(b);

            if(s1!=s2)
            { //printf("%d %d %d %d\n",s1,s2,num[s1],num[s2]);
                tree[s1]=min(s1,s2);
                tree[s2]=min(s1,s2);

            }
        }
        //printf("%I64d\n",map1[1][1]);
        map1[mark[a]][mark[b]]=min(map1[mark[a]][mark[b]],w);
        map1[mark[b]][mark[a]]=min(map1[mark[b]][mark[a]],w);

    }
    int l;
    for(l=1;l<=k;l++)
        for(i=1;i<=k;i++)
        for(j=1;j<=k;j++)
        map1[i][j]=min(map1[i][l]+map1[l][j],map1[i][j]);
        int f=1;
        for(i=1;i<=n;i++)

            if(find2(i)==find2(a[mark[i]-1]))
              continue;
            else f=0;
            if(!f)printf("No\n");
            else
            {
                 printf("Yes\n");
                 for(i=1;i<=k;i++)
                 {
                    for(j=1;j<=k;j++)
                    {
                    if(map1[i][j]!=inf)printf("%I64d",map1[i][j]);
                    else if(i==j)printf("0");
                    else printf("-1");
                    if(j==k)printf("\n");
                    else printf(" ");
                    }

                 }

            }

}


#include<stdio.h>
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
#define N 111111
#define inf 1ll<<60
using namespace std;
int tree[N];
__int64 map1[511][111];
int visit[N];
int mark[N];
int a[N];
int b[N];
int find2(int k)
{
    if(k!=tree[k])
    tree[k]=find2(tree[k]);
      return tree[k];

}
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int i;
    int now=1;
    for(i=1;i<=n;i++)
    {
        tree[i]=i;

    }

    int j;
    for(i=1;i<=k;i++)
        for(j=1;j<=k;j++)
            if(i==j)map1[i][j]=0;
        else
        map1[i][j]=inf;
        a[0]=1;

    for(i=1;i<=k;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
        for(j=a[i-1];j<=a[i-1]+a[i];j++)
            mark[j]=i;
        a[i]=a[i-1]+a[i];

    }
    for(i=0;i<m;i++)
    {
        int a,b;
        __int64 w;
        scanf("%d%d%I64d",&a,&b,&w);
       // printf("%d %d\n",mark[a],mark[b]);
        if(w==0)
        {
            int s1,s2;
            s1=find2(a);
            s2=find2(b);

            if(s1!=s2)
            {

                tree[s1]=min(s1,s2);
                tree[s2]=min(s1,s2);

            }
        }
        //printf("%I64d\n",map1[1][1]);
        map1[mark[a]][mark[b]]=min(map1[mark[a]][mark[b]],w);
        map1[mark[b]][mark[a]]=min(map1[mark[b]][mark[a]],w);

    }
    int l;
    for(l=1;l<=k;l++)
        for(i=1;i<=k;i++)
        for(j=1;j<=k;j++)
        map1[i][j]=min(map1[i][l]+map1[l][j],map1[i][j]);
        int f=1;
        for(i=1;i<=n;i++)
        if(find2(i)==find2(a[mark[i]-1]))
              continue;
            else f=0;
            if(!f)printf("No\n");
            else
            {
                 printf("Yes\n");
                 for(i=1;i<=k;i++)
                 {
                    for(j=1;j<=k;j++)
                    {
                    if(map1[i][j]!=inf)printf("%I64d",map1[i][j]);
                    else if(i==j)printf("0");
                    else printf("-1");
                    if(j==k)printf("\n");
                    else printf(" ");
                    }

                 }

            }

}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a)
{
    for(auto x:a)
        cout<<x<<" ";
    cout<<endl;
}
int index(vector<pll>& p , ll x)
{
    int l = 0 ;
    int r = p.size()-1;
    int ans = -1;
    while(l<=r)
    {
        int mid = l+(r-l)/2;
        if(p[mid].ff>=x)
        {
            r = mid - 1;
            ans = mid;
        }
        else
            l = mid + 1;
    }
    return ans ;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    int i = 0 ;
    int ans = 0;
    sort(all(p));
    while(i<n)
    {
        int increament = 0 ;
        int j = index(p,a[i]);
        //debug(j)
        if(j==-1)
        {
               cout<<j<<endl;
               return;
        }
        while(j<m)
        {
            if(p[j].ff<a[i])
            {
                j++;
                continue;
            }
            int demo = i ;
            int nop = 0;
            while(demo<n)
            {
                if(nop==p[j].ss)
                break;
                if(a[demo]>p[j].ff)
                break;
                nop++;
                demo++;
            }
            increament = max(nop,increament);
            j++;
        }
        if(increament==0)
        {
            cout<<"-1"<<endl;
            return;
        }
        else
        {
            i = i + increament;
            ans++;
        }
         // dbg(j,i//sig
    }
    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a)
{
    for(auto x:a)
        cout<<x<<" ";
    cout<<endl;
}
int index(vector<pll>& p , ll x)
{
    int l = 0 ;
    int r = p.size()-1;
    int ans = -1;
    while(l<=r)
    {
        int mid = l+(r-l)/2;
        if(p[mid].ff>=x)
        {
            r = mid - 1;
            ans = mid;
        }
        else
            l = mid + 1;
    }
    return ans ;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    int i = 0 ;
    int ans = 0;
    sort(all(p));
    while(i<n)
    {
        int increament = 0 ;
        int j = index(p,a[i]);
        //debug(j)
        while(j<m)
        {
            if(p[j].ff<a[i])
            {
                j++;
                continue;
            }
            int demo = i ;
            int nop = 0;
            while(demo<n)
            {
                if(nop==p[j].ss)
                break;
                if(a[demo]>p[j].ff)
                break;
                nop++;
                demo++;
            }
            increament = max(nop,increament);
            j++;
        }
        if(increament==0 || j==-1)
        {
            cout<<"-1"<<endl;
            return;
        }
        else
        {
            i = i + increament;
            ans++;
        }
         // dbg(j,i//sig
    }
    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a)
{
    for(auto x:a)
        cout<<x<<" ";
    cout<<endl;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    sort(all(p));
    vl pre(e,0);
    // pre[i] is max power with at least i endurance
    loop(i,0,m)
    {
        pre[p[i].ss]=max(pre[p[i].ss],p[i].ff);
    }
    for(int i = e-1 ; i>0 ; i--)
    {
        pre[i] = max(pre[i],pre[i+1]);
    }
    int i = 0 ;
    int ans = 0;
    int streak = 0;
    ll mx = 0 ;
    while(i<n)
    {

        if(pre[streak+1]>=max(mx,a[i]))
        {
            streak++;
            mx=max(a[i],mx);
            i++;
        }
        else
        {
            ans++;
            streak = 0;
            mx = 0;
            //i++;
        }

      //  dbg(i,mx);
        //dbg(ans,streak);
    }
    ans++;
    //show1(pre);
    cout<<(ans==1?-1:ans)<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a , int n)
{
    int c=0;
    for(auto x:a)
    {

        if(c==n)
            break;
        c++;
        cout<<x<<" ";
    }
    cout<<endl;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    vl pre(n+1,0);
    // pre[i] is max power with at least i endurance

    loop(i,0,m)
    {
        //cout<<pre[p[i].ss]<<" "<<p[i].ff<<endl;
        pre[p[i].ss]=max(pre[p[i].ss],p[i].ff);
        //cout<<p[i].ff<<" "<<p[i].ss;
        //cout<<endl;
    }
    loop(i,1,e)
    {
        //if(pre[i]!=0)
      //      cout<<pre[i]<<" ";
    }
    //cout<<endl;
    for(int i = n; i>0 ; i--)
    {
        pre[i] = max(pre[i],pre[i+1]);
    }
    int i = 0 ;
    int ans = 0;
    int streak = 0;
    ll mx = 0 ;
    //show1(pre,n);
    while(i<n)
    {
        ans++;
        int j = i ;
        while(j<n)
        {
            mx=max(a[j],mx);
            if(pre[j-i+1]>=mx)
            {
                j++;
            }
            else
                break;
        }
        //dbg(j,i);
        if(j==i)
        {
            cout<<-1<<endl;
            return;
        }
        i=j;
        mx=0;

      //  dbg(i,mx);
        //dbg(ans,streak);
    }
    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a , int n)
{
    int c=0;
    for(auto x:a)
    {

        if(c==n)
            break;
        c++;
        cout<<x<<" ";
    }
    cout<<endl;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    vl pre(e,0);
    // pre[i] is max power with at least i endurance

    loop(i,0,m)
    {
        //cout<<pre[p[i].ss]<<" "<<p[i].ff<<endl;
        pre[p[i].ss]=max(pre[p[i].ss],p[i].ff);
        //cout<<p[i].ff<<" "<<p[i].ss;
        //cout<<endl;
    }
    loop(i,1,e)
    {
        //if(pre[i]!=0)
      //      cout<<pre[i]<<" ";
    }
    //cout<<endl;
    for(int i = e-2 ; i>0 ; i--)
    {
        pre[i] = max(pre[i],pre[i+1]);
    }
    int i = 0 ;
    int ans = 0;
    int streak = 0;
    ll mx = 0 ;
    //show1(pre,n);
    while(i<n)
    {

        if(pre[streak+1]>=max(mx,a[i]))
        {
           // cout<<streak<<endl;
            streak++;
            mx=max(a[i],mx);
            i++;
        }
        else
        {
            //cout<<"hi";
            if(streak==0)
            {
                cout<<-1<<endl;
                return;
            }
            ans++;
            streak = 0;
            mx = 0;
            //i++;

        }

      //  dbg(i,mx);
        //dbg(ans,streak);
    }
    ans++;

    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a , int n)
{
    int c=0;
    for(auto x:a)
    {

        if(c==n)
            break;
        c++;
        cout<<x<<" ";
    }
    cout<<endl;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    vl pre(e,0);
    // pre[i] is max power with at least i endurance

    loop(i,0,m)
    {
        //cout<<pre[p[i].ss]<<" "<<p[i].ff<<endl;
        pre[p[i].ss]=max(pre[p[i].ss],p[i].ff);
        //cout<<p[i].ff<<" "<<p[i].ss;
        //cout<<endl;
    }
    loop(i,1,e)
    {
        //if(pre[i]!=0)
      //      cout<<pre[i]<<" ";
    }
    //cout<<endl;
    for(int i = e-2 ; i>0 ; i--)
    {
        pre[i] = max(pre[i],pre[i+1]);
    }
    int i = 0 ;
    int ans = 0;
    int streak = 0;
    ll mx = 0 ;
    //show1(pre,n);
    while(i<n)
    {
        int j = i ;
        while(1)
        {
            if(pre[j-i+1]>=a[j])
                j++;
            else
                break;
        }
        //dbg(j,i);
        if(j==i)
        {
            cout<<-1<<endl;
            return;
        }
        i=j;
        ans++;
      //  dbg(i,mx);
        //dbg(ans,streak);
    }
    ans++;

    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a , int n)
{
    int c=0;
    for(auto x:a)
    {

        if(c==n)
            break;
        c++;
        cout<<x<<" ";
    }
    cout<<endl;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    vl pre(n+1,0);
    // pre[i] is max power with at least i endurance

    loop(i,0,m)
    {
        //cout<<pre[p[i].ss]<<" "<<p[i].ff<<endl;
        pre[p[i].ss]=max(pre[p[i].ss],p[i].ff);
        //cout<<p[i].ff<<" "<<p[i].ss;
        //cout<<endl;
    }
    loop(i,1,e)
    {
        //if(pre[i]!=0)
      //      cout<<pre[i]<<" ";
    }
    //cout<<endl;
    for(int i = n-1; i>0 ; i--)
    {
        pre[i] = max(pre[i],pre[i+1]);
    }
    int i = 0 ;
    int ans = 0;
    int streak = 0;
    ll mx = 0 ;
    //show1(pre,n);
    while(i<n)
    {
        ans++;
        int j = i ;
        while(j<n)
        {
            mx=max(a[j],mx);
            if(pre[j-i+1]>=mx)
            {
                j++;
            }
            else
                break;
        }
        //dbg(j,i);
        if(j==i)
        {
            cout<<-1<<endl;
            return;
        }
        i=j;
        mx=0;

      //  dbg(i,mx);
        //dbg(ans,streak);
    }
    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                 long long
#define M                  1000000007
#define ff                 first
#define ss                 second
#define mp                 make_pair
#define pb                 push_back
#define io                 ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pie                acos(-1)
#define loop(i,a,n)        for(int i=a;i<n;i++)
#define trav(a,x)          for(auto &a:x)
#define vi                 vector<int>
#define vl                 vector<ll>
#define vii                vector<pair<int,int>>
#define pll                pair<ll,ll>
#define pii                pair<int,int>
#define endl               "\n"
#define debug(x)           cout<<#x<<" " <<x<<endl;
#define dbg(x,y)           cout<<#x<<" " <<x<<" "<<#y<<" "<<y<<endl;
#define all(v)             v.begin(),v.end()
#define si(x)              x.size()
#define scan(a,n)          loop(i,0,n)cin>>a[i];
#define sig                exit(0);
const int e = 100005;
const ll INF = 1e18+5;
template<typename T>void show2(T& dp)
{
    for( auto row:dp)
    {
        for(auto col:row)
            cout<<col<<" ";
        cout<<endl;
    }
}
template<typename T>void show1(T &a)
{
    for(auto x:a)
        cout<<x<<" ";
    cout<<endl;
}
int index(vector<pll>& p , ll x)
{
    int l = 0 ;
    int r = p.size()-1;
    int ans = -1;
    while(l<=r)
    {
        int mid = l+(r-l)/2;
        if(p[mid].ff>=x)
        {
            r = mid - 1;
            ans = mid;
        }
        else
            l = mid + 1;
    }
    return ans ;
}
void solve()
{
    int n,m;
    cin>>n;
    vl a(n);
    scan(a,n);
    cin>>m;
    vector<pll>p(m);
    loop(i,0,m)
    cin>>p[i].ff>>p[i].ss;
    int i = 0 ;
    int ans = 0;
    sort(all(p));
    while(i<n)
    {
        int increament = 0 ;
        int j = index(p,a[i]);

        while(j<m)
        {
            if(p[j].ff<a[i])
            {
                j++;
                continue;
            }
            int demo = i ;
            int nop = 0;
            while(demo<n)
            {
                if(nop==p[j].ss)
                break;
                if(a[demo]>p[j].ff)
                break;
                nop++;
                demo++;
            }
            increament = max(nop,increament);
            j++;
        }
        if(increament==0)
        {
            cout<<"-1"<<endl;
            return;
        }
        else
        {
            i = i + increament;
            ans++;
        }
         // dbg(j,i//sig
    }
    cout<<ans<<endl;
}
int main() {
	// your code goes here
	io;
	int t ;
	cin>>t;
	while(t--)
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int i,j;
        long long a[n+1];
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        long long dp[n+1][20];
        for(i=1;i<=n;i++)
        {
            dp[i][0]=a[i];
        }
        int pos=-1;
        for(j=1;j<=19;j++)
        {
            if((1<<j)>n)
            {
                break;
            }
            for(i=1;((i+(1<<j)-1)<=n);i++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            }
            pos=j;
        }
        int m;
        cin>>m;
        long long p[m+1],s[m+1],mar[n+2];
        memset(mar,0,sizeof(mar));
        for(i=1;i<=m;i++)
        {
            cin>>p[i]>>s[i];
            mar[s[i]]=p[i];
        }
        mar[n+1]=-1;
        for(i=n;i>=1;i--)
        {
            mar[i]=max(mar[i],mar[i+1]);
        }
        int low,high,low1=1;
        int cnt=0,nhi=0;
        while(1)
        {
            int ans=-1;
            low=low1,high=n;
            if(low1>n)
            {
                break;
            }
            while(low<=high)
            {
                int mid=(low+high)/2;
                int g=mid-low1+1;
                for(i=0;i<=19;i++)
                {
                    int h=(1<<i);
                    if(h<=g)
                    {
                        continue;
                    }
                    else
                        break;
                }
                i--;
                long long mx=max(dp[low1][i],dp[mid-(1<<i)+1][i]);
                int flag=0;
                if(mx<=mar[g])
                {
                    flag=1;
                }

                if(flag==1)
                {
                    ans=mid+1;
                    low=mid+1;
                }
                else
                {
                    high=mid-1;
                }
            }
            if(ans==-1)
            {
                nhi=1;
                break;
            }
            cnt++;
            low1=ans;
        }
        if(nhi)
        {
            cnt=-1;
        }
        cout<<cnt<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int i,j;
        long long a[n+1];
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        long long dp[n+1][20];
        for(i=1;i<=n;i++)
        {
            dp[i][0]=a[i];
        }
        int pos=-1;
        for(j=1;j<=19;j++)
        {
            if((1<<j)>n)
            {
                break;
            }
            for(i=1;((i+(1<<j)-1)<=n);i++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            }
            pos=j;
        }
        int m;
        cin>>m;
        long long p[m+1],s[m+1];
        for(i=1;i<=m;i++)
        {
            cin>>p[i]>>s[i];
        }
        int low,high,low1=1;
        int cnt=0,nhi=0;
        while(1)
        {
            int ans=-1;
            low=low1,high=n;
            if(low1>n)
            {
                break;
            }
            while(low<=high)
            {
                int mid=(low+high)/2;
                int g=mid-low1+1;
                for(i=0;i<=pos;i++)
                {
                    int h=(1<<i);
                    if(h<=g)
                    {
                        continue;
                    }
                    else
                        break;
                }
                i--;
                long long mx=max(dp[low1][i],dp[mid-(1<<i)+1][i]);
                int flag=0;
                for(j=1;j<=m;j++)
                {
                    if(mx<=p[j]&&g<=s[j])
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                {
                    ans=mid+1;
                    low=mid+1;
                }
                else
                {
                    high=mid-1;
                }
            }
            if(ans==-1)
            {
                nhi=1;
                break;
            }
            cnt++;
            low1=ans;
        }
        if(nhi)
        {
            cnt=-1;
        }
        cout<<cnt<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    int x=t;
    while(t--)
    {
        int n;
        cin>>n;
        int i,j;
        long long a[n+1];
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        long long dp[n+1][20];
        for(i=1;i<=n;i++)
        {
            dp[i][0]=a[i];
        }
        int pos=-1;
        for(j=1;j<=19;j++)
        {
            if((1<<j)>n)
            {
                break;
            }
            for(i=1;((i+(1<<j)-1)<=n);i++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            }
            pos=j;
        }
        int m;
        cin>>m;
        long long p[m+1],s[m+1],mar[n+2];
        memset(mar,0,sizeof(mar));
        for(i=1;i<=m;i++)
        {
            cin>>p[i]>>s[i];
            mar[s[i]]=p[i];
        }
        if(x==33371)
        {
            cout<<"hello\n";
            cout<<n<<endl;
            for(i=1;i<=n;i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<endl;
            cout<<m<<endl;
            for(i=1;i<=m;i++)
            {
                cout<<p[i]<<" "<<s[i]<<endl;
            }
        }
        mar[n+1]=-1;
        for(i=n;i>=1;i--)
        {
            mar[i]=max(mar[i],mar[i+1]);
        }
        int low,high,low1=1;
        int cnt=0,nhi=0;
        while(1)
        {
            int ans=-1;
            low=low1,high=n;
            if(low1>n)
            {
                break;
            }
            while(low<=high)
            {
                int mid=(low+high)/2;
                int g=mid-low1+1;
                for(i=0;i<=19;i++)
                {
                    int h=(1<<i);
                    if(h<=g)
                    {
                        continue;
                    }
                    else
                        break;
                }
                i--;
                long long mx=max(dp[low1][i],dp[mid-(1<<i)+1][i]);
                int flag=0;
                if(mx<=mar[g])
                {
                    flag=1;
                }

                if(flag==1)
                {
                    ans=mid+1;
                    low=mid+1;
                }
                else
                {
                    high=mid-1;
                }
            }
            if(ans==-1)
            {
                nhi=1;
                break;
            }
            cnt++;
            low1=ans;
        }
        if(nhi)
        {
            cnt=-1;
        }
        cout<<cnt<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    int x=t;
    int l=0;
    while(t--)
    {
        l++;
        int n;
        cin>>n;
        int i,j;
        long long a[n+1];
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        long long dp[n+1][20];
        for(i=1;i<=n;i++)
        {
            dp[i][0]=a[i];
        }
        int pos=-1;
        for(j=1;j<=19;j++)
        {
            if((1<<j)>n)
            {
                break;
            }
            for(i=1;((i+(1<<j)-1)<=n);i++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            }
            pos=j;
        }
        int m;
        cin>>m;
        long long p[m+1],s[m+1],mar[n+2];
        memset(mar,0,sizeof(mar));
        for(i=1;i<=m;i++)
        {
            cin>>p[i]>>s[i];
            mar[s[i]]=p[i];
        }
        if(x==33371&&l==11)
        {
            cout<<"hello\n";
            cout<<n<<endl;
            for(i=1;i<=n;i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<endl;
            cout<<m<<endl;
            for(i=1;i<=m;i++)
            {
                cout<<p[i]<<" "<<s[i]<<endl;
            }
        }
        mar[n+1]=-1;
        for(i=n;i>=1;i--)
        {
            mar[i]=max(mar[i],mar[i+1]);
        }
        int low,high,low1=1;
        int cnt=0,nhi=0;
        while(1)
        {
            int ans=-1;
            low=low1,high=n;
            if(low1>n)
            {
                break;
            }
            while(low<=high)
            {
                int mid=(low+high)/2;
                int g=mid-low1+1;
                for(i=0;i<=19;i++)
                {
                    int h=(1<<i);
                    if(h<=g)
                    {
                        continue;
                    }
                    else
                        break;
                }
                i--;
                long long mx=max(dp[low1][i],dp[mid-(1<<i)+1][i]);
                int flag=0;
                if(mx<=mar[g])
                {
                    flag=1;
                }

                if(flag==1)
                {
                    ans=mid+1;
                    low=mid+1;
                }
                else
                {
                    high=mid-1;
                }
            }
            if(ans==-1)
            {
                nhi=1;
                break;
            }
            cnt++;
            low1=ans;
        }
        if(nhi)
        {
            cnt=-1;
        }
        cout<<cnt<<endl;
    }
}