Read problems statements in Mandarin Chinese  , Russian and Vietnamese 

A number is called palindromic if its decimal representation is a palindrome. You are given a range, described by a pair of integers L and R. Find the sum of all palindromic numbers lying in the range [L, R], inclusive of both the extrema.

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

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a pair of space separated integers L and R denoting the range for which you are required to find the sum of the palindromic numbers. 

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

For each test case, output a single line containing the sum of all the palindromic numbers in the given range.

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

$1 ≤ T ≤ 100$
$Subtask 1 (34 points) : 1 ≤ L ≤ R ≤ 10^{3}$
$Subtask 2 (66 points) : 1 ≤ L ≤ R ≤ 10^{5}$

----- Sample Input 1 ------ 
2
1 10
123 150
----- Sample Output 1 ------ 
45
272
----- explanation 1 ------ 
Example case 1. The palindromic numbers between 1 and 10 are all numbers except the number 10. Their sum is 45.
Example case 2. The palindromic numbers between 123 and 150 are 131 and 141 and their sum is 272.

Difficulty: EASY
Tags: String algorithms

Ad-hoc

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	su = 0
	for i in range(x, y + 1):
		s = str(i)
		s1 = s[::-1]
		if s == s1:
			su += i
	print(su)


for _ in range(int(input())):
	(l, r) = map(int, input().split(' '))
	ans = 0
	for x in range(l, r + 1):
		x = str(x)
		i = 0
		j = len(x) - 1
		flag = True
		while i <= j:
			if x[i] != x[j]:
				flag = False
				break
			i += 1
			j -= 1
		if flag:
			ans += int(x)
	print(ans)


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	l1 = []
	for i in range(l, r + 1):
		i = str(i)
		if i == i[::-1]:
			l1.append(int(i))
	print(sum(l1))


for i in range(int(input())):
	(x, y) = map(int, input().split())
	s = 0
	for i in range(x, y + 1):
		if str(i) == str(i)[::-1]:
			s += int(i)
	print(s)


def rever(x):
	num = str(x)
	size = len(num)
	reversed_num = num[size::-1]
	return reversed_num
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split(' '))
	su = 0
	for j in range(a, b + 1):
		if j == int(rever(j)):
			su = su + j
	print(su)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	tot = 0
	for i in range(l, r + 1):
		s = str(i)
		if s[::-1] == s:
			tot += i
	print(tot)


t = int(input())
for i in range(t):
	(l, r) = list(map(int, input().split(' ')))
	total = 0
	for i in range(l, r + 1):
		ary = []
		ary.extend(str(i))
		if ary == ary[::-1]:
			total += i
	print(total)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	s = 0
	for i in range(l, r + 1):
		x = str(i)
		if x == x[::-1]:
			s = s + i
	print(s)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	print(sum((j for j in range(x, y + 1) if str(j) == str(j)[::-1])))


t = int(input())
while t > 0:
	(a, b) = map(int, input().split())
	c = 0
	for i in range(a, b + 1):
		i = str(i)
		if i == i[::-1]:
			c = c + int(i)
	print(c)
	t = t - 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = []
	k = []
	for i in range(a, b + 1):
		l.append(i)
	for i in l:
		j = str(i)
		if j == j[::-1]:
			k.append(j)
	g = 0
	for i in k:
		g += int(i)
	print(g)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	sum1 = 0
	for x in range(a, b + 1):
		d = str(x)
		if d == d[::-1]:
			sum1 += x
	print(sum1)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = []
	for x in range(a, b + 1):
		c = str(x)
		n = len(c) - 1
		i = 0
		j = 0
		while n >= i:
			if c[i] != c[n]:
				j = 1
				break
			i = i + 1
			n = n - 1
		if j == 0:
			l.append(x)
	print(sum(l))


for t in range(int(input())):
	sum = 0
	(a, b) = map(int, input().split())
	for i in range(a, b + 1):
		a = str(i)
		if a == a[::-1]:
			sum += i
	print(sum)


t = int(input())
for tc in range(t):
	(x, y) = map(int, input().split())
	s = 0
	for j in range(x, y + 1):
		a = str(j)
		if a == a[::-1]:
			s += j
	print(s)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	sumu = 0
	for i in range(a, b + 1):
		j = str(i)
		if j == j[::-1]:
			sumu += i
	print(sumu)


for _ in range(int(input())):
	(l, r) = list(map(int, input().split()))
	s = 0
	while l <= r:
		p = str(l)
		if p == p[::-1]:
			s += l
		l += 1
	print(s)


def Palindrom(x):
	return True if str(x) == str(x)[::-1] else False

def main():
	T = int(input())
	for _ in range(T):
		(x, y) = map(int, input().split())
		sm = 0
		for i in range(x, y + 1):
			if Palindrom(i):
				sm += i
		print(sm)
