Source Code
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define all(v) v.begin(), v.end()
#define pb push_back
#define sz(x) (int)(x).size()

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
  cout << vars << " = ";
  string delim = "";
  (..., (cout << delim << values, delim = ", "));
  cout << '\n';
}

inline void solve() {
  // a * b / (gcd(a, b)^2)
  ll n, a;
  cin >> n >> a;
  ll mx = (a + 1) * a, ans = a + 1;
  for(ll i = n; ; --i) {
    ll g = gcd(i, a);
    if(g == 1) {
      cout << i << '\n';
      return;
    }
  }
  cout << ans << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  // freopen("input.txt", "r", stdin);
  int T = 1;
  cin >> T;
  while(T--) {
    solve();
  }
}
Copy
LCM and GCD noomaK
GNU G++17
12 ms
508 KB
Accepted