Problem p03297 (python)

Fixed submission:
from math import floor
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
T = int(input())
for i in range(T):
    A,B,C,D = map(int,input().split())
    if B > D or B > A:
        print("No")
    else:
        if A%B>C:
            print("No")
        else:
            d = gcd(B,D%B)
            r = (A%B)%d
            if B - d + r > C:
                print("No")
            else:
                print("Yes")

Buggy submission (verdict: Runtime Error):
from math import floor
import sys
sys.setrecursionlimit(500000)
T = int(input())
for i in range(T):
    A,B,C,D = map(int,input().split())
    if B > D:
        print("No")
    else:
        memo = {}
        def buy(A):
            if A < B:
                return False
            else:
                if A % B > C:
                    return False
                else:
                    A = floor((C-A%B)/B)*B+A%B+D%B
                    if A not in memo:
                        memo[A] = 0
                        return buy(A)
                    else:
                        return(True)
        
        if buy(A):
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
from math import floor
import sys
sys.setrecursionlimit(500000)
T = int(input())
for i in range(T):
    if B > D:
        print("No")
    else:
        memo = {}
        def buy(A):
            if A < B:
                return False
            else:
                if A % B > C:
                    return False
                else:
                    A = floor((C-A%B)/B)*B+A%B+D%B
                    if A not in memo:
                        memo[A] = 0
                        return buy(A)
                    else:
                        return(True)
        A,B,C,D = map(int,input().split())
        if buy(A):
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
from math import floor
import sys
sys.setrecursionlimit(500000)
T = int(input())
for i in range(T):
    memo = {}
    def buy(A):
        if A < B:
            return False
        else:
            if A % B > C:
                return False
            else:
                A = floor((C-A%B)/B)*B+A%B+D%B
                if A not in memo:
                    memo[A] = 0
                    return buy(A)
                else:
                    return(True)
    A,B,C,D = map(int,input().split())
    if B > D:
        print("No")
    else:
        if buy(A):
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
from math import floor
import sys
sys.setrecursionlimit(5000000)
T = int(input())
for i in range(T):
    memo = {}
    def buy(A):
        if A < B:
            return False
        else:
            if A % B > C:
                return False
            else:
                A = floor((C-A%B)/B)*B+A%B+D
                if A not in memo:
                    memo[A] = 0
                    return buy(A)
                else:
                    return(True)
    A,B,C,D = map(int,input().split())
    if B > D:
        print("No")
    else:
        if buy(A):
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
from math import floor
T = int(input())
for i in range(T):
    memo = {}
    def buy(A):
        if A < B:
            return False
        else:
            if A % B > C:
                return False
            else:
                A = floor((C-A%B)/B)*B+A%B+D
                if A not in memo:
                    memo[A] = 0
                    return buy(A)
                else:
                    return(True)
    A,B,C,D = map(int,input().split())
    if B > D:
        print("No")
    else:
        if buy(A):
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
from math import floor
T = int(input())
for i in range(T):
    memo = {}
    def buy(A):
        if A < B:
            return False
        else:
            if A % B > C:
                return False
            else:
                A = floor((C-A%B)/B)*B+A%B+D
                if A not in memo:
                    memo[A] = 0
                    return buy(A)
                else:
                    return(True)
    A,B,C,D = map(int,input().split())
    if buy(A):
        print("Yes")
    else:
        print("No")