#include <bits/stdc++.h>
using namespace std;
long long int spf[1000005];
void pf() {
  spf[1] = 1;
  for (long long int i = 2; i < 1000005; i++) spf[i] = i;
  for (long long int i = 4; i < 1000005; i += 2) spf[i] = 2;
  for (long long int i = 3; i * i < 1000005; i++) {
    if (spf[i] == i) {
      for (long long int j = i * i; j < 1000005; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
void shikhar7s(long long int cas) {
  pf();
  long long int n, k, i;
  cin >> n >> k;
  if (spf[n] == 2) {
    cout << n + (2 * k) << "\n";
  } else {
    cout << n + spf[n] + (2 * (k - 1)) << "\n";
  }
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int t = 1;
  cin >> t;
  long long int cas = 1;
  while (cas <= t) {
    shikhar7s(cas);
    cas++;
  }
  return 0;
}
