import re


def replace_pair(pair, sentences, indices):
    """Replace all occurrences of a token pair ('ABC', 'XYZ') with a new symbol 'ABC XYZ'"""
    first, second = pair
    pair_str = ' '.join(pair)
    pair_str = pair_str.replace('\\', '\\\\')
    changes = []
    pattern = re.compile(re.escape(first + '\t' + second))
    for j, freq in indices[pair].items():
        if freq < 1:
            continue
        tokens = sentences[j]
        new_snetence = '\t'.join(tokens)
        new_snetence = pattern.sub(pair_str, new_snetence)
        new_snetence = tuple(new_snetence.split('\t'))

        sentences[j] = new_snetence
        changes.append((j, new_snetence, tokens))

    return changes