Let $length(A)$ denote the count of digits of a number $\mbox{A}$ in its decimal representation. 

John is looking for new methods of determining which numbers are strange all day long. 

All non-negative numbers of length 1 are strange. Further, a number $\mbox{X}$ with $length(X)\geq1$ can also be considered strange if and only if  

$\mbox{X}$ is evenly divisible by $length(X)$  
the number $X/length(X)$ is recursively strange

Your task is to calculate how many strange numbers belong to an interval $[L,R]$.  

Input Format 

The first line contains single integer $\mathbf{T}$ - the number of test cases. Next $\mathbf{T}$ lines contain two integers separated by single space $\boldsymbol{\mbox{L}}$ and $\mbox{R}$.  

Output Format 

In $\mathbf{T}$ lines, print $\mathbf{T}$ integers - count of strange numbers belonging to the interval $[L,R]$.

Constraints 

$1\leq T\leq200$ 

$0\leq L<R\leq10^{18}$  

Sample Input

5
7 25
45 50
1 100
99 103
0 1000000

Sample Output

10
1
26
0
96

Explanation 

First testcase: There are $10$ strange numbers that belong to the interval $[7,25]$. They are $7,8,9,10,12,14,16,18,20,24$. 

Second testcase: Only $48$ satisfies the given constraints.

Source: hackerrank | Difficulty: MEDIUM_HARD
Tags: Number theory


Solutions:
import math
import bisect
_l = [n for n in range(10)]
for i in range(2, 19):
	j = 1
	while j < len(_l):
		if i == int(math.log10(_l[j] * i)) + 1:
			_l.append(_l[j] * i)
		j += 1

def s(n):
	return bisect.bisect(_l, n)

def si(l, r):
	return s(R) - s(L - 1) if l else s(R)
T = int(input())
for _ in range(T):
	(L, R) = map(int, input().split())
	print(si(L, R))


import bisect
pow10 = [10 ** i for i in range(20)]

def islength(n, i):
	return n >= pow10[i - 1] and n < pow10[i]
S = set()
s = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
while s:
	s_ = set()
	for ss in s:
		S.add(ss)
		(n, i) = (ss << 1, 2)
		while i <= 18 and (not islength(n, i)):
			n += ss
			i += 1
		while i <= 18 and islength(n, i):
			s_.add(n)
			n += ss
			i += 1
	s = s_
S = sorted(S)
for _ in range(int(input())):
	(L, R) = (int(_) for _ in input().split())
	print(bisect.bisect(S, R) - bisect.bisect_left(S, L))


