# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem #1 – Junior Division
# Author :  Aashray	Rajagopalan
# Language: Python 3.x
# -----------------------------------------------------------------------------------------

##########################################################################################################
#                                  OVMS CODING CLUB 2019                                                 #
#                                      ACSL Contest#1                                                    #
#                                      Student ID#914                                                    #
##########################################################################################################
# Problem
# Given a positive integer (call it N), a position in that integer (call it P), and a
# transition integer (call it D). Transform N as follows:
#
# ● If the Pth digit of N from the right is from 0 to 4, add D to it. Replace the Pth digit by the
# units digit of the sum. Then, replace all digits to the right of the Pth digit by 0.
#
# ● If the Pth digit of N from the right is from 5 to 9, subtract D from it. Replace the Pth
# digit by the leftmost digit of the absolute value of the difference. Then, replace all digits to the
# right of the Pth digit by 0.
##########################################################################################################

#-----------------------------------------------
# Main Function to solve the problem
# This function transforms the number
# To find the digit to transform, we use the index as len(N) - P, since P is position from the right
# Then check if the digit is between 0 and 4 or between 5 and 9
def transform(N,P,D):
    if P<=0:
        return ('0 and negative does not work')
    else:
        if ((0<= int(N[len(N)-P])) and (int(N[len(N)-P]) <=4)):
            # get the transformation sum and get it as string
            X=str(int(N[(len(N)-P)])+D)
            # Use only the digit in the ones place - from the string get the rightmost digit.
            replace_digit=X[len(X) - 1]
        else:
            # get the transformation by subtracting D from the digit at the index
            X=str(abs(int(N[(len(N)-P)])-D))
            # Use the leftmost digit - from the string get the first index
            replace_digit=X[0]
        N_transformed=N[:(len(N)-P)] + replace_digit + ('0'*(P-1))
        return N_transformed


#-----------------------------------------------
# Auxiliary code for reading input and writing output
# this part of the code is reading the input called ovcc_grader.
# Next it is clearing the out put file to be empty
# The for loop is used to work on all lines in input.txt

input = 'input.txt'
output = 'output.txt'

def readinput():
  words=[]
  with open(input) as f:
    lines = f.readlines()
    for line in lines:
      if line.split()!=[]:
        words.append(line.split())
  for j in range (0,len(words)):
    for i in range(0,len(words[j])):
      words[j][i]=int(words[j][i])
  return words


def writeoutput(line):
  with open(output,'a') as f:
     f.write(str(line)+'\n')

def clearoutput():
  f=open(output,'w')
  f.write('')

allinputs = readinput()

clearoutput()
for line in allinputs:
    N = line[0]
    P = line[1]
    D = line[2]
    out = transform(str(N), P, D)
    writeoutput(out)