p, q, r, K = map(int, input().split()) def matrix(a, b): ans = [[0]*len(b[0]) for _ in range(len(a))] for i in range(len(a)): for j in range(len(b[0])): ans[i][j] = sum(a[i][k]*b[k][j] for k in range(len(b)))%10 return ans dp = [[[0]*3 for _ in range(3)]] for i in range(3): dp[0][0][i] = 1 dp[0][1][0] = 1 dp[0][2][1] = 1 for _ in range(59): dp.append(matrix(dp[-1], dp[-1])) K -= 3 ansM = [[r], [q], [p]] for i in range(60): if 1<