import sys
data = [map(int, line.strip().split(' ')) for line in sys.stdin.readlines()[1:]]
strange = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 96, 108, 120, 144, 168, 192, 216, 240, 288, 324, 360, 432, 504, 576, 648, 720, 864, 972, 1152, 1296, 1440, 1728, 2016, 2304, 2592, 2880, 3456, 3888, 4608, 5184, 5760, 6912, 8064, 9216, 10080, 11520, 12960, 14400, 17280, 19440, 23040, 25920, 28800, 34560, 40320, 46080, 50400, 57600, 64800, 72000, 86400, 97200, 103680, 116640, 138240, 155520, 172800, 207360, 241920, 276480, 302400, 345600, 388800, 432000, 518400, 583200, 622080, 699840, 829440, 933120, 1088640, 1209600, 1451520, 1693440, 1935360, 2116800, 2419200, 2721600, 3024000, 3628800, 4082400, 4354560, 4898880, 5806080, 6531840, 7620480, 8467200, 11612160, 13547520, 15482880, 16934400, 19353600, 21772800, 24192000, 29030400, 32659200, 34836480, 39191040, 46448640, 52254720, 60963840, 67737600, 92897280, 104509440, 121927680, 139345920, 152409600, 174182400, 195955200, 217728000, 261273600, 293932800, 313528320, 352719360, 418037760, 470292480, 548674560, 609638400, 836075520, 940584960, 1045094400, 1219276800, 1393459200, 1524096000, 1741824000, 1959552000, 2177280000, 2612736000, 2939328000, 3135283200, 3527193600, 4180377600, 4702924800, 5486745600, 6096384000, 8360755200, 9405849600, 10346434560, 11496038400, 13412044800, 15328051200, 16765056000, 19160064000, 21555072000, 23950080000, 28740096000, 32332608000, 34488115200, 38799129600, 45984153600, 51732172800, 60354201600, 67060224000, 91968307200, 100329062400, 112870195200, 124157214720, 137952460800, 160944537600, 183936614400, 201180672000, 229920768000, 258660864000, 287400960000, 344881152000, 387991296000, 413857382400, 465589555200, 551809843200, 620786073600, 724250419200, 804722688000, 1195587993600, 1304277811200, 1467312537600, 1614043791360, 1793381990400, 2092278988800, 2391175987200, 2615348736000, 2988969984000, 3362591232000, 3736212480000, 4483454976000, 5043886848000, 5380145971200, 6052664217600, 7173527961600, 8070218956800, 9415255449600, 10139505868800, 11266117632000, 16738231910400, 18259889356800, 20542375526400, 22596613079040, 25107347865600, 29291905843200, 33476463820800, 36614882304000, 41845579776000, 47076277248000, 52306974720000, 62768369664000, 70614415872000, 75322043596800, 84737299046400, 107602919424000, 121053284352000, 141228831744000, 152092588032000, 168991764480000, 251073478656000, 273898340352000, 308135632896000, 338949196185600, 376610217984000, 439378587648000, 502146957312000, 549223234560000, 627683696640000, 706144158720000, 784604620800000, 941525544960000, 1004293914624000, 1129830653952000, 1205152697548800, 1355796784742400, 1721646710784000, 1936852549632000, 2259661307904000, 2433481408512000, 2703868231680000, 4017175658496000, 4382373445632000, 4930170126336000, 5423187138969600, 6025763487744000, 7030057402368000, 8034351316992000, 8787571752960000, 10670622842880000, 12004450698240000, 13338278553600000, 16005934264320000, 17072996548608000, 19207121117184000, 20487595858329600, 23048545340620800, 29267994083328000, 32926493343744000, 38414242234368000, 41369183944704000, 45965759938560000, 68291986194432000, 74500348575744000, 83812892147712000, 92194181362483200, 108463742779392000, 126541033242624000, 144618323705856000, 158176291553280000, 192071211171840000, 216080112568320000, 240089013964800000, 288106816757760000, 307313937874944000, 345728180109312000, 368776725449932800, 414873816131174400, 526823893499904000, 592676880187392000, 691456360218624000, 744645311004672000, 827383678894080000, 1297547737694208000, 1415506622939136000, 1592444950806528000, 1751689445887180800, 2060811112808448000, 2404279631609856000, 2747748150411264000, 3005349539512320000, 3649353012264960000, 4105522138798080000, 4561691265331200000, 5474029518397440000, 5838964819623936000, 6568835422076928000, 7006757783548723200, 7882602506492313600]
for (left, right) in data:
	lft_found = False
	rgt_found = False
	lft = rgt = 0
	print(len([number for number in strange if left <= number <= right]))


import math
import bisect

def between(L, R, X):
	Li = bisect.bisect_left(X, L)
	Ri = bisect.bisect_left(X, R)
	if X[Ri] == R:
		Ri += 1
	return Ri - Li

def build(maxSeed):
	insort = bisect.insort
	log10 = math.log10
	floor = math.floor
	digits = lambda n: floor(log10(n)) + 1
	seeds = set([10, 12, 14, 16, 18])
	array = list(range(10))
	while seeds:
		seed = seeds.pop()
		insort(array, seed)
		if seed <= maxSeed:
			m = digits(seed)
			while True:
				newSeed = seed * m
				if newSeed > maxSeed:
					break
				if digits(newSeed) < m:
					break
				if digits(newSeed) == m:
					seeds.add(newSeed)
				m += 1
	return array
arr = build(10 ** 19)
for _ in range(int(input())):
	(L, R) = [int(e) for e in input().strip().split()]
	print(between(L, R, arr))


def makeDP(a):
	if a < 10:
		return list(range(a + 1))
	la = len(str(a))
	li = list(range(10))
	t0 = 10
	for l in range(2, la):
		t1 = 10 * t0
		li_ = []
		for i in range(len(li) - 1, -1, -1):
			if li[i] * l < t0:
				break
			t = li[i]
			while t * l < t1:
				t *= l
				li_.append(t)
		li_.sort()
		li.extend(li_)
		t0 = t1
	li_ = []
	for i in range(len(li) - 1, -1, -1):
		if li[i] * la < t0:
			break
		t = li[i]
		while t * la <= a:
			t *= la
			li_.append(t)
	li_.sort()
	li.extend(li_)
	return li
