### subroutines

def cur_seg(lines, cx, cy)
  x0 = x1 = cx
  while x0 > 0 && lines[cy][x0 - 1]
    x0 -= 1
  end
  while lines[cy][x1]
    x1 += 1
  end
  [x0, x1, lines[cy][x0...x1]]
end

def edit(cmdline)
  lines = [[cmdline[0]]]
  cx = 1
  cy = 0

  for i in (1...cmdline.length)
    ch = cmdline[i]
    case ch
    when 'F'
      cx += 1
      return nil if lines[cy][cx].nil? && lines[cy][cx - 1].nil?
    when 'B'
      cx -= 1
      return nil if cx < 0 || lines[cy][cx].nil?
    when 'P'
      cy += 1
      return nil if lines[cy].nil? ||
        (lines[cy][cx].nil? && (cx == 0 || lines[cy][cx - 1].nil?))
    when 'N'
      cy -= 1
      return nil if cy < 0 ||
        (lines[cy][cx].nil? && (cx == 0 || lines[cy][cx - 1].nil?))
    when 'D'
      return nil if lines[cy][cx].nil?
      lines[cy].delete_at(cx)
      if lines[cy][cx].nil? && (cx == 0 || lines[cy][cx - 1].nil?)
        cy -= 1
        return nil if cy < 0 ||
          (lines[cy][cx].nil? && (cx == 0 || lines[cy][cx - 1].nil?))
      end

      y = cy + 1
      while lines[y]
        x = 0
        while x < lines[y].length
          if lines[y][x]
            x0, x1, seg = cur_seg(lines, x, y)
            slen = x1 - x0
            y0 = y
            while y0 > 0 && lines[y0 - 1][x0...x1].compact.empty?
              lines[y0][x0...x1] = [nil] * slen
              y0 -= 1
              lines[y0][x0...x1] = seg
            end
            x = x1
          end
          x += 1
        end
        y += 1
      end
    when 'C'
      ch0 = lines[cy][cx]
      return nil if ch0.nil?
      cy += 1
      lines[cy] = [] if lines[cy].nil?
      return nil if ! lines[cy][cx].nil?
      lines[cy][cx] = ch0
      cx += 1
    else
      lines[cy][cx,0] = ch
      cx += 1
      x = cx
      while ! lines[cy][x].nil?
        x += 1
      end
      lines[cy].delete_at(x)
      #p [lines, cx, cy]
    end    
  end

  cur_seg(lines, cx, cy)
end

### main

n = gets.to_i

n.times do
  cmdline = gets.strip

  x0, x1, cseg = edit(cmdline)
  puts (cseg ? cseg.join('') : 'ERROR')
end