#include <bits/stdc++.h>
using namespace std;
const int N = 1000002;
vector<int> primeFact[N];
int calc(int y, int x, vector<int> &a) {
  int ret = 0, n = (int)(a).size();
  for (int(mask) = (0); (mask) <= (int)((1 << n) - 1); (mask)++) {
    int p = 1, sign = 1;
    for (int(i) = (0); (i) <= (int)(n - 1); (i)++)
      if (mask & (1 << i)) p *= a[i], sign *= -1;
    int frst = x + (p - x % p) % p;
    ret += sign * max((y - frst + p) / p, 0);
  }
  return ret;
}
int main() {
  int t;
  for (int(i) = (2); (i) <= (int)(N - 1); (i)++)
    if (primeFact[i].empty()) {
      for (int j = i; j < N; j += i) primeFact[j].push_back(i);
    }
  scanf("%d", &t);
  while (t--) {
    int x, p, k;
    scanf("%d%d%d", &x, &p, &k);
    int l = x + 1, r = 20 * N;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (k <= calc(mid, x + 1, primeFact[p]))
        r = mid;
      else
        l = mid + 1;
    }
    printf("%d\n", l);
  }
  return 0;
}
