Trouble using Python

I have submitted the below code for tiling Problem 2:

      def tileBU(n, m): 
        dp =[0]*(n+1) 
        for i in range(1, n + 1): 
            if i > m: 
                dp[i] = dp[i-1] + dp[i-m] 
            elif i < m:  
                dp[i] = 1
            else: 
                dp[i] = 2
        return dp[n] 
    MOD = (10**9) + 7
    T = int(input())
    for i in range(T):
        n,m = map(int,input().split())
        print(tileBU(n,m)%MOD)

And this code for Counting no of binary strings:

    T = int(input())
    result = []
    for i in range(T):
        n = int(input())
        if  n == 1:
            result.append(2)
        elif n == 2:
            result.append(3)
        else:
            a,b = 2,3
            for j in range(2,n):
                a,b = b,a+b
            result.append(b)
    for i in result:
        print(i)

The first code is giving TLE despite it is the most optimized version of the problem.
The second code is able to run test case -3 but for the remaining 2 test cases it is giving “run-error”.
Someone, Please Explain.
Thankyou