#include #include #include using namespace std; using ll = long long; vector ans; int f(int n , int k){ if(k == 1){ return n - 1; } int now = 1 , res = 1; for(;res <= 1000; res++){ if(res == 1)now += 1; if(res == 2)now += k; if(res >= 3)now+= pow(k , res - 1); if(now >= n)break; } return res; } int main(){ int q; cin >> q; while(q--){ int n , k; cin >> n >> k; ans.push_back(f(n , k)); } for(int i = 0; i < ans.size(); i++){ cout << ans[i] << endl; } return 0; }