#include using namespace std; const int N=105,E=100005,inf=1000000000; int t,a[5],i; long long n,ans; struct str{ long long l,r,d; }p[4]; int main(){ scanf("%d",&t); while(t--) { a[0]=-1; for(i=1;i<=4;++i) { cin>>a[i]; p[i-1]=(str){a[i-1],a[i],a[i-1]+1}; } cin>>n; ans=0; while(1) { int x=0; for(i=1;i<=3;++i) if((p[i].d*(4-i)p[x].r*2)&&p[i].d<=p[i].r*2) x=i; long long num=(min(p[x].d,p[x].r-(p[x].d-p[x].r)+1)-max(0ll,2*p[x].l-p[x].d+1))*4; if(p[x].d==0) num=1; if(n<=num) { ans+=n*p[x].d*(4-x); break; } else { ans+=num*p[x].d*(4-x); n-=num; } ++p[x].d; } cout<