#include #include #include #include using namespace std; struct edge{ int f,t,nxt,cap,flow,cost; }a[300009]; int fir[1009],now[1009],ans; queue q; int cur=1,dist[1009],ss,tt,p[1009],from[1009]; bool inq[10009]; int s[509],t[509],P,Q,k,n,m,T,fff; #define INF 0x3f3f3f3f void addedge(int from,int to,int flow,int cost){ cur++; if (fir[from]==0) fir[from]=cur; a[cur].t=to; a[cur].f=from; a[cur].cost=cost; a[now[from]].nxt=cur; a[cur].cap=flow; now[from]=cur; } void addedge_2(int from,int to,int flow,int cost){ addedge(from,to,flow,cost); addedge(to,from,0,-cost); } bool spfa(){ q.push(ss); memset(dist,0x3f,sizeof(dist)); inq[ss]=1; dist[ss]=0; while(!q.empty()){ int x=q.front(); inq[x]=false; q.pop(); for (int i=fir[x];i!=0;i=a[i].nxt){ if (a[i].cap==a[i].flow) continue; if (dist[a[i].t]>dist[x]+a[i].cost){ dist[a[i].t]=dist[x]+a[i].cost; from[a[i].t]=i; if (!inq[a[i].t]){ q.push(a[i].t); inq[a[i].t]=true; } } } } if (dist[tt]==0x3f3f3f3f) return false; int p=tt,flow=0x3f3f3f3f; while(p!=ss){ flow=min(flow,a[from[p]].cap-a[from[p]].flow); p=a[from[p]].f; } p=tt; while(p!=ss){ int x=from[p]; a[x].flow+=flow; a[x^1].flow-=flow; p=a[from[p]].f; } ans+=flow*dist[tt]; fff+=flow; return true; } int main(){ scanf("%d",&T); while(T--){ memset(fir,0,sizeof(fir)); memset(now,0,sizeof(now)); memset(a,0,sizeof(a)); cur=1; ans=0; int cnt=0; fff=0; scanf("%d%d",&n,&k); for (int i=1;i<=n;i++){ scanf("%d",&p[i]); cnt+=p[i]; } scanf("%d%d%d",&m,&P,&Q); for (int i=1;i<=m;i++){ scanf("%d%d",&s[i],&t[i]); } ss = 0,tt = (n << 1) + 1; for (int i = 1;i <= n;i ++) addedge_2(ss,i,p[i],0),addedge_2(i + n,tt,p[i],0); for (int i = 1;i < n;i ++) addedge_2(i + n,i + n + 1,INF,0); addedge_2(ss,n + 1,k,0); for (int i = max(1,P);i <= n;i ++) addedge_2(ss,i + n,INF,Q); for (int i = 1;i <= n;i ++) { for (int j = 1;j <= m;j ++) { if (i + t[j] <= n) addedge_2(i,i + t[j] + n,INF,s[j]); } } while(spfa()); if (cnt!=fff){ printf("No solution\n"); } else printf("%d\n",ans); } }