q = []
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	q.append((L, R))
	try:
		if R > Rmx:
			Rmx = R
	except:
		Rmx = R
li = makeDP(Rmx)

def srch(a):
	if a >= li[len(li) - 1]:
		return len(li)
	for i in range(len(li)):
		if a < li[i]:
			return i
for (L, R) in q:
	print(srch(R) - srch(L - 1))


import sys

def L(x):
	return len(str(x))
s = set(range(10))
t = s.copy()
N = 10 ** 19
while len(t) > 0:
	st = set([])
	for x in t:
		for i in range(3):
			if L(x) + i > 1 and L(x * (L(x) + i)) == L(x) + i and (x * (L(x) + i) < N):
				st.add(x * (L(x) + i))
	for x in st:
		s.add(x)
	t = st.copy()
t = int(input())
for _ in range(t):
	ID = input().split()
	x = int(ID[0])
	y = int(ID[1])
	ans = 0
	for c in s:
		if c >= x and c <= y:
			ans += 1
	print(ans)


from math import log10
strange_memo = [x for x in range(0, 10)]

def generate_strange(d):
	digits = num_digits(max(strange_memo))
	while digits < d:
		for key in [x for x in strange_memo[2:] if num_digits(x) == digits]:
			offset = 1
			while offset <= 2:
				new_num = key * (digits + offset)
				while num_digits(new_num) == digits + offset:
					strange_memo.append(new_num)
					new_num = new_num * (digits + offset)
				offset += 1
		digits += 1

def num_digits(n):
	return int(log10(n)) + 1
generate_strange(19)
T = int(input())
for i in range(T):
	(L, R) = [int(x) for x in input().strip().split(' ')]
	count = 0
	for i in strange_memo:
		if L <= i <= R:
			count += 1
	print(count)


import sys
strange = set()
for ii in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 96, 108, 120, 144, 168, 192, 216, 240, 288, 324, 360, 432, 504, 576, 648, 720, 864, 972, 1152, 1296, 1440, 1728, 2016, 2304, 2592, 2880, 3456, 3888, 4608, 5184, 5760, 6912, 8064, 9216, 10080, 11520, 12960, 14400, 17280, 19440, 23040, 25920, 28800, 34560, 40320, 46080, 50400, 57600, 64800, 72000, 86400, 97200, 103680, 116640, 138240, 155520, 172800, 207360, 241920, 276480, 302400, 345600, 388800, 432000, 518400, 583200, 622080, 699840, 829440, 933120, 1088640, 1209600, 1451520, 1693440, 1935360, 2116800, 2419200, 2721600, 3024000, 3628800, 4082400, 4354560, 4898880, 5806080, 6531840, 7620480, 8467200, 11612160, 13547520, 15482880, 16934400, 19353600, 21772800, 24192000, 29030400, 32659200, 34836480, 39191040, 46448640, 52254720, 60963840, 67737600, 92897280, 104509440, 121927680, 139345920, 152409600, 174182400, 195955200, 217728000, 261273600, 293932800, 313528320, 352719360, 418037760, 470292480, 548674560, 609638400, 836075520, 940584960, 1045094400, 1219276800, 1393459200, 1524096000, 1741824000, 1959552000, 2177280000, 2612736000, 2939328000, 3135283200, 3527193600, 4180377600, 4702924800, 5486745600, 6096384000, 8360755200, 9405849600, 10346434560, 11496038400, 13412044800, 15328051200, 16765056000, 19160064000, 21555072000, 23950080000, 28740096000, 32332608000, 34488115200, 38799129600, 45984153600, 51732172800, 60354201600, 67060224000, 91968307200, 100329062400, 112870195200, 124157214720, 137952460800, 160944537600, 183936614400, 201180672000, 229920768000, 258660864000, 287400960000, 344881152000, 387991296000, 413857382400, 465589555200, 551809843200, 620786073600, 724250419200, 804722688000, 1195587993600, 1304277811200, 1467312537600, 1614043791360, 1793381990400, 2092278988800, 2391175987200, 2615348736000, 2988969984000, 3362591232000, 3736212480000, 4483454976000, 5043886848000, 5380145971200, 6052664217600, 7173527961600, 8070218956800, 9415255449600, 10139505868800, 11266117632000, 16738231910400, 18259889356800, 20542375526400, 22596613079040, 25107347865600, 29291905843200, 33476463820800, 36614882304000, 41845579776000, 47076277248000, 52306974720000, 62768369664000, 70614415872000, 75322043596800, 84737299046400, 107602919424000, 121053284352000, 141228831744000, 152092588032000, 168991764480000, 251073478656000, 273898340352000, 308135632896000, 338949196185600, 376610217984000, 439378587648000, 502146957312000, 549223234560000, 627683696640000, 706144158720000, 784604620800000, 941525544960000, 1004293914624000, 1129830653952000, 1205152697548800, 1355796784742400, 1721646710784000, 1936852549632000, 2259661307904000, 2433481408512000, 2703868231680000, 4017175658496000, 4382373445632000, 4930170126336000, 5423187138969600, 6025763487744000, 7030057402368000, 8034351316992000, 8787571752960000, 10670622842880000, 12004450698240000, 13338278553600000, 16005934264320000, 17072996548608000, 19207121117184000, 20487595858329600, 23048545340620800, 29267994083328000, 32926493343744000, 38414242234368000, 41369183944704000, 45965759938560000, 68291986194432000, 74500348575744000, 83812892147712000, 92194181362483200, 108463742779392000, 126541033242624000, 144618323705856000, 158176291553280000, 192071211171840000, 216080112568320000, 240089013964800000, 288106816757760000, 307313937874944000, 345728180109312000, 368776725449932800, 414873816131174400, 526823893499904000, 592676880187392000, 691456360218624000, 744645311004672000, 827383678894080000]:
	strange.add(ii)
