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