#include <bits/stdc++.h>
using namespace std;
long long f[2000000];
void pre() {
  long long i, j, k;
  f[1] = 1;
  for (i = 2; i <= 2000000; i++) {
    if (f[i] == 0) {
      for (j = i; j <= 2000000; j += i) {
        if (f[j] == 0) {
          f[j] = i;
        }
      }
    }
  }
}
void solve() {
  long long n, k;
  cin >> n >> k;
  if (n % 2 == 0) {
    n = n + (k * 2);
  } else {
    k--;
    n = n + f[n];
    n = n + (k * 2);
  }
  cout << n << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  pre();
  while (t--) {
    solve();
  }
}
