#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main(int argc, char const *argv[]) {
  int lines, n, m, count;
  scanf("%d", &lines);
  getchar();
  while (lines--) {
    scanf("%d %d", &n, &m);
    getchar();
    count = 0;
    while (n) {
      int n2 = n;
      while (!(n2 % m)) {
        count++;
        n2 /= m;
      }
      n--;
    }
    printf("%d\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb6(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11ff <main+0xcf>
lea    0xe86(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11eb <main+0xbb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
cmp    $0x0,%edx
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c3 <main+0x93>
jmp    11dd <main+0xad>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    11a7 <main+0x77>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1197 <main+0x67>
mov    -0x20(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
jmp    115f <main+0x2f>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xec0(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
subl   $0x1,0x8(%rsp)
jae    116c <main+0x3c>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8d(%rip),%r14
lea    0x4(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xe82(%rip),%rbx
jmp    11a9 <main+0x79>
cs nopw 0x0(%rax,%rax,1)
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
subl   $0x1,0x8(%rsp)
jb     115d <main+0x2d>
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
je     1198 <main+0x68>
xor    %ebp,%ebp
mov    0xc(%rsp),%edi
jmp    11e5 <main+0xb5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffff,%ecx
je     1190 <main+0x60>
mov    %ecx,%eax
cltd
idiv   %edi
test   %edx,%edx
jne    11e0 <main+0xb0>
mov    %ecx,%esi
add    $0x1,%ebp
mov    %esi,%eax
cltd
idiv   %edi
mov    %eax,%esi
cltd
idiv   %edi
test   %edx,%edx
je     11f0 <main+0xc0>
jmp    11e0 <main+0xb0>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%12 = call i32 (...) @getchar()
br label %13
13: ; preds = %40, %2
%14 = load i32, i32* %6
%15 = add nsw i32 %14, -1
store i32 %15, i32* %6
%16 = icmp ne i32 %14, 0
br i1 %16, label %17, label %43
17: ; preds = %13
%18 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %7, i32* %8)
%19 = call i32 (...) @getchar()
store i32 0, i32* %9
br label %20
20: ; preds = %37, %17
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = load i32, i32* %7
store i32 %24, i32* %10
br label %25
25: ; preds = %31, %23
%26 = load i32, i32* %10
%27 = load i32, i32* %8
%28 = srem i32 %26, %27
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
%34 = load i32, i32* %8
%35 = load i32, i32* %10
%36 = sdiv i32 %35, %34
store i32 %36, i32* %10
br label %25
37: ; preds = %25
%38 = load i32, i32* %7
%39 = add nsw i32 %38, -1
store i32 %39, i32* %7
br label %20
40: ; preds = %20
%41 = load i32, i32* %9
%42 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %13
43: ; preds = %13
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%10 = call i32 (...) @getchar()
%11 = load i32, i32* %3
%12 = add nsw i32 %11, -1
store i32 %12, i32* %3
%13 = icmp eq i32 %11, 0
br i1 %13, label %44, label %14
14: ; preds = %2, %38
%15 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5)
%16 = call i32 (...) @getchar()
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %38, label %19
19: ; preds = %14
%20 = load i32, i32* %5
br label %21
21: ; preds = %19, %33
%22 = phi i32 [ 0, %19 ], [ %34, %33 ]
%23 = phi i32 [ %17, %19 ], [ %35, %33 ]
%24 = srem i32 %23, %20
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %21, %26
%27 = phi i32 [ %30, %26 ], [ %23, %21 ]
%28 = phi i32 [ %29, %26 ], [ %22, %21 ]
%29 = add nsw i32 %28, 1
%30 = sdiv i32 %27, %20
%31 = srem i32 %30, %20
%32 = icmp eq i32 %31, 0
br i1 %32, label %26, label %33
33: ; preds = %26, %21
%34 = phi i32 [ %22, %21 ], [ %29, %26 ]
%35 = add nsw i32 %23, -1
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %21
37: ; preds = %33
store i32 0, i32* %4
br label %38
38: ; preds = %37, %14
%39 = phi i32 [ %34, %37 ], [ 0, %14 ]
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %3
%42 = add nsw i32 %41, -1
store i32 %42, i32* %3
%43 = icmp eq i32 %41, 0
br i1 %43, label %44, label %14
44: ; preds = %38, %2
ret i32 0
}