def contact_sequence(data, pIDs):
    """
    Given a pd.DataFrame with variables ['gID', 'pID', 'begin', 'end'], generate a contact sequence of events (j, i, t) where j is the group member being interrupted, i is the group member doing the interrupting, and t is the start time of the interruptive speech. Ignores non-interruptive simultaneous speech.
    Ref: Holme & Saramäki (2012)
    """
    C = []

    data = data[data['pID'].isin(pIDs)]
    for i in range(len(data)):
        ego = data.iloc[i, :]
        interruptions = data[
            (data['end'] > ego['end']) &
            (data['begin'] < ego['end']) &
            (data['begin'] >= ego['begin'])
        ]
        
        if interruptions.empty:
            continue
        else:
            for j in range(len(interruptions)):
                alter = interruptions.iloc[j, :]
                c = (ego['pID'], alter['pID'], alter['begin'])
                C.append(c)
    C = sorted(C, key = lambda x: x[2])
    return C