#include #define ll long long using namespace std; inline ll read(){ ll x=0,f=1;char cc=getchar(); while(cc<'0' || cc>'9') {if(cc=='-') f=-1;cc=getchar();} while(cc>='0' && cc<='9') {x=x*10+cc-'0';cc=getchar();} return x*f; } ll len[5],num[5],sum[5],d[5]; ll k,ans; int main(){ int tt=read(); for(;tt;tt--){ for(int i=1;i<=4;i++) len[i]=read(); k=read();ans=0; sum[1]=0;num[1]=1; for(int i=2;i<=4;i++) sum[i]=len[i-1]+1; for(int i=1;i<=4;i++) d[i]=sum[i]*(5-i); num[2]=num[3]=num[4]=4; while(k>0){ int x=1; for(int i=2;i<=4;i++){ if(num[x]==0){ x=i; continue; } if(num[i]>0 && d[i]=(len[x]<<1)+1){ num[x]=0;continue; } num[x]=sum[x]<<2; if(x>1 && sum[x]<=len[x-1]<<1) num[x]-=(len[x-1]<<3)-(sum[x]<<2)+4; if(sum[x]>len[x]) num[x]-=((sum[x]-len[x])<<3)-4; } printf("%lld\n",ans); } }