#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char* string, int len) {
  int half = len/2;
  int i;
  for(i=0; i<half; i++) {
    char temp;
    temp = string[i];
    string[i] = string[len-1-i];
    string[len-1-i] = temp;
  }
}

void make_string(char* string, char* half_left, char* half_right, int len_left, int len_right, int flag) {
  if(flag == -1) {
    reverse(half_right, len_right);
    printf("%s%s", half_right, half_left);
  } else {
    reverse(half_left, len_left);
    printf("%s%s", half_left, half_right);
  }
}

int main(void) {
  char s[100001];
  char half_left[200001];
  char half_right[300001];
  char str_q[7];
  int q;
  int i, flag;
  int len_left, len_right;
  char str_query[6];
  char query;
  len_left = len_right = 0;
  
  scanf("%s", half_right);
  len_right += strlen(half_right);
  scanf("%s", str_q);
  q = atoi(str_q);
  flag=1;
  
  for(i=0; i<q; i++) {
    scanf("%s", str_query);
    if(str_query[0] == '1') {
      flag *= -1;
    } else {
      scanf("%s", str_query);
      if((str_query[0] == '1' && flag==1) || (str_query[0] == '2' && flag == -1)) {
        scanf("%s", str_query);
        half_left[len_left] = str_query[0];
        len_left += 1;
      } else {
        scanf("%s", str_query);
        half_right[len_right] = str_query[0];
        len_right += 1;
      }
    }
  }
  
  make_string(s, half_left, half_right, len_left, len_right, flag);
  return 0;
}
   
; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_170/source.m'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_170/source.m"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1
@.str.1 = private unnamed_addr constant [3 x i8] c"%s\00", align 1

; Function Attrs: minsize nofree nosync nounwind optsize memory(argmem: readwrite) uwtable
define dso_local void @reverse(ptr nocapture noundef %string, i32 noundef %len) local_unnamed_addr #0 {
entry:
  %div = sdiv i32 %len, 2
  %smax = tail call i32 @llvm.smax.i32(i32 %div, i32 0)
  %wide.trip.count = zext i32 %smax to i64
  br label %for.cond

for.cond:                                         ; preds = %for.body, %entry
  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  %exitcond.not = icmp eq i64 %indvars.iv, %wide.trip.count
  br i1 %exitcond.not, label %for.end, label %for.body

for.body:                                         ; preds = %for.cond
  %arrayidx = getelementptr inbounds i8, ptr %string, i64 %indvars.iv
  %0 = load i8, ptr %arrayidx, align 1, !tbaa !5
  %1 = trunc i64 %indvars.iv to i32
  %2 = xor i32 %1, -1
  %sub1 = add i32 %2, %len
  %idxprom2 = sext i32 %sub1 to i64
  %arrayidx3 = getelementptr inbounds i8, ptr %string, i64 %idxprom2
  %3 = load i8, ptr %arrayidx3, align 1, !tbaa !5
  store i8 %3, ptr %arrayidx, align 1, !tbaa !5
  store i8 %0, ptr %arrayidx3, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br label %for.cond

for.end:                                          ; preds = %for.cond
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: minsize nofree nounwind optsize uwtable
define dso_local void @make_string(ptr nocapture readnone %string, ptr noundef %half_left, ptr noundef %half_right, i32 noundef %len_left, i32 noundef %len_right, i32 noundef %flag) local_unnamed_addr #2 {
entry:
  %cmp = icmp eq i32 %flag, -1
  br i1 %cmp, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @reverse(ptr noundef %half_right, i32 noundef %len_right) #7
  %call = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, ptr noundef %half_right, ptr noundef %half_left) #7
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @reverse(ptr noundef %half_left, i32 noundef %len_left) #7
  %call1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, ptr noundef %half_left, ptr noundef %half_right) #7
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  ret void
}

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: minsize nofree nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #2 {
entry:
  %half_left = alloca [200001 x i8], align 16
  %half_right = alloca [300001 x i8], align 16
  %str_q = alloca [7 x i8], align 1
  %str_query = alloca [6 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 200001, ptr nonnull %half_left) #8
  call void @llvm.lifetime.start.p0(i64 300001, ptr nonnull %half_right) #8
  call void @llvm.lifetime.start.p0(i64 7, ptr nonnull %str_q) #8
  call void @llvm.lifetime.start.p0(i64 6, ptr nonnull %str_query) #8
  %call = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %half_right) #7
  %call2 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %half_right) #9
  %conv3 = trunc i64 %call2 to i32
  %call5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %str_q) #7
  %call7 = call i32 @atoi(ptr nocapture noundef nonnull %str_q) #9
  %smax = call i32 @llvm.smax.i32(i32 %call7, i32 0)
  br label %for.cond

