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.
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
import java.util.Map.Entry;



public class Main 
{
    
    public static void main(String[] args) throws Exception
    {
        /*
        FileWriter writer = new FileWriter("input.txt");
        for(int i=1;i<=10000000;++i)
        {
            long a = Mother_Class.getRandomInteger(1, 100000000);
            long b = Mother_Class.getRandomInteger(1, 100000000);
            writer.write(a+" "+b+"\n");
        }        
        writer.close();*/
        
        //long start = System.currentTimeMillis();
        
        Reader r = new Reader();
        Print p = new Print();
        int test;
        long n, k;
        test = r.rint();
        while(test-- > 0)
        {
            n = r.rlong(); k = r.rlong();
            long step = 0;
            while(n>0)
            {
                if(n%k==0) { n /= k; ++step; }
                else 
                { 
                    long y = n/k;
                    long x = y*k;
                    step += Math.abs(n-x);
                    n = x;
                }
            }
            p.println(step);
        }
        
        //long end = System.currentTimeMillis();
        //p.println("Execution Time: "+(end-start)+"ms");
        p.close();
    }
    
    
}

class Pair
{
    public int first;
    public int second;

    public Pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString()
    {
        return "{"+first+", "+second+"}";
    }

    
}

class CustomCompare implements Comparator<Pair>
{

    @Override
    public int compare(Pair o1, Pair o2) 
    {
        return o2.first - o1.first;
    }
    
}

class Triplet
{
    public int first;
    public int second;
    public int third;

    public Triplet(int first, int second, int third)
    {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    @Override
    public String toString()
    {
        return "{"+first+", "+second+", "+third+"}";
    }
}



class Reader 
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String rstren() throws IOException
    {
        byte[] buf = new byte[500000]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1) 
        {
            if (c=='\n' || c==13) 
            {
                if (cnt != 0) 
                {
                    break;
                }
                else 
                {
                    continue;
                }
            }
            buf[cnt++] = (byte)c;
        }
        return new String(buf, 0, cnt);
    }

    public int rint() throws IOException
    {
        int ret = 0;
        byte c = read();
        while ((c <= 47 || c>=58) && c!=45) 
        {
            c = read();
        }
        boolean neg = (c == '-');
        if (neg) { c = read(); }
        do 
        {
            ret = ret * 10 + c - '0';
        } 
        while ((c = read()) >= '0' && c <= '9');

        if (neg) { return -ret; }
        return ret;
    }

    public long rlong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while ((c <= 47 || c>=58) && c!=45) { c = read(); }
        boolean neg = (c == '-');
        if (neg) { c = read(); }
        do 
        {
            ret = ret * 10 + c - '0';
        } 
        while ((c = read()) >= '0' && c <= '9');
        if (neg) { return -ret; }
        return ret;
    }

    public double rdouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while ((c <= 47 || c>=58) && c!=45) { c = read(); }
        boolean neg = (c == '-');
        if (neg) { c = read(); }
        do 
        {
            ret = ret * 10 + c - '0';
        } 
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.') 
        {
            while ((c = read()) >= '0' && c <= '9') 
            {
                ret += (c - '0') / (div *= 10);
            }
        }
        if (neg) { return -ret; }
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) { buffer[0] = -1; }
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead) { fillBuffer(); }
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null) { return; }
        din.close();
    }
}

class Print
{
    private final BufferedWriter bw;
    public Print()
    {
        this.bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public void print(Object object)throws IOException
    {
        bw.append(""+object);
    }
    public void println(Object object)throws IOException
    {
        print(object);
        bw.append("\n");
    }
    public void close()throws IOException
    {
        bw.close();
    }
}
