You are given an integer n and an integer k.

In one step you can do one of the following moves: 

  * decrease n by 1; 
  * divide n by k if n is divisible by k. 



For example, if n = 27 and k = 3 you can do the following steps: 27 → 26 → 25 → 24 → 8 → 7 → 6 → 2 → 1 → 0.

You are asked to calculate the minimum number of steps to reach 0 from n. 

Input

The first line contains one integer t (1 ≤ t ≤ 100) — the number of queries.

The only line of each query contains two integers n and k (1 ≤ n ≤ 10^{18}, 2 ≤ k ≤ 10^{18}).

Output

For each query print the minimum number of steps to reach 0 from n in single line. 

Example

Input


2
59 3
1000000000000000000 10


Output


8
19

Note

Steps for the first test case are: 59 → 58 → 57 → 19 → 18 → 6 → 2 → 1 → 0.

In the second test case you have to divide n by k 18 times and then decrease n by 1.
//package com.company;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }

    static void sieve(LinkedHashMap<Integer,Integer> primes) {
        boolean mark[] = new boolean[100001];

        Arrays.fill(mark,true);

        for(int p=2;p*p<100001;p++) {
            if(mark[p]) {
                for(int i=p*2;i<100001;i+=p) {
                    mark[i] = false;
                }
            }
        }

        for(int p=2;p<100001;p++) {
            if(mark[p]) {
                primes.put(p,1);
            }
        }
    }

    public static void main(String[] args) {
        InputReader in  = new InputReader(System.in);


        int t = in.readInt();

        while(t != 0) {
            long n = in.readLong();
            long k = in.readLong();

            long ans = 0 ;
            while(n != 0) {
                if(n%k == 0) {
                    n = n/k;
                    ans++;
                }
                else {
                    ans += n % k;
                    n = n - n % k;
                }
            }

            System.out.println(ans);
            t--;
        }



    }


    static int lowerBound(int a[],int n,long x) {
        int lo = 0;
        int hi = n;

        while(lo < hi) {
           int mid = (lo + hi)/2;
           if(a[mid] >= x) {
               hi = mid;
           }
           else {
               lo = mid + 1;
           }
        }

        return lo;
    }


}