#include <bits/stdc++.h>
const int LIM = 1e5;
using namespace std;
void solve() {
  long long n, k;
  cin >> n >> k;
  if (n % 2 == 0)
    n += k * 1LL * 2;
  else {
    int f = 0;
    for (long long i = 3; i * i <= n; i++)
      if (n % i == 0) {
        f = 1;
        n += i;
        break;
      }
    if (!f) n += n;
    if (k) n += (k - 1) * 1LL * 2;
  }
  cout << n << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
