#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 150001
using namespace std;
const long long oo=round(1e18);
long long kq,st[4*N],f[N];
vector <int> a[5][N];
int sl[5],n,m,i,j,u,v,c[5][N];
void update(int id,int l,int r,int u,long long k)
{
if(u<l || u>r) return ;
if(l==r) { st[id]=k; return ; }
int mid=(l+r)>>1;
update(id*2,l,mid,u,k); update(id*2+1,mid+1,r,u,k);
st[id]=min(st[id*2],st[id*2+1]);
}
long long get(int id,int l,int r,int u,int v)
{
if(u>r || v<l || l>r) return oo;
if(u<=l && r<=v) return st[id];
int mid=(l+r)>>1;
return min(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
int main()
{
// freopen("ntu.inp","r",stdin);
// freopen("ntu.out","w",stdout);
cin>>sl[1]>>sl[2]>>sl[3]>>sl[4];
n=max(max(sl[1],sl[2]),max(sl[3],sl[4]));
for(j=1;j<=4;j++)
for(i=1;i<=sl[j];i++) cin>>c[j][i];
for(j=2;j<=4;j++)
{
cin>>m;
while(m--)
{
cin>>u>>v;
a[j][v].push_back(u);
}
}
for(i=1;i<=sl[1];i++) f[i]=c[1][i];
kq=oo;
for(j=2;j<=4;j++)
{
for(i=1;i<=4*n;i++) st[i]=oo;
for(i=1;i<=sl[j-1];i++)
{
update(1,1,n,i,f[i]);
}
for(u=1;u<=sl[j];u++)
{
a[j][u].push_back(0); a[j][u].push_back(n+1);
sort(a[j][u].begin(),a[j][u].end());
f[u]=oo;
for(i=1;i<a[j][u].size();i++)
{
f[u]=min(f[u],get(1,1,n,a[j][u][i-1]+1,a[j][u][i]-1)+c[j][u]);
f[u]=min(f[u],oo);
}
if(j==4) kq=min(kq,f[u]);
}
}
if(kq==oo) kq=-1;
cout<<kq;
}
1562B - Scenes From a Memory | 1521A - Nastia and Nearly Good Numbers |
208. Implement Trie | 1605B - Reverse Sort |
1607C - Minimum Extraction | 1604B - XOR Specia-LIS-t |
1606B - Update Files | 1598B - Groups |
1602B - Divine Array | 1594B - Special Numbers |
1614A - Divan and a Store | 2085. Count Common Words With One Occurrence |
2089. Find Target Indices After Sorting Array | 2090. K Radius Subarray Averages |
2091. Removing Minimum and Maximum From Array | 6. Zigzag Conversion |
1612B - Special Permutation | 1481. Least Number of Unique Integers after K Removals |
1035. Uncrossed Lines | 328. Odd Even Linked List |
1219. Path with Maximum Gold | 1268. Search Suggestions System |
841. Keys and Rooms | 152. Maximum Product Subarray |
337. House Robber III | 869. Reordered Power of 2 |
1593C - Save More Mice | 1217. Minimum Cost to Move Chips to The Same Position |
347. Top K Frequent Elements | 1503. Last Moment Before All Ants Fall Out of a Plank |