#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  long long n = 1000002;
  vector<long long> least_prime(n + 1, 0);
  least_prime[1] = 1;
  for (long long i = 2; i <= n; i++) {
    if (least_prime[i] == 0) {
      least_prime[i] = i;
      for (long long j = 2 * i; j <= n; j += i)
        if (least_prime[j] == 0) least_prime[j] = i;
    }
  }
  while (t--) {
    long long ans, m, k;
    cin >> m >> k;
    if (m % 2 == 0) {
      ans = m + (k * 2);
      cout << ans << endl;
    } else {
      m = m + least_prime[m];
      ans = m + (k - 1) * 2;
      cout << ans << endl;
    }
  }
}
