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

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

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

d.q = i
d.o = i
c.o = g
c.y = d
g.o = c
g.y = d
i.q = d
i.o = d
assert g.o.o.o.y.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.o: A = None
        self.txt = txt

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

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

d.q = i
d.o = i
c.o = g
c.y = d
g.o = c
g.y = d
i.q = d
i.o = d
assert g.o.o.o.y.txt == "d"