Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Eugene loves sequences, especially arithmetic progressions. One day he was asked to solve a difficult problem.

If a sequence of numbers A_{1}, A_{2}, ... , A_{N} form an arithmetic progression A, he was asked to calculate sum of F(A_{i}), for L ≤ i ≤ R.
F(X) is defined as:

If X < 10 then F(X) = X.

Else F(X) = F(sum_{of}_digits(X)).

Example:

F(1378) = 
F(1+3+7+8) = 
F(19) = 
F(1 + 9) = 
F(10) = 
F(1+0) = 
F(1) = 1

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases.
Each test case is described in one line containing four integers: A_{1} denoting the first element of the arithmetic progression A, D denoting the common difference between successive members of A, and L and R as described in the problem statement.

------ Output ------ 

For each test case, output a single line containing one integer denoting sum of F(A_{i}).

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ A_{1} ≤ 10^{9}$
$0 ≤ D ≤ 10^{9}$
$1 ≤ R ≤ 10^{18}$
$1 ≤ L ≤ R$

------ Subtasks ------ 

$Subtask 1:  0 ≤ D ≤ 100, 1 ≤ A_{1} ≤ 10^{9}, 1 ≤ R ≤ 100  - 15 points$

$Subtask 2:  0 ≤ D ≤ 10^{9}, 1 ≤ A_{1} ≤ 10^{9}, 1 ≤ R ≤ 10^{6}  - 25 points$

$Subtask 3: Original constraints - 60 points$

----- Sample Input 1 ------ 
2

1 1 1 3

14 7 2 4
----- Sample Output 1 ------ 
6

12
----- explanation 1 ------ 
Example case 1.
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...}
A1 = 1
A2 = 2
A3 = 3
F(A1) = 1
F(A2) = 2
F(A3) = 3
1+2+3=6

Example case 2.
A = {14, 21, 28, 35, 42, 49, 56, 63, 70, 77,  ...}
A2 = 21
A3 = 28
A4 = 35
F(A2) = 3
F(A3) = 1
F(A4) = 8
3+1+8=12

Difficulty: MEDIUM_HARD

Solutions:
def S(n):
	if n % 9 == 0:
		return 9
	else:
		return n % 9
for _ in range(int(input())):
	(a, d, l, r) = map(int, input().split())
	e = a
	s = 0
	for i in range(9):
		s += S(e)
		e += d
	t = r - l + 1
	u = t // 9
	l2 = u * 9
	l1 = u * s
	v = t % 9
	e = a + (l + l2 - 1) * d
	s1 = 0
	for i in range(v):
		s1 += S(e)
		e += d
	ans = s1 + l1
	print(ans)


for i in range(int(input())):
	(A, D, L, R) = map(int, input().split())
	(ans, val) = (0, 0)
	D %= 9
	A = (A + (L - 1) * D) % 9
	if not A:
		A = 9
	if not D:
		D = 9
	R = R - L
	for j in range(R % 9 + 1):
		if not (A + j * D) % 9:
			ans += 9
		else:
			ans += (A + j * D) % 9
	for j in range(9):
		if not (A + j * D) % 9:
			val += 9
		else:
			val += (A + j * D) % 9
	ans += R // 9 * val
	print(ans)


T = int(input())
while T > 0:
	inp = input()
	(a, d, l, r) = list(map(int, inp.split()))
	sums = [0 for _ in range(10)]
	temp = a + (l - 1) * d
	lim = r - l + 1
	rem = lim % 9
	sums[1] = 1 + (temp - 1) % 9
	for k in range(2, 10):
		temp += d
		sums[k] = sums[k - 1] + (1 + (temp - 1) % 9)
	if lim == rem:
		print(sums[rem])
	else:
		sums[9] *= lim // 9
		sums[9] += sums[rem]
		print(sums[9])
	T -= 1


__author__ = 'masterchief'

def solve():
	num_test = int(input())
	for i in range(num_test):
		test_case()

def test_case():
	(a_1, d, l, r) = [int(i) for i in input().split()]
	a_L = f(a_1 + (l - 1) * d)
	d = f(d)
	s2 = 0
	for i in range((r - l + 1) % 9):
		a_temp = f(a_L + i * d)
		s2 += a_temp
	terms_9 = (r - l + 1) // 9
	if terms_9 != 0:
		for i in range(9):
			a_temp = terms_9 * f(a_L + i * d)
			s2 += a_temp
	print(s2)

def f(num):
	return (num - 1) % 9 + 1
solve()


def F(x):
	return (x - 1) % 9 + 1
for _ in range(int(input())):
	(A, D, L, R) = map(int, input().split())
	L = L - 1
	R = R - 1
	sum_per9 = 0
	for i in range(0, 9):
		sum_per9 += F(A + D * i)
	ans = 0
	while (R - L + 1) % 9 != 0:
		ans += F(A + D * R)
		R -= 1
	ans += sum_per9 * (R - L + 1) // 9
	print(ans)


import sys

def F(x):
	return (x - 1) % 9 + 1
