由题意可以知
CRT模板即可
#include<iostream>
using namespace std;
typedef long long ll;
const int N=19;
ll a[N],b[N],m[N],invm[N];
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1,y=0;return a;}ll d=exgcd(b,a%b,y,x);y-=a/b*x;return d;
}
void solve(){for(int i=1;i<=3;i++){cin>>a[i];}int d;cin>>d;b[1]=23;b[2]=28;b[3]=33;ll M=1;for(int i=1;i<=3;i++){M*=b[i];}for(int i=1;i<=3;i++){m[i]=M/b[i];}for(int i=1;i<=3;i++){ll t;ll d=exgcd(m[i],b[i],invm[i],t);}ll X=0;for(int i=1;i<=3;i++){X=(X+a[i]*m[i]*invm[i]%M)%M;}while(X<=d){X+=M;}ll ans=X-d;cout<<ans<<'\n';
}
int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);int t;cin>>t;while(t--){solve();}return 0;
}