Problem: Unfortunately someone has come and eaten the problem statement. Are you good enough to solve it without the statement?

Input
The first line contains T denoting the number of test cases.
The next  T lines describe test cases and contain two integers each: N and M.

Output
For each test case output one integer - answer for the question.

Constraints
T ≤ 1000
1 ≤ N, M ≤ 10^9

Note:
N, M ≤ 1000 for 30% of the test

SAMPLE INPUT
6
28 1
39 1
90 1
15 2
123 2
114514 1919

SAMPLE OUTPUT
68
90
81
40
17
16

Solution (in PYTHON):

Solution:
#!/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())

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

cache = {}

def do(a):
    if a not in cache:
        ans = 0
        for d in map(int, str(a)):
            ans += d ** 2
        cache[a] = ans
        return ans
    return cache[a]

mod = 10**7
t = read_int()
for i in xrange(t):
    a, b = read_ints()
    cache = {}
    m = {a: 0}
    did = 0

    i = 0
    while i < b:
        # print "Q", i, a, "->", do(a)
        a = do(a)
        i += 1
        if not did and a in m:
            did = 1
            cyc = i - m[a]
            left = b - i
            left %= cyc
            # print a, m[a], b, i, left, cyc
            i = b - left
            # continue
        else:
            m[a] = i
    print a