geeky-alice-and-naughty-bob

One day Alice was experimenting with the numbers to make new algorithms. He introduce a new term Rsum.
Rsum of any number is defined as number obtained by iterative summing of digits of the given number until single digit number is obtained. 
For example: 
365 --> (3+6+5) = 14
 14  --> 1+4 = 5  
Rsum(365)=5   

Naughty Bob came in the room of Alice in his absence and change all the numbers on which Alice was experimenting by their corresponding factorials. For example Bob will change 3 with  6 (3!=321). Consider 0!=0.

When Alice start experimenting without knowing that the numbers are now changed. He got some ambiguous results and unable to find some pattern for his algorithm.  So he wants your help. You are given a range [A,B] of actual numbers and your task is to find sum of all Rsum values in range [A,B] of new changed numbers.

Input:
First line consist of T test cases. Next T line contain two integers a,b in each line.  

Output:
Find Sum of Rsum of all numbers in given range [A,B].  

Constraints:
1 ≤ T ≤ 10^5
0 ≤ A ≤ B ≤ 10^6 

SAMPLE INPUT
2
1 3
3 4

SAMPLE OUTPUT
9
12

Explanation

Test case 1:
1!=1 and Rsum(1)=1,
2!=2 and Rsum(2)=2,
3!=6 and Rsum(6)=6.
So the sum of Rsums is 1+2+6=9.

Similarly for Test case 2.

Source: hackerearth | Difficulty: MEDIUM
Tags: Mathematics


Solutions:
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
print 'Hello World!'
'''

cases = int(input())

for case in range(0,cases):
	low , high = list(map(int,input().split()))
	if low < 6 and high < 6 :
		sum = 0
		for num in range(low,high+1):
			if num == 0:
				sum = sum + 0
			if num == 1:
				sum = sum + 1
			if num == 2:
				sum = sum + 2
			if num == 3:
				sum = sum + 6
			if num == 4:
				sum = sum + 6
			if num == 5:
				sum = sum + 3
		print(sum)
	
	if low < 6 and high >= 6 :
		sum = 0
		for num in range(low,6):
			if num == 0:
				sum = sum + 0
			if num == 1:
				sum = sum + 1
			if num == 2:
				sum = sum + 2
			if num == 3:
				sum = sum + 6
			if num == 4:
				sum = sum + 6
			if num == 5:
				sum = sum + 3
		sum = sum + (high - 6 + 1)*9
		print(sum)
	
	if low >= 6 and high >= 6 :
		print((high - low + 1)*9)
	


t=eval(input())
x1=[]
for i in range(t):
	t1=list(map(int,input().strip().split(" ")))
	x1.append(t1)
for i1 in x1:
		t2,tmp=0,0
		i2,h=i1[0],i1[1]
		if i2>5:
				print(9*(h+1-i2))
		elif i2<=5:
			if i2==0 and h!=0:
					 t2,tmp,i2=0,1,i2+1
			while (i2<h+1):
					 
					 if i2!=0 and i2<3:
						 #i2=tmp*i2
						 t2,tmp =t2+i2,tmp+1
					 
					 elif i2==3 or i2==4:
					 	  t2 ,tmp=t2+6,tmp+1
					 	  
					 elif i2==5:
					 	   t2,tmp=t2+3,tmp+1
					 elif i2>=6:
						 t2 =t2+ 9*(i1[1]+1-i1[0]-tmp)
						 break;
					 
					 elif i2>=6:   
							t2 +=9*(i1[1]+1-i1[0])
							break;
					 i2 +=1
			print(t2)  
	


T = input("")
T = int(T)
for i in range(T):
	ab = input("")
	ab = ab.split()
	a = int(ab[0])
	b = int(ab[1])
	count = 0
	if(a > 5):
		count = (b + 1 - a) * 9
	elif(a <= 5 and b >5):
		if a < 2:
			count = 18
		elif a == 2:
			count = 17
		elif a == 3:
			count = 15
		elif a == 4:
			count = 9
		elif a== 5:
			count = 3
		count = count + (b + 1 - 6) * 9
	else:
		test = "012663"
		for j in range(a, b + 1):
			count = count + int(test[j])
	print(count)