Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created March 23, 2023 14:17
Show Gist options
  • Save qjatn0120/26e54d8e9ccdeb241cb56cfb4962a428 to your computer and use it in GitHub Desktop.
Save qjatn0120/26e54d8e9ccdeb241cb56cfb4962a428 to your computer and use it in GitHub Desktop.
#ifdef DEBUG
#include "debug.h"
#endif // DEBUG
#ifndef DEBUG
template <typename T>
void debug(T &x){}
template <typename T>
void debug(T &x, int i, int j){}
#endif // DEBUG
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b){
if(b == 0) return a;
return gcd(b, a % b);
}
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--){
long long int A, B, C, D;
cin >> A >> B >> C >> D;
long long int rem = A % B;
if(B > D) swap(B, D);
if(rem + 1 != B) cout << "1\n";
else cout << (B * D / gcd(B, D) - rem) << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment