#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n, x, p, k;
vector<long long> pp;
vector<long long> fac(long long x) {
  vector<long long> ans = {};
  for (long long i = 2; i * i <= x; i++) {
    if (x % i) continue;
    ans.push_back(i);
    do {
      x /= i;
    } while (x % i == 0);
  }
  if (x > 1) ans.push_back(x);
  return ans;
}
long long sharedlesseqhelp(long long x, int ind = pp.size() - 1) {
  if (ind < 0) return 0;
  if (x == 1) return 0;
  long long p = pp[ind];
  return x / p + sharedlesseqhelp(x, ind - 1) -
         sharedlesseqhelp(x / p, ind - 1);
}
long long coprimelesseq(long long x) { return x - sharedlesseqhelp(x); }
long long binhelp(long long x) {
  long long lo = x, hi = 1e17;
  while (lo < hi) {
    long long mid = (lo + hi) / 2;
    if (coprimelesseq(mid) < x) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  return lo;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> p >> k;
    pp = fac(p);
    cout << binhelp(k + coprimelesseq(x)) << endl;
  }
  cout.flush();
  return 0;
}
