### subroutines

def find_seg_exist(segs, cx)
  segs.each_with_index do |seg, i|
    return i if seg[0] <= cx && cx < seg[1]
  end
  nil
end

def find_seg_range(segs, cx)
  segs.each_with_index do |seg, i|
    return i if seg[0] <= cx && cx <= seg[1]
  end
  nil
end

def merge_segs(segs)
  i = 0
  while i < segs.length - 1
    seg0 = segs[i]
    seg1 = segs[i + 1]
    if seg0[1] == seg1[0]
      seg0[1] = seg1[1]
      seg0[2] += seg1[2]
      segs.delete_at(i + 1)
    else
      i += 1
    end
  end
end

def conflict?(segs, cseg)
  segs.each do |seg|
    return true if seg[0] < cseg[1] && cseg[0] < seg[1]
  end
  false
end

def insert_seg(segs, cseg)
  i = 0
  while i < segs.length
    if segs[i][0] > cseg[0]
      segs[i, 0] = [cseg]
      return
    end
    i += 1
  end
  segs << cseg
end

def fall_seg(lines, y, i)
  seg = lines[y][i]
  y0 = y - 1
  while y0 >= 0
    break if conflict?(lines[y0], seg)
    y0 -= 1
  end
  y0 += 1
  if y != y0
    lines[y].delete_at(i)
    insert_seg(lines[y0], seg)
  end
  y0
end

def edit(cmdline)
  cseg = [0, 1, cmdline[0]]
  lines = [[cseg]]
  nlines = 1
  cx = 1
  cy = 0
  #p [cmdline[0], cx, cy, lines]

  for i in (1...cmdline.length)
    ch = cmdline[i]
    case ch
    when 'F'
      cx += 1
      return nil if cx > cseg[1]

    when 'B'
      cx -= 1
      return nil if cx < cseg[0]

    when 'P'
      cy += 1
      return nil if cy >= nlines
      k = find_seg_range(lines[cy], cx)
      return nil if k.nil?
      cseg = lines[cy][k]

    when 'N'
      cy -= 1
      return nil if cy < 0
      k = find_seg_range(lines[cy], cx)
      return nil if k.nil?
      cseg = lines[cy][k]

    when 'D'
      return nil if cx >= cseg[1]
      cseg[1] -= 1
      cseg[2][cx - cseg[0]] = ''

      if cseg[0] == cseg[1]
        lines[cy].delete(cseg)
        cy -= 1
        return nil if cy < 0
        k = find_seg_range(lines[cy], cx)
        return nil if k.nil?
        cseg = lines[cy][k]
      end

      falled = false
      for y in (cy...nlines)
        k = 0
        while k < lines[y].length
          seg = lines[y][k]
          is_cseg = (cy == y && seg[0] <= cx && cx <= seg[1])
          y0 = fall_seg(lines, y, k)
          if y == y0
            k += 1
          else
            changed = true
            cy = y0 if is_cseg
          end
        end
      end
      #p ["  fall", cx, cy, lines]
      
      if changed
        lines.each{|segs| merge_segs(segs)}
        k = find_seg_range(lines[cy], cx)
        cseg = lines[cy][k]
      end
      
    when 'C'
      return nil if cx >= cseg[1]
      y = cy + 1
      if y >= nlines
        lines[y] = []
        nlines += 1
      else
        return nil if find_seg_exist(lines[y], cx)
      end
      seg = [cx, cx + 1, cseg[2][cx - cseg[0]]]
      insert_seg(lines[y], seg)
      #p ["insert", lines]
      merge_segs(lines[y])
      #p ["merge", lines]
      cy = y
      cx += 1
      k = find_seg_range(lines[cy], cx)
      cseg = lines[cy][k]
    else
      cseg[1] += 1
      cseg[2][cx - cseg[0], 0] = cmdline[i]
      merge_segs(lines[cy])
      cx += 1
    end    

    #p [cmdline[i], cx, cy, lines]
  end

  cseg
end

### main

n = gets.to_i

n.times do
  cmdline = gets.strip

  cseg = edit(cmdline)
  puts (cseg ? cseg[2]: 'ERROR')
end