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

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

g = A("g")
e = B("e")
f = B("f")
h = A("h")

g.u = h
e.p = g
e.q = h
f.p = h
f.q = g
h.u = g
assert f.q.u.u.txt == "
class A:
    def __init__(self, txt: str):
        self.u: A = None
        self.txt = txt

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

g = A("g")
e = B("e")
f = B("f")
h = A("h")

g.u = h
e.p = g
e.q = h
f.p = h
f.q = g
h.u = g
assert f.q.u.u.txt == "g"