#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t, n, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    if (n % 2 == 0) {
      cout << n + k * 2 << "\n";
      continue;
    } else {
      int flag = 0, j;
      for (j = 3; j <= sqrt(n); j += 2) {
        if (n % j == 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        cout << 2 * n + ((k - 1) * 2) << "\n";
        continue;
      } else {
        cout << (n + j) + (2 * (k - 1)) << "\n";
        continue;
      }
    }
  }
  return 0;
}
