#include <iostream>
using namespace std;

int main(){
  int t;
  cin >> t;
  while (t--){
    unsigned n, k;
    cin >> n >> k;

    if ( ! ( n <= 2147483648u && k <= 2147483648u && k >= 2 )){
      cerr << "error!! " << n << ' ' << k << endl;
    }

    if (n == 0){
      cout << 0 << endl;
    } else {
      cout << (n-1)/(k-1) + n << endl;
    }
  }
}
