#include <bits/stdc++.h>
using namespace std;
bool prime[1000001];
void SieveOfEratosthenes(int n = 1000001) {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  if (n % 2 == 0) {
    cout << (n + 2 * k) << endl;
  } else {
    int div = -1;
    for (int i = 2; i < n; i++) {
      if (n % i == 0) {
        div = i;
        break;
      }
    }
    if (div == -1) {
      int ans = 2 * n;
      cout << ans + 2 * (k - 1) << endl;
    } else {
      int ans = n + div;
      cout << ans + 2 * (k - 1) << endl;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
