#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> u;
    int now = 2;
    while (now * now <= p) {
      if (p % now == 0) {
        u.emplace_back(now);
        while (p % now == 0) p /= now;
      }
      ++now;
    }
    if (p > 1) u.emplace_back(p);
    auto cal = [&](long long x) {
      long long ans = 0;
      for (int i = 1; i < 1 << (int)u.size(); i++) {
        long long now = 1;
        int cnt = 0;
        for (int j = 0; j < (int)u.size(); j++) {
          if (i >> j & 1) {
            now *= u[j];
            ++cnt;
          }
        }
        if (cnt & 1)
          ans += x / now;
        else
          ans -= x / now;
      }
      return x - ans;
    };
    long long fix = cal(n);
    int l = 1, r = 1e9;
    while (l <= r) {
      int mid = l + r >> 1;
      if (cal(mid) - fix >= k)
        r = mid - 1;
      else
        l = mid + 1;
    }
    cout << l << "\n";
  }
}
