class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.r: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

d = A("d")
c = B("c")
j = A("j")
b = B("b")
f = A("f")
k = B("k")

d.q = b
d.r = f
c.x = b
j.q = k
j.r = f
b.x = c
f.q = c
f.r = j
k.x = c
assert c.x.x.x.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.r: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

d = A("d")
c = B("c")
j = A("j")
b = B("b")
f = A("f")
k = B("k")

d.q = b
d.r = f
c.x = b
j.q = k
j.r = f
b.x = c
f.q = c
f.r = j
k.x = c
assert c.x.x.x.txt == "b"