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