#line 1 "c.cpp" #include #include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define repp(i,m,n) for (int i = m; i < int(n); ++i) #define reb(i,n) for (int i = int(n)-1; i >= 0; --i) #define all(v) v.begin(),v.end() using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair; using PL = pair; using pdd = pair; using pil = pair; using pli = pair; templateistream &operator>>(istream &is,vector &v){for(auto &e:v)is>>e;return is;} templatebool range(T a,T b,T x){return (a<=x&&xbool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));} template T rev(const T& str_or_vec){T res = str_or_vec; reverse(res.begin(),res.end()); return res; } templatebool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} templatebool chmax(T &a,const T &b){if(avoid uniq(vector &v){sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());} templatevoid print(pair a); templatevoid print(vector v); templatevoid print(vector> v); void print(){ putchar(' '); } void print(bool a){ printf("%d", a); } void print(int a){ printf("%d", a); } void print(long a){ printf("%ld", a); } void print(long long a){ printf("%lld", a); } void print(char a){ printf("%c", a); } void print(char a[]){ printf("%s", a); } void print(const char a[]){ printf("%s", a); } void print(long double a){ printf("%.15Lf", a); } void print(const string& a){ for(auto&& i : a) print(i); } void print(unsigned int a){ printf("%u", a); } void print(unsigned long long a) { printf("%llu", a); } template void print(const T& a){ cout << a; } int out(){ putchar('\n'); return 0; } template int out(const T& t){ print(t); putchar('\n'); return 0; } template int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } templatevoid print(pair a){print(a.first);print(),print(a.second);} templatevoid print(vector v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}} templatevoid print(vector> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}} void yes(){out("Yes");} void no (){out("No");} void yn (bool t){if(t)yes();else no();} void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout< dx = {0,1,0,-1,1,1,-1,-1}; const vector dy = {1,0,-1,0,1,-1,-1,1}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; const string NUM = "0123456789"; } // namespace noya2 using namespace noya2; //using mint = modint998244353; using mint = modint1000000007; //using mint = modint; void out(mint a){out(a.val());} void out(vector a){vector b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);} void out(vector> a){for (auto v : a) out(v);} #line 2 "Graph-core.hpp" #line 5 "Graph-core.hpp" namespace noya2 { using namespace std; struct naiveGraph { // undirected unweighted tree naiveGraph (int _n = 0) : n(_n){ init();} void add_edge(int u, int v, bool undirect = true, int id = -1){ es0[u].emplace_back(v); es1[u].emplace_back(v,id); if (undirect){ es0[v].emplace_back(u); es1[v].emplace_back(u,id); } } void remake(int new_n){ es0.clear(); es1.clear(); vis.clear(); n = new_n; init(); } bool yet(int v){ return vis[v] == 0;} void visit(int v) { vis[v]++;} void reset(int v = -1){ if (v == -1) fill(vis.begin(),vis.end(),0); else vis[v] = 0; } const vector& operator[](int idx) const { return es0[idx];} const vector>& operator()(int idx) const {return es1[idx];} private: int n; vector> es0; vector>> es1; vector vis; void init(){ es0.resize(n); es1.resize(n); vis.resize(n,0); } }; struct usefulGraph { // directed weighted graph using ll = long long; static constexpr ll DINF = 2e18; usefulGraph (int _n = 0) : n(_n) { init();} void add_edge(int u, int v, ll cost = 1, bool undirect = true){ es[u].emplace_back(v,cost); if (undirect) es[v].emplace_back(u,cost); } void remake(int new_n){ es.clear(); vis.clear(); n = new_n; init(); } bool yet(int v){ return vis[v] == 0;} void visit(int v) { vis[v]++;} void reset(int v = -1){ if (v == -1) fill(vis.begin(),vis.end(),0); else vis[v] = 0; } vector dijkstra(int s){ // all edge weight >= 0 vector dist(n,DINF); dist[s] = 0LL; priority_queue,vector>,greater>> pque; pque.push(pair(0,s)); while (!pque.empty()){ ll d = pque.top().first; int f = pque.top().second; pque.pop(); if (dist[f] < d) continue; for (auto e : es[f]){ if (dist[e.first] > d + e.second){ dist[e.first] = d + e.second; pque.push(pair(dist[e.first],e.first)); } } } return dist; } vector bfs01(int s){ // all edge weight = 0 or 1 vector dist(n,DINF); dist[s] = 0; deque que; que.push_back(s); while (!que.empty()){ auto f = que.front(); que.pop_front(); for (auto e : es[f]){ if (dist[e.first] > dist[f] + e.second){ dist[e.first] = dist[f] + e.second; if (e.second == 0) que.push_front(e.first); else que.push_back(e.first); } } } return dist; } vector bellman_ford(int s, bool &ng_cycle){ vector dist(n,DINF); vector ng; dist[s] = 0; int t = 0; while (t < n){ bool finish = true; for (int f = 0; f < n; f++){ if (dist[f] == DINF) continue; for (auto e : es[f]){ if (dist[e.first] > dist[f] + e.second){ dist[e.first] = dist[f] + e.second; finish = false; if (t == n-1) ng.emplace_back(e.first); } } } if (finish) break; t++; } ng_cycle = (t == n); if (ng_cycle){ for (auto v : ng) dist[v] = -DINF; t = n; while (t--){ for (int f = 0; f < n; f++){ if (dist[f] != -DINF) continue; for (auto e : es[f]){ dist[e.first] = -DINF; } } } } return dist; } vector> warshall_floyd(){ vector> res(n,vector(n,DINF)); for (int i = 0; i < n; i++){ res[i][i] = 0; for (auto e : es[i]){ res[i][e.first] = min(res[i][e.first],e.second); } } for (int k = 0; k < n; k++){ for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ res[i][j] = min(res[i][j],res[i][k]+res[k][j]); } } } return res; } const vector>& operator[](int idx) const { return es[idx];} private: int n; vector>> es; vector vis; void init(){ es.resize(n); vis.resize(n,0); } }; } // namespace noya2 #line 77 "c.cpp" void solve(){ ll x, y, a; cin >> x >> y >> a; set st; ll ix = x; while (true){ st.insert(ix); if (ix == 0) break; ix /= a; } ll iy = y; set sy0, sy1; vector ys; while (true){ st.insert(iy); sy0.insert(iy); st.insert(iy+1); sy1.insert(iy+1); ys.emplace_back(iy); if (iy == 0) break; iy /= a; } map mp; vector vs; int n = 0; for (auto s : st){ mp[s] = n++; vs.emplace_back(s); } usefulGraph g(n); auto add = [&](ll u, ll v, ll c){ assert(c >= 0); g.add_edge(mp[u],mp[v],c,false); }; ix = x; while (ix > 0){ add(ix,ix/a,1); auto ite0 = sy0.lower_bound(ix); if (ite0 != sy0.end()){ add(ix,*ite0,*ite0-ix); } auto ite1 = sy1.upper_bound(ix); if (ite1 != sy1.begin()){ ll nxt1 = *prev(ite1); add(ix,nxt1,ix-nxt1); } ix /= a; } int siz = ys.size(); rep(i,siz){ add(ys[i],ys[i]+1,1); add(ys[i]+1,ys[i],1); if (i >= 1){ add(ys[i],ys[i-1],1+ys[i-1]-ys[i]*a); add(ys[i]+1,ys[i-1]+1,1+ys[i]*a+a-ys[i-1]-1); add(ys[i]+1,ys[i-1],ys[i-1]-ys[i]-1); } if (i >= 2){ add(ys[i],ys[i-2],1+ys[i-2]-ys[i]*a); } } out(g.dijkstra(mp[x])[mp[y]]); } int main(){ fast_io(); int t = 1; cin >> t; while(t--) solve(); }