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