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

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

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

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

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

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

h.q = i
h.y = d
d.p = g
i.q = h
i.y = g
a.q = i
a.y = d
g.p = d
assert h.y.p.p.p.txt == "g"