#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
  int c = 0;
  for (int i = 2; i <= sqrt(n); i++)
    if (n % i == 0) {
      c++;
      break;
    }
  if (c == 0)
    return true;
  else
    return false;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    if (n % 2 == 0)
      cout << n + 2 * k << endl;
    else {
      if (isprime(n))
        cout << n + n + 2 * (k - 1) << endl;
      else {
        for (long long int i = 3; i <= sqrt(n); i += 2) {
          if (n % i == 0) {
            cout << n + i + 2 * (k - 1) << endl;
            break;
          }
        }
      }
    }
  }
}
