#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    if (n % 2 == 0)
      cout << n + 2 * k << endl;
    else {
      long long i = 2;
      for (; i * i <= n; i++)
        if (n % i == 0) break;
      if (n % i) i = n;
      n += i;
      cout << n + 2 * (k - 1) << endl;
    }
  }
}
