ll@N,@M,@--(A,B)[M],@--C[N],@W[10],@Q,@--(U,V)[Q],a[Q],c;rep(q,Q)a[q]=-1;REP(b,1024){c=sum[d,0,10@(b>>d)&1](W[d]);unionFind t('m',N,1);rep(j,M)if((b>>C[A[j]])&1&(b>>C[B[j]]))t.connect(A[j],B[j]);rep(q,Q)if((b>>C[U[q]])&1&(b>>C[V[q]])&t(U[q])==t(V[q])&(a[q]<0|a[q]>c))a[q]=c;}wtLn(a(Q));