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

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

d = A("d")
a = B("a")
i = B("i")
g = A("g")

d.r = g
d.u = a
a.r = i
a.q = i
i.r = a
i.q = a
g.r = d
g.u = a
assert d.r.u.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.u: B = None
        self.txt = txt

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

d = A("d")
a = B("a")
i = B("i")
g = A("g")

d.r = g
d.u = a
a.r = i
a.q = i
i.r = a
i.q = a
g.r = d
g.u = a
assert d.r.u.r.r.txt == "a"