#include <bits/stdc++.h>
using namespace std;
using INT = long long;
const int NN = 1272727;
int vec[10];
int dfs(int *A, int sz, int x) {
  if (!x or !sz) return 0;
  int ans = 0;
  int SZ = 0;
  int B[8];
  for (int i = 1; i <= sz; i++) {
    ans = ans + x / A[i] - dfs(B, SZ, x / A[i]);
    B[++SZ] = A[i];
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int x, p, k;
    scanf("%d%d%d", &x, &p, &k);
    int r = p;
    int cnt = 0;
    for (int i = 2; i * i <= r; i++) {
      if (r % i == 0) {
        vec[++cnt] = i;
        while (r % i == 0) r /= i;
      }
    }
    if (r > 1) vec[++cnt] = r;
    int N = x - dfs(vec, cnt, x);
    N += k;
    int st = 0, ed = 1e9;
    while (st + 1 < ed) {
      int mid = st + ed >> 1;
      int M = mid - dfs(vec, cnt, mid);
      if (M >= N)
        ed = mid;
      else
        st = mid;
    }
    printf("%d\n", ed);
  }
  return 0;
}
