Help me in solving CHANGEXY problem

My issue

I am getting the right output but time limit gets exceeded, what can i do to optimize it,

My code

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int a,b,k;
    cin >> a >> b >> k;
    int c=0;
    while(b!=a)
    {
        c++;
        if(((b/k)>=a) && ((b%k)==0))
        {
            b=b/k;
        }
        else
        {
            b=b-1;
        }
    }
    cout << c << endl;
}

int main() {
	int t;
	cin >> t;
	while(t--)
	{
	    solve();
	}

}

Problem Link: Change A to B Practice Coding Problem

you are doint b=b-1
this is causing you TLE
to get b such that b%k==0
b=b- b%k;cost=b%k;
and check that b should not be less than a
if(less than a)cost=b-a