use std::collections::VecDeque;                                                                                                                     
                                                                                                                                                    
fn string_formation() -> Result<String, &'static str> {                                                                                             
    let mut S = String::new();                                                                                                                      
    std::io::stdin().read_line(&mut S)                                                                                                              
        .map_err(|_| "input error")?;                                                                                                               
    let S = S.trim_right();                                                                                                                         
                                                                                                                                                    
    let mut Q = String::new();                                                                                                                      
    std::io::stdin().read_line(&mut Q)                                                                                                              
        .map_err(|_| "input error")?;                                                                                                               
    let Q: i64 = Q.trim_right().parse::<i64>()                                                                                                      
        .map_err(|_| "parse error")?;                                                                                                               
                                                                                                                                                    
    let mut end_add = true;                                                                                                                         
    let mut queue: VecDeque<char> = VecDeque::new();                                                                                                
    for c in S.chars() {                                                                                                                            
        queue.push_back(c);                                                                                                                         
    }                                                                                                                                               
    for i in 0..Q {                                                                                                                                 
        let mut input = String::new();                                                                                                              
        std::io::stdin().read_line(&mut input)                                                                                                      
            .map_err(|_| "input error")?;                                                                                                           
        let line: Vec<char> = input.chars().collect();                                                                                              
                                                                                                                                                    
        if line[0] == '1' {                                                                                                                         
            end_add = !end_add;                                                                                                                     
        }                                                                                                                                           
        else {                                                                                                                                      
            if end_add {                                                                                                                            
                if line[2] == '1' {                                                                                                                 
                    queue.push_front(line[4]);                                                                                                      
                }                                                                                                                                   
                else {                                                                                                                              
                    queue.push_back(line[4]);                                                                                                       
                }                                                                                                                                   
            }                                                                                                                                       
            else {                                                                                                                                  
                if line[2] == '1' {
                    queue.push_back(line[4]);
                }
                else {
                    queue.push_front(line[4]);
                }
            }
        }
    }
    if end_add {
        for c in queue {
            print!("{}", c);
        }
    }
    else {
        for c in queue.iter().rev() { 
            print!("{}", c);
        }
    }
    Ok(String::from(""))
}

fn main() {
    println!("{}", string_formation().unwrap());
}

Measured memory_bytes: 8700