from collections import defaultdict, deque
from itertools import product
from sys import setrecursionlimit

setrecursionlimit(100000)


def op1(arg, H):
	arg = deque(arg)
	arg.rotate(H)
	return "".join(arg)

def op2(arg, A):
	arg = deque(arg)
	for i in xrange(1, len(arg) + 1):
		if i % 2 == 0:
			c = arg[i-1]
			c = ( int(c) + A ) % 10
			arg[i-1] = str(c)
	return "".join(arg)

def dfs(elem, visited, Graph, H, A):
	node1 = op1(elem, H)
	node2 = op2(elem, A)
	Graph[elem].add(node1)
	Graph[elem].add(node2)

	nodes = Graph[elem]

	ans = min([elem, node1, node2])

	while nodes:
		item = nodes.pop()
		if item in visited:
			continue
		visited.add(item)
		node1 = op1(item, H)
		node2 = op2(item, A)
		ans = min(ans, min([item, node1, node2]))
		nodes.update([node1, node2])
	return ans

def solve():
	s = raw_input().strip()
	A, H = map(int, raw_input().strip().split())

	visited = set()
	Graph = defaultdict(set)

	print dfs(s, visited, Graph, H, A)

T = input()
for i in xrange(T):
	solve()