for.cond:                                         ; preds = %for.inc, %entry
  %flag.0 = phi i32 [ 1, %entry ], [ %flag.1, %for.inc ]
  %len_left.0 = phi i32 [ 0, %entry ], [ %len_left.1, %for.inc ]
  %len_right.0 = phi i32 [ %conv3, %entry ], [ %len_right.1, %for.inc ]
  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
  %exitcond.not = icmp eq i32 %i.0, %smax
  br i1 %exitcond.not, label %for.end, label %for.body

for.body:                                         ; preds = %for.cond
  %call10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %str_query) #7
  %0 = load i8, ptr %str_query, align 1, !tbaa !5
  %cmp12 = icmp eq i8 %0, 49
  br i1 %cmp12, label %if.then, label %if.else

if.then:                                          ; preds = %for.body
  %mul = sub nsw i32 0, %flag.0
  br label %for.inc

if.else:                                          ; preds = %for.body
  %call15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %str_query) #7
  %1 = load i8, ptr %str_query, align 1, !tbaa !5
  %cmp18 = icmp eq i8 %1, 49
  %cmp20 = icmp eq i32 %flag.0, 1
  %or.cond = select i1 %cmp18, i1 %cmp20, i1 false
  br i1 %or.cond, label %if.then29, label %lor.lhs.false

lor.lhs.false:                                    ; preds = %if.else
  %cmp24 = icmp eq i8 %1, 50
  %cmp27 = icmp eq i32 %flag.0, -1
  %or.cond46 = select i1 %cmp24, i1 %cmp27, i1 false
  br i1 %or.cond46, label %if.then29, label %if.else35

if.then29:                                        ; preds = %lor.lhs.false, %if.else
  %call31 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %str_query) #7
  %2 = load i8, ptr %str_query, align 1, !tbaa !5
  %idxprom = sext i32 %len_left.0 to i64
  %arrayidx33 = getelementptr inbounds [200001 x i8], ptr %half_left, i64 0, i64 %idxprom
  store i8 %2, ptr %arrayidx33, align 1, !tbaa !5
  %add34 = add nsw i32 %len_left.0, 1
  br label %for.inc

if.else35:                                        ; preds = %lor.lhs.false
  %call37 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %str_query) #7
  %3 = load i8, ptr %str_query, align 1, !tbaa !5
  %idxprom39 = sext i32 %len_right.0 to i64
  %arrayidx40 = getelementptr inbounds [300001 x i8], ptr %half_right, i64 0, i64 %idxprom39
  store i8 %3, ptr %arrayidx40, align 1, !tbaa !5
  %add41 = add nsw i32 %len_right.0, 1
  br label %for.inc

for.inc:                                          ; preds = %if.then, %if.else35, %if.then29
  %flag.1 = phi i32 [ %mul, %if.then ], [ %flag.0, %if.then29 ], [ %flag.0, %if.else35 ]
  %len_left.1 = phi i32 [ %len_left.0, %if.then ], [ %add34, %if.then29 ], [ %len_left.0, %if.else35 ]
  %len_right.1 = phi i32 [ %len_right.0, %if.then ], [ %len_right.0, %if.then29 ], [ %add41, %if.else35 ]
  %inc = add nuw i32 %i.0, 1
  br label %for.cond

for.end:                                          ; preds = %for.cond
  call void @make_string(ptr nonnull poison, ptr noundef nonnull %half_left, ptr noundef nonnull %half_right, i32 noundef %len_left.0, i32 noundef %len_right.0, i32 noundef %flag.0) #7
  call void @llvm.lifetime.end.p0(i64 6, ptr nonnull %str_query) #8
  call void @llvm.lifetime.end.p0(i64 7, ptr nonnull %str_q) #8
  call void @llvm.lifetime.end.p0(i64 300001, ptr nonnull %half_right) #8
  call void @llvm.lifetime.end.p0(i64 200001, ptr nonnull %half_left) #8
  ret i32 0
}

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: minsize mustprogress nofree nounwind optsize willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: minsize mustprogress nofree nounwind optsize willreturn memory(read)
declare i32 @atoi(ptr nocapture noundef) local_unnamed_addr #5

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #6

attributes #0 = { minsize nofree nosync nounwind optsize memory(argmem: readwrite) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { minsize nofree nounwind optsize uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { minsize nofree nounwind optsize "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { minsize mustprogress nofree nounwind optsize willreturn memory(argmem: read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { minsize mustprogress nofree nounwind optsize willreturn memory(read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #7 = { minsize optsize }
attributes #8 = { nounwind }
attributes #9 = { minsize nounwind optsize willreturn memory(read) }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.0.0 (++20231007042255+8abb2ace888b-1~exp1~20231007042414.1230)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
