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

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

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

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

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

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

d.x = i
d.p = i
i.q = g
g.x = i
g.p = i
assert d.p.q.p.txt == "i"