#include <bits/stdc++.h>
using namespace std;
vector<int> prime;
char sieve[10000005];
void isprime(int n) {
  sieve[0] = 1, sieve[1] = 1;
  sieve[2] = 0;
  for (int i = 4; i <= n; i += 2) sieve[i] = 1;
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (sieve[i] == 0) {
      for (int j = i * i; j <= n; j += 2 * i) {
        sieve[j] = 1;
      }
    }
  }
  prime.push_back(2);
  for (int i = 3; i <= n; i += 2) {
    if (sieve[i] == 0) prime.push_back(i);
  }
}
long long div(long long n) {
  long long num = 1e16;
  for (long long i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i) {
        num = min(num, i);
      } else {
        num = min(num, i);
        num = min(num, n / i);
      }
    }
  }
  return num;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  if (n % 2 == 0) {
    cout << n + (2 * k) << '\n';
  } else {
    if (sieve[n] == 0) {
      k--;
      long long ans = (n * 2) + (2 * k);
      cout << ans << '\n';
    } else {
      k--;
      long long ans = div(n) + n;
      cout << ans + (k * 2) << '\n';
    }
  }
}
int main() {
  isprime(1000000);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
