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

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

n = A("n")
j = B("j")
h = B("h")
a = A("a")

n.x = h
j.q = a
h.q = a
a.x = h
assert a.x.q.x.q.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

n = A("n")
j = B("j")
h = B("h")
a = A("a")

n.x = h
j.q = a
h.q = a
a.x = h
assert a.x.q.x.q.txt == "a"