import random

def task_single_chain_search(mode='train'):

    def choose_next(x, adjacent_mod_list, not_equal=[]):

        while True:
            tmp = random.choice(adjacent_mod_list)
            mod = (x + tmp) % 5
            next_x = random.choice(range(20, 100))
            next_x = next_x // 5 * 5 + mod
            if next_x not in not_equal and next_x <= 100:
                return next_x

    chain = []

    if mode == 'train':
        adjacent_mod_list = [0, 1, 4]
    elif mode == 'test':
        adjacent_mod_list = [2, 3]


    x1 = random.choice(range(1, 100))
    chain.append(x1)

    x2 = choose_next(x1, adjacent_mod_list, not_equal=[x1])
    chain.append(x2)

    x3 = choose_next(x2, adjacent_mod_list, not_equal=[x1, x2])
    chain.append(x3)

    x4 = choose_next(x3, adjacent_mod_list, not_equal=[x1, x2, x3])
    chain.append(x4)

    x5 = choose_next(x4, adjacent_mod_list, not_equal=[x1, x2, x3, x4])
    chain.append(x5)

    x6 = choose_next(x5, adjacent_mod_list, not_equal=[x1, x2, x3, x4, x5])
    chain.append(x6)

    x7 = choose_next(x6, adjacent_mod_list, not_equal=[x1, x2, x3, x4, x5, x6])
    chain.append(x7)

    chain = [chain[i:i+2] for i in range(len(chain)-1)]

    random.shuffle(chain)

    QA = [[x1, x3], [x2, x4], [x3, x5], [x4, x6], [x5, x7]]
    qa = random.choice(QA)

    chain.append(qa)

    seq = [item for sublist in chain for item in sublist]

    return seq


    