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

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

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

a.p = d
a.q = d
d.t = b
d.z = g
g.p = b
g.q = b
b.t = d
b.z = g
assert d.t.t.z.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.q: B = None
        self.txt = txt

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

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

a.p = d
a.q = d
d.t = b
d.z = g
g.p = b
g.q = b
b.t = d
b.z = g
assert d.t.t.z.p.txt == "b"