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

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

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

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

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

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

i.p = d
i.q = j
g.x = f
g.s = f
j.x = g
j.s = g
f.x = g
f.s = g
d.p = i
d.q = f
assert g.s.x.s.x.x.txt == "f"