• + 0 comments

    Python3 solution

    def ok(p, L):
        sum = 0
        while L > 0:
            sum += L % p
            L //= p
        return sum % p
    
    def solve(p, L):
        L1 = L // p
        r = p - ok(p, L1)
        if r == p:
            r = 0
        if r < L % p:
            return L1 + 1
        else:
            return L1
    
    T = int(input())
    for t in range(T):
        p, L = map(int, input().split())
        L += 1
        L1 = L // p
        ans = 0
        if ok(p, L1) == 0:
            ans += L % p
        print(ans + solve(p, L1) * p - 1)