Hi, can someone tell what's wrong with my code

Problem Link

# cook your dish here
n = int(input())
substrings = []

def palindrome(n):
    
    s = input().split()[0]
    for i in range(n):
        j = i + 1
        while j <= n:
            substrings.append(s[i:j])
            j += 1
    
    uniqueSubstrings = list(set(substrings))
    notPalindromes = set()
    
    for i in range(len(uniqueSubstrings)):
        substring = uniqueSubstrings[i]
        for j in range(len(substring)):
            if substring[len(substring) - (j + 1)] != substring[j]:
                notPalindromes.add(substring)
    
    palindromes = sorted(list(set(uniqueSubstrings) - notPalindromes))
    addPalindromes(palindromes)
    
def addPalindromes(palindromes):
    T = ''.join(palindromes)
    main(T)


def main(T):
    m = int(input())
    for i in range(m):
        L, R = map(int, input().split())
        print(T[L - 1: R]) if R - 1 <= len(T) else print(-1) 
    
palindrome(n)

It gives correct answer with given sample inputs and custom inputs, but in submission it cant pass any test case. Thanks in advance.

View Submission