#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
cin>>t;
while(t--){
int l=0,r=0,ans=0,a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
l+=a,r+=a;ans+=a;
while((l>0||r>0)&&(b>0||c>0)){
if(l<0&&r<0)break;
if(l<=0&&c<=0||r<=0&&b<=0)break;
if(c!=0||b==0){
if(r>=c)r-=c,l+=c,ans+=c,c=0;
else l+=r,c-=r,ans+=r,r=0;
}
if(l<=0&&c<=0||r<=0&&b<=0)break;
if(b!=0||c==0){
if(l>=b)l-=b,r+=b,ans+=b,b=0;
else r+=l,b-=l,ans+=l,l=0;
}
}if((l==0||r==0)&&(b||c||d))ans++;
if(l>0&&r>0)ans+=min(min(l,r)+1,d);
cout<<ans<<endl;
}
return 0;
}