main()


for i in range(int(input())):
	(l, r) = map(int, input().split())
	sum = 0
	for j in range(l, r + 1):
		res = str(j)
		if res == res[::-1]:
			sum += j
	print(sum)


t = int(input())
while t != 0:
	(n, m) = map(int, input().split())
	s = 0
	for i in range(n, m + 1):
		if str(i) == str(i)[::-1]:
			s += i
	print(s)
	t -= 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(sum(filter(lambda x: str(x) == str(x)[::-1], range(a, b + 1))))


for _ in range(int(input())):
	(L, R) = map(int, input().split())
	res = 0
	for i in range(L, R + 1):
		if str(i)[-1::-1] == str(i):
			res += i
	print(res)


for i in range(int(input())):
	(l, r) = input().split()
	s = 0
	for i in range(int(l), int(r) + 1):
		i = str(i)
		if i == i[::-1]:
			s += int(i)
	print(s)


def is_pal(n: int) -> bool:
	return str(n) == str(n)[::-1]
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	s = 0
	for i in range(l, r + 1):
		if is_pal(i):
			s += i
	print(s)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	k = 0
	for i in range(a, b + 1):
		p = str(i)
		if p == p[::-1]:
			k += i
	print(k)


for _ in range(int(input())):
	c = 0
	(l, r) = map(int, input().split())
	for i in range(l, r + 1):
		k = str(i)
		if k == k[::-1]:
			c += i
	print(c)


import sys
for _ in range(int(sys.stdin.readline())):
	(a, b) = map(int, sys.stdin.readline().split())
	sys.stdout.write(str(sum(filter(lambda x: str(x) == str(x)[::-1], range(a, b + 1)))) + '\n')


def ispalindrom(i):
	i = str(i)
	iss = i[::-1]
	if i == iss:
		return True
	else:
		return False
for i in range(0, int(input())):
	(a, b) = map(int, input().split())
	sumi = 0
	if b <= 10:
		for i in range(a, b):
			sumi += i
		print(sumi)
	else:
		for i in range(a, b + 1):
			if ispalindrom(i) == 1:
				sumi += i
		print(sumi)


for i in range(int(input())):
	(l, r) = map(int, input().split())
	sm = 0
	for j in range(l, r + 1):
		st1 = str(j)
		st2 = st1[::-1]
		if st1 == st2:
			sm += j
	print(sm)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	total = 0
	for i in range(a, b + 1):
		if str(i) == str(i)[::-1]:
			total += i
	print(total)


t = int(input())
for _ in range(t):
	[l, r] = input().split()
	add = 0
	for a in range(int(l), int(r) + 1):
		a = str(a)
		if a == a[::-1]:
			add += int(a)
	print(add)


for i in range(int(input())):
	(l, r) = map(int, input().split())
	s = 0
	for j in range(l, r + 1):
		p = str(j)
		if p[::-1] == p:
			s += j
	print(s)


t = int(input())
for i in range(t):
	sum = 0
	(l, r) = map(int, input().split())
	for i in range(l, r + 1):
		if str(i) == str(i)[::-1]:
			sum += i
	print(sum)


for i in range(int(input())):
	(l, r) = map(int, input().split())
	ans = 0
	for i in range(l, r + 1):
		a = str(i)
		if a == a[::-1]:
			ans += i
	print(ans)


def pali(n):
	a = str(n)
	if a == a[::-1]:
		return True
	return False
for t in range(int(input())):
	sum = 0
	(a, b) = map(int, input().split())
	for i in range(a, b + 1):
		if pali(i):
			sum += i
	print(sum)


for _ in range(int(input())):
	(s1, s2) = map(int, input().split())
	sum = 0
	for num in range(s1, s2 + 1):
		num = str(num)
		if num == num[::-1]:
			sum += int(num)
	print(sum)


for t in range(int(input())):
	(L, R) = map(int, input().split())
	sum = 0
	for i in range(L, R + 1):
		i = str(i)
		if i == i[::-1]:
			sum += int(i)
	print(sum)


for _ in range(int(input())):
	(l, u) = map(int, input().split())
	sum_ = 0
	for i in range(l, u + 1):
		a = str(i)
		if a == a[::-1]:
			sum_ += i
	print(sum_)


t = int(input())
for k in range(t):
	(l, r) = map(int, input().split())
	s = 0
	for j in range(l, r + 1):
		if str(j) == str(j)[::-1]:
			s += j
	print(s)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	sum = 0
	for i in range(l, r + 1):
		if i < 10:
			sum += i
		elif str(i) == str(i)[::-1]:
			sum += i
	print(sum)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	count = 0
	for i in range(a, b + 1):
		if str(i) == str(i)[::-1]:
			count += i
	print(count)


