{"trustable":false,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eYou are given an integer $$$n$$$ and an integer $$$k$$$.\u003c/p\u003e\n\u003cp\u003eIn one step you can do one of the following moves:\u003c/p\u003e\n\u003cul\u003e\n \u003cli\u003edecrease $$$n$$$ by $$$1$$$;\u003c/li\u003e\n \u003cli\u003edivide $$$n$$$ by $$$k$$$ if $$$n$$$ is divisible by $$$k$$$.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eFor example, if $$$n \u003d 27$$$ and $$$k \u003d 3$$$ you can do the following steps: $$$27 \\rightarrow 26 \\rightarrow 25 \\rightarrow 24 \\rightarrow 8 \\rightarrow 7 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$$$.\u003c/p\u003e\n\u003cp\u003eYou are asked to calculate the minimum number of steps to reach $$$0$$$ from $$$n$$$.\u003c/p\u003e\n先输入一个 t(1≤t≤100),代表t组样例,接下来t行每行输入一个n and k(1≤n≤1e18,,2≤k≤1e18) 需要你一步一步的把当前的n变成0,每步能进行以下操作:\u003c/p\u003e\n1、当前数减一 (n\u003dn-1)\n\u003c/p\u003e\n2、如果当前数能够整除k(n%k\u003d\u003d0),就可以除k(n\u003dn/k)\u003c/p\u003e\n\n问你最少需要多少步把n变成0\u003c/p\u003e\n\n比如n \u003d 27 , k \u003d 3\u003c/p\u003e\n\n我可以 27 -\u003e 26 -\u003e 25 -\u003e 24 -\u003e 8 -\u003e 7 -\u003e 6 -\u003e 2 -\u003e 1 -\u003e 0"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of queries.\u003c/p\u003e\n\u003cp\u003eThe only line of each query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^{18}$$$, $$$2 \\le k \\le 10^{18}$$$).\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each query print the minimum number of steps to reach $$$0$$$ from $$$n$$$ in single line.\u003c/p\u003e"}},{"title":"Sample 1","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2\n59 3\n1000000000000000000 10\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e8\n19\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eSteps for the first test case are: $$$59 \\rightarrow 58 \\rightarrow 57 \\rightarrow 19 \\rightarrow 18 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$$$.\u003c/p\u003e\n\u003cp\u003eIn the second test case you have to divide $$$n$$$ by $$$k$$$ $$$18$$$ times and then decrease $$$n$$$ by $$$1$$$.\u003c/p\u003e"}}]}