#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int N = 1e6 + 5;
vector<int> d[N];
bool vis[N];
void init() {
  for (int i = 2; i < N; i++)
    if (!vis[i])
      for (int j = i; j < N; j += i) d[j].push_back(i), vis[j] = 1;
}
int gao(int n, int x) {
  long long ret = 0;
  for (int i = 0; i < (1 << d[x].size()); i++) {
    int tmp = 1;
    for (int j = 0; j < d[x].size(); j++)
      if (i >> j & 1) tmp *= d[x][j];
    if (__builtin_parity(i)) tmp *= -1;
    ret += n / tmp;
  }
  return ret;
}
int main() {
  init();
  int t;
  scanf("%d", &t);
  while (t--) {
    static int x, p, k;
    scanf("%d%d%d", &x, &p, &k);
    int l = 1, r = 1e8, ans = x;
    int pos = gao(x, p) + k;
    while (l <= r) {
      int mid = l + r >> 1;
      int tmp = gao(mid, p);
      if (tmp < pos)
        ans = mid + 1, l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}
