# Anthony Li
# Bergen County Academies
# Intermediate Division

from math import floor, log10

def get_digit(n: int, k: int) -> int:
    return floor(n / (10 ** k)) % 10

def set_digit(n: int, k: int, t: int) -> int:
    return (n - get_digit(n, k) * (10 ** k)) + t * (10 ** k)

def solve(n: int, p: int) -> int:
    d = get_digit(n, p - 1)
    num_digits = floor(log10(n))
    result = n
    for i in range(p - 1):
        result = set_digit(result, i, abs(get_digit(result, i) - d) % 10)
    
    for i in range(p, num_digits + 1):
        result = set_digit(result, i, (get_digit(result, i) + d) % 10)

    return result

file = open("int-sample-input.txt", "r")
for i in range(5):
    line = file.readline().split(" ")
    print(solve(int(line[0]), int(line[1])))

file.close()