#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys, math, random, operator
from string import ascii_lowercase
from string import ascii_uppercase
from fractions import Fraction, gcd
from decimal import Decimal, getcontext
from itertools import product, permutations, combinations
from Queue import Queue, PriorityQueue
from collections import deque, defaultdict, Counter
getcontext().prec = 100

MOD = 10**9 + 7
INF = float("+inf")

if sys.subversion[0] != "CPython":  # PyPy?
    raw_input = lambda: sys.stdin.readline().rstrip()
pr = lambda *args: sys.stdout.write(" ".join(str(x) for x in args) + "\n")
epr = lambda *args: sys.stderr.write(" ".join(str(x) for x in args) + "\n")
die = lambda *args: pr(*args) ^ exit(0)

read_str = raw_input
read_strs = lambda: raw_input().split()
read_int = lambda: int(raw_input())
read_ints = lambda: map(int, raw_input().split())
read_float = lambda: float(raw_input())
read_floats = lambda: map(float, raw_input().split())

"---------------------------------------------------------------"

def add(s, a):
    return tuple( (d + (a * (i % 2))) % 10 for i, d in enumerate(s) )

def rot(s, n):
    n %= len(s)
    return s[-n:] + s[:-n]

t = read_int()
for t in xrange(t):
    s = tuple(map(int, read_str()))
    a, h = read_ints()
    pq = PriorityQueue()
    pq.put(s)
    ans = s
    seen = {s}
    while not pq.empty():
        t = pq.get()
        t1 = rot(t, h)
        t2 = add(t, a)
        # print t, "->", t1, t2, "a", a, "h", h
        good = 0
        for t in (t1, t2):
            if t not in seen:
                seen.add(t)
                pq.put(t)
                ans = min(ans, t)
                if ans == (0,) * len(s):
                    good = 1
                    break
        if good:
            break
    print "".join(map(str, ans))