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

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

class C:
    def __init__(self, txt: str):
        self.r: A = None
        self.w: A = None
        self.txt = txt

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

b = A("b")
i = B("i")
j = C("j")
c = D("c")

b.r = i
i.r = c
j.r = b
j.w = b
c.q = i
c.r = i
assert c.q.r.q.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.r: A = None
        self.w: A = None
        self.txt = txt

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

b = A("b")
i = B("i")
j = C("j")
c = D("c")

b.r = i
i.r = c
j.r = b
j.w = b
c.q = i
c.r = i
assert c.q.r.q.r.txt == "c"