
# Author: Travis Meade

import sys
input = sys.stdin.readline
ints = lambda a : map(int, a.split())

# Function to find the sum of the digits of some input number
def digSum(x):
    return 0 if not x else (digSum(x//10)+x%10)

# Read in the number of cases
cc, = ints(input())

# Handle each case
for _ in range(cc):
    # Read in the target and the skip
    a,b = ints(input())

    # We have to count to a multiple of the skip (a%b)
    # We can then use the skip as a shortcut greedily taking largest powers
    # of 10; this process involves summing the digits of the target when
    # divided by the skip value
    print(a%b + digSum(a//b))

