a=int(input()) d=[[0,8],[1,7,4,3,9,8],[2,8],[3,9,8],[4,9,8],[5,6,9,8],[6,8],[7,9,8],[8],[9]] e=[[0,1],[0,1,2,3,4,5],[0,2],[0,1,2],[0,2,3],[0,1,1,2],[0,1],[0,2,4],[0],[0]]
x=""
for i in range(a): b,c=map(int,input().split()) for i in range(len(str(b))): e[int(str(b)[i])].append(c+1) cm=d[int(str(b)[i])][0:e[int(str(b)[i])].index(c+1)] x+=str(max(cm)) c-=e[int(str(b[i]))][d[int(str(b[i]))].index(max(cm))] e[int(str(b[i]))].insert(c+1) print(x) x=""