T=int(raw_input())
for iCase in xrange(T):
  S=raw_input()
  lenS=len(S)
  A,H=map(int, raw_input().split())
  H%=lenS 
  allstr=set([])
  news=[S]
  while news:
    old=news
    news=[]
    for st in old:
      S1=st[-H:]+st[:-H]
      if S1 not in allstr:
        allstr.add(S1)
        news.append(S1)
      S2=st[0]
      for i in xrange(1,lenS,2):
        S2+=str((int(st[i])+A)%10)
        if i+1<lenS:
          S2+=st[i+1]
      if S2 not in allstr:
        allstr.add(S2)
        news.append(S2)
  print min(allstr)