T = int(sys.stdin.readline())
for t in range(T):
	inp = sys.stdin.readline().split()
	(A, D, L, R) = (int(inp[0]), int(inp[1]), int(inp[2]) - 1, int(inp[3]) - 1)
	sum_per9 = 0
	for i in range(1, 10):
		sum_per9 += F(A + D * i)
	ans = 0
	while (R - L + 1) % 9 != 0:
		ans += F(A + D * R)
		R -= 1
	ans += sum_per9 * (R - L + 1) // 9
	sys.stdout.write(str(ans) + '\n')


import sys

def F(x):
	return (x - 1) % 9 + 1
T = int(input())
for t in range(T):
	inp = input().split()
	(A, D, L, R) = (int(inp[0]), int(inp[1]), int(inp[2]) - 1, int(inp[3]) - 1)
	sum_per9 = 0
	for i in range(1, 10):
		sum_per9 += F(A + D * i)
	ans = 0
	while (R - L + 1) % 9 != 0:
		ans += F(A + D * R)
		R -= 1
	ans += sum_per9 * (R - L + 1) // 9
	print(ans)


import sys
from array import array
input = sys.stdin.readline
out = array('q')
for _ in range(int(input())):
	(a1, d, l, r) = map(int, input().split())
	vis = array('b')
	(cur, sum_, period) = ((a1 - 1) % 9 + 1, 0, 0)
	while cur not in vis:
		vis.append(cur)
		period += 1
		sum_ += cur
		a1 += d
		cur = (a1 - 1) % 9 + 1
	(divr, modr) = divmod(r, period)
	(divl, modl) = divmod(l - 1, period)
	out.append(sum_ * divr + sum(vis[:modr]) - sum_ * divl - sum(vis[:modl]))
print('\n'.join(map(str, out)))


x = input()
x = int(x)
for j in range(x):
	(a, d, l, r) = input().split()
	a = int(a)
	d = int(d)
	l = int(l)
	r = int(r)

	def fun(n):
		return (n - 1) % 9 + 1
	m = 0
	for i in range(9):
		m = m + fun(a + (i + 1) * d)
	s = 0
	if (r - l + 1) % 9 != 0:
		for j in range((l - 1) % 9 + 1, (l - 1) % 9 + (r - l) % 9 + 2):
			s = s + fun(a + (j - 1) * d)
	fs = m * ((r - l + 1) // 9) + s
	print(fs)


def func(n):
	strn = str(n)
	sum = 0
	for digit in strn:
		sum += int(digit)
	if sum < 10:
		return sum
	else:
		return func(sum)

def func2(n):
	return 1 + (n - 1) % 9
test = int(input())
for it in range(test):
	(a, d, l, r) = [int(x) for x in input().strip().split()]
	a = a + (l - 1) * d
	a -= 1
	r = r - l + 1
	sumo = r
	div = sumo // 9
	rem = sumo % 9
	st = a
	termsum = 0
	for i in range(9):
		termsum = termsum + st % 9
		st = (st + d) % 9
	sumo = sumo + termsum * div
	st = a
	for i in range(rem):
		sumo = sumo + st % 9
		st = (st + d) % 9
	print(sumo)


def m(x):
	mx = x % 9
	if mx == 0:
		mx = 9
	return mx
tc = int(input(''))
while tc > 0:
	tc -= 1
	(A, D, L, R) = map(int, input('').split())
	N = R - L + 1
	AL = m(A + (L - 1) * D)
	s = [AL]
	ss = AL
	c = m(AL + D)
	while c != s[0]:
		s.append(c)
		ss += c
		c = m(c + D)
	ans = ss * (N // len(s))
	r = N % len(s)
	for i in range(r):
		ans += s[i]
	print(ans)


def summ(a):
	if a % 9 == 0:
		return 9
	else:
		return a % 9

def main():
	t = int(input())
	for aa in range(t):
		(a1, d, l, r) = map(int, input().split())
		i = 0
		ans = 0
		sum1 = 0
		a = [0] * 9
		a1 = summ(a1)
		d = summ(d)
		z = 0
		a[z] = summ(a1)
		for z in range(1, 9):
			a[z] = summ(a1 + summ(z * d)) + a[z - 1]
		sum1 = a[8]
		l = l - 1
		j = l % 9
		i = r % 9
		p = a[i - 1]
		q = a[j - 1]
		if i == 0:
			p = 0
		if j == 0:
			q = 0
		ans = r // 9 * sum1 + p
		xx = l // 9 * sum1 + q
		ans = ans - xx
		print(ans)
main()


T = int(input())
for tn in range(T):
	(first, delta, l, r) = [int(x) for x in input().split()]
	res = []
	ressum = 0
	for i in range(9):
		now = (first + delta * i) % 9
		if now == 0:
			res.append(9)
		else:
			res.append(now)
		ressum = ressum + res[i]
	ans = (r - l + 1) // 9 * ressum
	count = (r - l + 1) % 9
	for i in range(count - 1, -1, -1):
		ans = ans + res[(r - i - 1) % 9]
	print(ans)


def f(A, D, i):
	return (A + i * D - 1) % 9 + 1
for i in range(int(input())):
	suma = 0
	(A, D, L, R) = tuple(map(int, input().split()))
	suma += (R - L) // 9 * sum((f(A, D, i) for i in range(L, L + 9)))
	L += (R - L) // 9 * 9
	D %= 9
	for i in range(L - 1, R):
		suma += (A + i * D - 1) % 9 + 1
	print(suma)