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

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

e = A("e")
k = B("k")
c = A("c")
i = B("i")
j = A("j")
h = B("h")

e.p = j
e.q = c
k.y = j
c.p = j
c.q = e
i.y = j
j.p = e
j.q = c
h.y = e
assert i.y.p.q.p.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.q: A = None
        self.txt = txt

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

e = A("e")
k = B("k")
c = A("c")
i = B("i")
j = A("j")
h = B("h")

e.p = j
e.q = c
k.y = j
c.p = j
c.q = e
i.y = j
j.p = e
j.q = c
h.y = e
assert i.y.p.q.p.q.q.txt == "e"