#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
int least_prime[1000002];
void leastPrimeFactor(int n) {
  least_prime[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (least_prime[i] == 0) {
      least_prime[i] = i;
      for (int j = 2 * i; j <= n; j += i)
        if (least_prime[j] == 0) least_prime[j] = i;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (register int i = 0; i < 1000002; i++) {
    least_prime[i] = 0;
  }
  leastPrimeFactor(1000002 - 1);
  int t, n, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    long long int sum = n;
    if (k == 1) {
      sum = sum + least_prime[n];
    } else {
      int temp = k - 1;
      sum = sum + least_prime[n];
      sum = sum + 2 * temp;
    }
    cout << sum << endl;
  }
  return 0;
}
