#include <bits/stdc++.h>
using namespace std;
long long sie[1000001] = {0};
void sieve() {
  for (int i = 2; i <= 1000001; i++) {
    if (sie[i] == 0) {
      sie[i] = i;
      for (int j = i * 2; j <= 1000001; j += i) {
        if (sie[j] == 0) sie[j] = i;
      }
    }
  }
}
long long div(long long n) {}
void func() {
  long long n, k;
  cin >> n >> k;
  long long ans = n;
  if (sie[ans] > 2) {
    cout << n + sie[ans] + (2 * (k - 1)) << endl;
  } else {
    cout << n + sie[ans] + 2 * (k - 1) << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  sie[0] = sie[1] = 0;
  sieve();
  int t;
  cin >> t;
  for (long long i = 1; i <= t; i++) {
    func();
  }
  return 0;
}