for i in range(int(input())):
	(x, y) = map(int, input().split())
	k = 0
	for i in range(x, y + 1):
		p = str(i)
		if p == p[::-1]:
			k = k + i
	print(k)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	h = 0
	for i in range(l, r + 1):
		a = str(i)
		if a == a[::-1]:
			h += i
	print(h)


a = set()
for i in range(1, 10 ** 5):
	j = str(i)
	if j == j[::-1]:
		a.add(i)
a = sorted(list(a))
for _ in range(int(input())):
	(c, b) = map(int, input().split())
	s = 0
	for i in range(len(a)):
		s += int(a[i] in range(c, b + 1)) * a[i]
		if a[i] > b:
			break
	print(s)


n = int(input())
for j in range(n):
	(a, b) = map(int, input().split())
	k = 0
	for j in range(a, b + 1):
		if str(j) == str(j)[::-1]:
			k = k + j
	print(k)


def pali(a):
	if str(a) == str(a)[::-1]:
		return 1
	else:
		return 0
t = int(input())
while t:
	(a, b) = map(int, input().split())
	l = []
	for i in range(a, b + 1):
		if pali(i):
			l.append(i)
	print(sum(l))
	t -= 1


def pal(n):
	n = str(n)
	if n == n[::-1]:
		return True
	else:
		return False
t = int(input())
while t > 0:
	s = 0
	(l, r) = (int(v) for v in input().split())
	for i in range(l, r + 1):
		if pal(i):
			s += i
	print(s)
	t = t - 1


def palin(i):
	num = str(i)
	n = len(num)
	(l, r) = (0, n - 1)
	for i in range(n // 2):
		if num[l] != num[r]:
			return False
		l += 1
		r -= 1
	return True
for tc in range(int(input())):
	(l, r) = map(int, input().split())
	tot = 0
	for i in range(l, r + 1):
		if palin(i):
			tot += i
	print(tot)


for _ in range(int(input())):
	(x, y) = [int(i) for i in input().split()]
	s = 0
	for j in range(x, y + 1):
		if str(j) == str(j)[::-1]:
			s += j
	print(s)


for _ in range(int(input())):
	(l, h) = map(int, input().split())
	s = 0
	for i in range(l, h + 1):
		if str(i) == str(i)[::-1]:
			s += i
	print(s)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = 0
	for i in range(a, b + 1):
		k = str(i)
		if k == k[::-1]:
			s += i
	print(s)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	arr = []
	for i in range(l, r + 1):
		x = str(i)
		y = x[::-1]
		if x == y:
			arr.append(i)
	print(sum(arr))


for i in range(int(input())):
	(s, e) = map(int, input().split())
	a = 0
	for i in range(s, e + 1):
		i = str(i)
		if i == i[::-1]:
			a = a + int(i)
	print(a)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	sum = 0
	for i in range(x, y + 1):
		if str(i) == str(i)[::-1]:
			sum += i
	print(sum)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	sum1 = 0
	for j in range(l, r + 1):
		st = str(j)
		st = st[::-1]
		if int(st) == j:
			sum1 += j
	print(sum1)


def ispalindrome(n):
	s = str(n)
	s1 = s[::-1]
	if s == s1:
		return 1
	else:
		return 0
for i in range(int(input())):
	(l, r) = map(int, input().split())
	a = []
	for i in range(l, r + 1, 1):
		if ispalindrome(i):
			a.append(i)
	res = sum(a)
	print(res)


def ispalindrome(x):
	s = str(x)
	isP = True
	for i in range(len(s)):
		if s[i] != s[len(s) - 1 - i]:
			isP = False
			break
	return isP
t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	L = []
	for j in range(l, r + 1):
		if ispalindrome(j):
			L.append(j)
	print(sum(L))


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	sums = 0
	for i in range(l, r + 1):
		if str(i) == str(i)[::-1]:
			sums += i
	print(sums)


for i in range(int(input())):
	L_R = input()
	lst = L_R.split(' ')
	L = int(lst[0])
	R = int(lst[1])
	count = 0
	for i in range(L, R + 1):
		if str(i) == str(i)[::-1]:
			count += i
	print(count)


q = int(input())
while q > 0:
	(l, r) = map(int, input().split())
	c = 0
	for i in range(l, r + 1):
		x = str(i)
		if x == x[::-1]:
			c = c + i
	print(c)
	q = q - 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	sum = 0
	for i in range(a, b + 1):
		temp = str(i)
		if temp == temp[::-1]:
			sum += i
	print(sum)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	x = []
	for num in range(a, b + 1):
		digit = str(num)
		digits = digit[::-1]
		if digit == digits:
			z = int(digits)
			x.append(z)
	print(sum(x))