T = int(input())
for _ in range(T):
	(L, R) = [int(ii) for ii in input().split(' ')]
	print(len([ii for ii in strange if ii >= L and ii <= R]))


import sys
import math

def num_digits(n):
	return math.ceil(math.log10(n + 1))

def generate_numbers(max_num):

	def gen_nums(init, max_nd):
		res = set()
		for num in init:
			num_nd = num_digits(num)
			for nd in range(num_nd, max_nd + 1):
				new_num = num * nd
				if num_digits(new_num) == nd:
					res.add(new_num)
		return res - init
	max_nd = num_digits(max_num)
	res = init = set(range(10))
	while init:
		init = gen_nums(init, max_nd)
		res = res.union(init)
	return res

def process_data(f):
	N = int(f.readline().strip())
	strange_numbers = generate_numbers(int(1e+18))
	for _ in range(N):
		(l, r) = [int(x) for x in f.readline().strip().split(' ')]
		print(len({e for e in strange_numbers if l <= e <= r}))
process_data(sys.stdin)


st = [i for i in range(11)]
i = 6
mx = 10
while mx <= 10 ** 18:
	m = st[i]
	fm = len(str(m))
	if m * fm < 10 ** fm and fm > 1:
		st.append(m * fm)
		mx = m * fm
	if 10 ** fm <= m * (fm + 1) < 10 ** (fm + 1):
		st.append(m * (fm + 1))
		mx = m * (1 + fm)
	if 10 ** (fm + 1) <= m * (fm + 2) < 10 ** (fm + 2):
		st.append(m * (fm + 2))
		mx = m * (2 + fm)
	i += 1
t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	q = 0
	for k in st:
		q += l <= k <= r
	print(q)


from math import ceil
import sys

def memoized(func):
	cache = {}

	def newfunc(*args):
		if args not in cache:
			cache[args] = func(*args)
		return cache[args]
	return newfunc

@memoized
def f(r):
	if r < 10:
		return r
	n = len(str(r))
	newl = int(ceil(10 ** (n - 1) / n))
	newr = r // n
	return f(10 ** (n - 1) - 1) + f(newr) - f(newl - 1)
for _ in range(int(input())):
	(l, r) = [int(x) for x in input().split()]
	print(f(r) - f(l - 1))


import sys
from time import time
import bisect

