#include <bits/stdc++.h>
using namespace std;
int N = 1000000;
int Last[1000010];
vector<int> H;
int calc(int val) {
  int sz = H.size();
  int ret = 0;
  for (int i = 0; i < (1 << sz); ++i) {
    int sign = 0;
    int prod = 1;
    for (int j = 0; j < sz; ++j) {
      if ((i >> j) & 1) prod = prod * H[j], ++sign;
    }
    sign = sign & 1;
    sign = sign ^ 1;
    sign = sign * 2 - 1;
    ret = ret + sign * (val / prod);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  for (int i = 2; i <= N; ++i) {
    if (!Last[i])
      for (int j = i; j <= N; j += i) Last[j] = i;
  }
  while (T--) {
    int x, p, k;
    cin >> x >> p >> k;
    H.clear();
    while (p != 1) {
      int div = Last[p];
      H.push_back(div);
      while (p % div == 0) {
        p = p / div;
      }
    }
    int d = calc(x);
    k = k + d;
    int l = 1, r = 1e9;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (calc(mid) >= k)
        r = mid - 1;
      else
        l = mid + 1;
    }
    cout << r + 1 << '\n';
  }
  return 0;
}