def rdigit_gen(n):
	if n == 0:
		yield n
	while n:
		yield (n % 10)
		n //= 10

def is_strange(n):
	l = sum((1 for _ in rdigit_gen(n)))
	if l == 1:
		return True
	if n % l != 0:
		return False
	return is_strange(n // l)

def solve_bf(l, r):
	print('l=%d r=%d' % (l, r), flush=True, file=sys.stderr)
	cnt = 0
	for i in range(l, r + 1):
		if is_strange(i):
			cnt += 1
			print('%d' % i, file=sys.stderr)
	return cnt

def strange_gen(N):
	stack = [(i, 1) for i in range(10)]
	while stack:
		(n, l) = stack.pop()
		yield n
		new_n = n * l
		new_l = sum((1 for _ in rdigit_gen(new_n)))
		while new_n < N and new_l >= l:
			if new_n != n and new_l == l:
				stack.append((new_n, new_l))
			l += 1
			new_n = n * l
			new_l = sum((1 for _ in rdigit_gen(new_n)))

def get_cache(n):
	cache = list()
	for i in strange_gen(n):
		if i > n:
			break
		cache.append(i)
	cache.sort()
	return cache

def solve(l, r, cache):
	i1 = bisect.bisect_left(cache, l)
	i2 = bisect.bisect_right(cache, r)
	return i2 - i1
tm = time()
N = pow(10, 18)
cache = get_cache(N + 1)
print('cache generated: %d' % len(cache), file=sys.stderr)
print('time elapsed: %s' % (time() - tm), file=sys.stderr)
t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	print(solve(l, r, cache))
print('time elapsed: %s' % (time() - tm), file=sys.stderr)


stranges = []

def rec(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 10):
		m = n
		while len(str(m * i)) == i:
			m *= i
			rec(m)
for n in range(0, 10):
	rec(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


import bisect
strange = list(range(10))
p = 10
for d in range(2, 19):
	i = bisect.bisect_left(strange, (p + d - 1) // d)
	p *= 10
	while i < len(strange):
		x = d * strange[i]
		if x >= p:
			break
		strange.append(x)
		i += 1
T = int(input())
for t in range(T):
	(L, R) = map(int, input().split())
	print(bisect.bisect_left(strange, R + 1) - bisect.bisect_left(strange, L))


import bisect
strange = list(range(10))
p = 10
for d in range(2, 19):
	i = bisect.bisect_left(strange, (p + d - 1) // d)
	p *= 10
	while i < len(strange):
		x = d * strange[i]
		if x >= p:
			break
		strange.append(x)
		i += 1
T = int(input())
for t in range(T):
	(L, R) = map(int, input().split())
	print(bisect.bisect_left(strange, R + 1) - bisect.bisect_left(strange, L))


from collections import deque
from bisect import bisect, bisect_left
N = 10 ** 18 + 1
num_digits = lambda x: len(str(x))
strange = set(range(10))
d = deque([(i, 2) for i in strange])
while d:
	(x, c) = d.popleft()
	n = x * c
	dc = num_digits(n)
	while dc >= c and n < N:
		if dc == c:
			if n not in strange:
				strange.add(n)
				d.append((n, c))
		c += 1
		n += x
		dc = num_digits(n)
strange = sorted(strange)
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(bisect(strange, r) - bisect_left(strange, l))


import os
import sys

def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


def gen():
	S = list(range(10))
	for l in range(2, 19):
		i = 0
		while i < len(S):
			x = S[i] * l
			if len(str(x)) == l:
				S.append(x)
			i += 1
	return S
S = gen()
T = int(input())
for _ in range(T):
	(L, R) = map(int, input().split())
	print(sum((int(L <= x <= R) for x in S)))


maxN = 10 ** 18
from math import log10

def numDigits(N):
	return 1 + int(log10(N))

def listStrangeNumbers(N):
	stack = [(jj, 2) for jj in range(5, 10)]
	S = set(list(range(10)))
	while stack:
		(P, multiplier) = stack.pop()
		D = numDigits(P * multiplier)
		if P * multiplier < N:
			if D > multiplier:
				stack.append((P, 1 + multiplier))
			elif D == multiplier:
				S.add(P * multiplier)
				stack.append((P * multiplier, multiplier))
				stack.append((P, 1 + multiplier))
	L = sorted(list(S))
	return L
strange = listStrangeNumbers(maxN)

def numStrange(n):
	dex = 0
	return sum([sss <= n for sss in strange])
T = int(input())
for t in range(T):
	(l, r) = list(map(int, input().split()))
	print(numStrange(r) - numStrange(l - 1))


M = 1000000
p = 1e+18 + 1

def g(n):
	l = len(str(n))
	if l == 1:
		return True
	if n % l == 0:
		if g(n // l):
			return True
	return False
l = []
for i in range(M):
	if g(i):
		l.append(i)
for i in l:
	t = i
	for le in range(3, 19):
		t1 = t * le
		if len(str(t1)) == le:
			if g(i):
				l.append(t1)
l = list(dict.fromkeys(l))
l.sort()
t = int(input())
for _ in range(t):
	(L, R) = [int(x) for x in input().split()]
	cnt = 0
	for i in range(len(l)):
		if l[i] >= L and l[i] <= R:
			cnt += 1
	print(cnt)


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


t = int(input())
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
i = 5
s = 0
while s < 10 ** 18:
	n = l[i]
	le = len(str(n))
	s = n * le
	if le != 1 and len(str(s)) == le:
		l.append(s)
	if len(str(n * (le + 1))) == le + 1:
		l.append(n * (le + 1))
	if len(str(n * (le + 2))) == le + 2:
		l.append(n * (le + 2))
	i = i + 1
l.sort()
for _ in range(t):
	(a, b) = map(int, input().split())
	co = 0
	for i in l:
		if i >= a and i <= b:
			co = co + 1
		elif i > b:
			break
	print(co)


import math
import bisect
_l = [n for n in range(10)]
for i in range(2, 19):
	j = 1
	while j < len(_l):
		if i == int(math.log10(_l[j] * i)) + 1:
			_l.append(_l[j] * i)
		j += 1

def s(n):
	return bisect.bisect(_l, n)

def si(l, r):
	return s(R) - s(L - 1) if l else s(R)
T = int(input())
for _ in range(T):
	(L, R) = map(int, input().split())
	print(si(L, R))


t = int(input())
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
i = 5
s = 0
while s < 10 ** 18:
	n = l[i]
	le = len(str(n))
	s = n * le
	if le != 1 and len(str(s)) == le:
		l.append(s)
	if len(str(n * (le + 1))) == le + 1:
		l.append(n * (le + 1))
	if len(str(n * (le + 2))) == le + 2:
		l.append(n * (le + 2))
	i = i + 1
l.sort()
for _ in range(t):
	(a, b) = map(int, input().split())
	co = 0
	for i in l:
		if i >= a and i <= b:
			co = co + 1
		elif i > b:
			break
	print(co)


maxN = 10 ** 18
from math import log10

def numDigits(N):
	return 1 + int(log10(N))

def listStrangeNumbers(N):
	stack = [(jj, 2) for jj in range(5, 10)]
	S = set(list(range(10)))
	while stack:
		(P, multiplier) = stack.pop()
		D = numDigits(P * multiplier)
		if P * multiplier < N:
			if D > multiplier:
				stack.append((P, 1 + multiplier))
			elif D == multiplier:
				S.add(P * multiplier)
				stack.append((P * multiplier, multiplier))
				stack.append((P, 1 + multiplier))
	L = sorted(list(S))
	return L
strange = listStrangeNumbers(maxN)

def numStrange(n):
	dex = 0
	return sum([sss <= n for sss in strange])
T = int(input())
for t in range(T):
	(l, r) = list(map(int, input().split()))
	print(numStrange(r) - numStrange(l - 1))


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in l:
	for y in range(2, 19):
		if len(str(x * y)) == y:
			l.append(x * y)
for x in range(int(input())):
	a = list(map(int, input().split()))
	b = 0
	for x in l:
		if a[0] <= x <= a[1]:
			b += 1
	print(b)


l = list(range(10))
for j in range(2, 19):
	for i in l:
		if len(str(i * j)) == j:
			l += [i * j]
for k in range(int(input())):
	(a, b) = map(int, input().split())
	print(sum((int(a <= x <= b) for x in l)))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


a = list(range(10))
i = 0
while i < len(a):
	for x in range(2, 19):
		if x * a[i] <= 10 ** 18 and len(str(x * a[i])) == x and (x * a[i] not in a):
			a.append(x * a[i])
	i += 1
for _ in range(int(input())):
	(L, R) = [int(x) for x in input().split()]
	print(sum((1 if L <= x <= R else 0 for x in a)))


import sys
count_strange_cache = {}

def count_strange(ubound):
	y = count_strange_cache.get(ubound)
	if y is None:
		if ubound <= 9:
			y = ubound
		else:
			nd = len(str(ubound))
			y = count_strange(ubound // nd) - count_strange(10 ** (nd - 1) // nd - 1)
			if nd >= 2:
				y += count_strange(10 ** (nd - 1) - 1)
		count_strange_cache[ubound] = y
	return y

def main():
	T = int(sys.stdin.readline())
	for _ in range(T):
		(L, R) = list(map(int, sys.stdin.readline().split()))
		print(count_strange(R) - count_strange(L - 1))
main()


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


import math
import bisect
_l = [n for n in range(10)]
for i in range(2, 19):
	j = 1
	while j < len(_l):
		if i == int(math.log10(_l[j] * i)) + 1:
			_l.append(_l[j] * i)
		j += 1

def s(n):
	return bisect.bisect(_l, n)

def si(l, r):
	return s(R) - s(L - 1) if l else s(R)
T = int(input())
for _ in range(T):
	(L, R) = map(int, input().split())
	print(si(L, R))


import heapq as hq
from collections import defaultdict
import bisect as bs
maxi = 18
functioni = [(1, i) for i in range(10)]
strange = set()
hq.heapify(functioni)
while functioni:
	(d, n) = hq.heappop(functioni)
	if d > maxi:
		break
	strange.add(n)
	for i in range(max(2, d), maxi + 1):
		if len(str(n * i)) == i:
			hq.heappush(functioni, (i, n * i))
strange = sorted(strange)

def solve(a, b):
	return bs.bisect_right(strange, b) - bs.bisect_left(strange, a)
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	answer = solve(L, R)
	print(answer)


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


import math
import bisect
_l = [n for n in range(10)]
for i in range(2, 19):
	j = 1
	while j < len(_l):
		if i == int(math.log10(_l[j] * i)) + 1:
			_l.append(_l[j] * i)
		j += 1

def s(n):
	return bisect.bisect(_l, n)

def si(l, r):
	return s(R) - s(L - 1) if l else s(R)
T = int(input())
for _ in range(T):
	(L, R) = map(int, input().split())
	print(si(L, R))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


def forensic():

	def is_strange(i):
		l = len(str(i))
		if l == 1:
			return True
		if i % l != 0:
			return False
		return is_strange(i // l)

	def construction(i):
		s = str(i)
		l = len(s)
		if l == 1:
			return '1' + ' (' + s + ')'
		return str(l) + ' ' + construction(i // l)
	n = 0
	l = 0
	for i in range(100, 100000):
		if is_strange(i):
			if l != len(str(i)):
				l = len(str(i))
				n = 0
			n += 1
			print('{:3} {:10} {}'.format(n, i, construction(i)))
stranges = []

def make_strange(n):
	stranges.append(n)
	l = len(str(n))
	if l > 18:
		return
	for i in range(l + 1, l + 5):
		m = n
		while len(str(m * i)) == i:
			m *= i
			make_strange(m)
for n in range(0, 10):
	make_strange(n)
stranges.sort()
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	print(sum((1 for i in stranges if L <= i <= R)))


import heapq as hq
from collections import defaultdict
import bisect as bs
DMAX = 18
work = [(1, i) for i in range(10)]
strange = set()
hq.heapify(work)
while work:
	(d, n) = hq.heappop(work)
	if d > DMAX:
		break
	strange.add(n)
	for k in range(max(2, d), DMAX + 1):
		if len(str(n * k)) == k:
			hq.heappush(work, (k, n * k))
strange = sorted(strange)

def solve(a, b):
	return bs.bisect_right(strange, b) - bs.bisect_left(strange, a)
t = int(input().strip())
for t_itr in range(t):
	first_multiple_input = input().rstrip().split()
	l = int(first_multiple_input[0])
	r = int(first_multiple_input[1])
	result = solve(l, r)
	print(str(result) + '')