Problem:
Problem E: The Devil of Gravity

Haven't you ever thought that programs written in Java, C++, Pascal, or any other modern computer languages look rather sparse? Although most editors provide sufficient screen space for at least 80 characters or so in a line, the average number of significant characters occurring in a line is just a fraction. Today, people usually prefer readability of programs to efficient use of screen real estate.

Dr. Faust, a radical computer scientist, believes that editors for real programmers shall be more space efficient. He has been doing research on saving space and invented various techniques for many years, but he has reached the point where no more essential improvements will be expected with his own ideas.

After long thought, he has finally decided to take the ultimate but forbidden approach. He does not hesitate to sacrifice anything for his ambition, and asks a devil to give him supernatural intellectual powers in exchange with his soul. With the transcendental knowledge and ability, the devil provides new algorithms and data structures for space efficient implementations of editors.

The editor implemented with those evil techniques is beyond human imaginations and behaves somehow strange. The mighty devil Dr. Faust asks happens to be the devil of gravity. The editor under its control saves space with magical magnetic and gravitational forces.

Your mission is to defeat Dr. Faust by re-implementing this strange editor without any help of the devil. At first glance, the editor looks like an ordinary text editor. It presents texts in two-dimensional layouts and accepts editing commands including those of cursor movements and character insertions and deletions. A text handled by the devil's editor, however, is partitioned into text segments, each of which is a horizontal block of non-blank characters. In the following figure, for instance, four text segments "abcdef", "ghijkl", "mnop", "qrstuvw" are present and the first two are placed in the same row.

abcdef ghijkl
mnop
qrstuvw

The editor has the following unique features.

A text segment without any supporting segments in the row immediately below it falls by the evil gravitational force.

Text segments in the same row and contiguous to each other are concatenated by the evil magnetic force.

For instance, if characters in the segment "mnop" in the previous example are deleted, the two segments on top of it fall and we have the following.

abcdef
qrstuvw ghijkl

After that, if "x" is added at the tail (i.e., the right next of the rightmost column) of the segment "qrstuvw", the two segments in the bottom row are concatenated.

abcdef
qrstuvwxghijkl

Now we have two text segments in this figure. By this way, the editor saves screen space but demands the users' extraordinary intellectual power.

In general, after a command execution, the following rules are applied, where S is a text segment, left(S) and right(S) are the leftmost and rightmost columns of S, respectively, and row(S) is the row number of S.

If the columns from left(S) to right(S) in the row numbered row(S)-1 (i.e., the row just below S) are empty (i.e., any characters of any text segments do not exist there), S is pulled down to row(S)-1 vertically, preserving its column position. If the same ranges in row(S)-2, row(S)-3, and so on are also empty, S is further pulled down again and again. This process terminates sooner or later since the editor has the ultimate bottom, the row whose number is zero.

If two text segments S and T are in the same row and right(S)+1 = left(T), that is, T starts at the right next column of the rightmost character of S, S and T are automatically concatenated to form a single text segment, which starts at left(S) and ends at right(T). Of course, the new segment is still in the original row.

Note that any text segment has at least one character. Note also that the first rule is applied prior to any application of the second rule. This means that no concatenation may occur while falling segments exist. For instance, consider the following case.

dddddddd
cccccccc
bbbb
aaa

If the last character of the text segment "bbbb" is deleted, the concatenation rule is not applied until the two segments "cccccccc" and "dddddddd" stop falling. This means that "bbb" and "cccccccc" are not concatenated.

The devil's editor has a cursor and it is always in a single text segment, which we call the current segment. The cursor is at some character position of the current segment or otherwise at its tail. Note that the cursor cannot be a support. For instance, in the previous example, even if the cursor is at the last character of "bbbb" and it stays at the same position after the deletion, it cannot support "cccccccc" and "dddddddd" any more by solely itself and thus those two segments shall fall. Finally, the cursor is at the leftmost "d"

The editor accepts the following commands, each represented by a single character.

F: Move the cursor forward (i.e., to the right) by one column in the current segment. If the cursor is at the tail of the current segment and thus it cannot move any more within the segment, an error occurs.

B: Move the cursor backward (i.e., to the left) by one column in the current segment. If the cursor is at the leftmost position of the current segment, an error occurs.

P: Move the cursor upward by one row. The column position of the cursor does not change. If the new position would be out of the legal cursor range of any existing text segment, an error occurs.

N: Move the cursor downward by one row. The column position of the cursor does not change. If the cursor is in the bottom row or the new position is out of the legal cursor range of any existing text segment, an error occurs.

D: Delete the character at the cursor position. If the cursor is at the tail of the current segment and so no character is there, an error occurs. If the cursor is at some character, it is deleted and the current segment becomes shorter by one character. Neither the beginning (i.e., leftmost) column of the current segment nor the column position of the cursor changes. However, if the current segment becomes empty, it is removed and the cursor falls by one row. If the new position would be out of the legal cursor range of any existing text segment, an error occurs.

Note that after executing this command, some text segments may lose their supports and be pulled down toward the hell. If the current segment falls, the cursor also falls with it.

C: Create a new segment of length one immediately above the current cursor position. It consists of a copy of the character under the cursor. If the cursor is at the tail, an error occurs. Also if the the position of the new segment is already occupied by another segment, an error occurs. After executing the command, the created segment becomes the new current segment and the column position of the cursor advances to the right by one column.

Lowercase and numeric characters (`a' to `z' and `0' to `9'): Insert the character at the current cursor position. The current segment becomes longer by one character. The cursor moves forward by one column. The beginning column of the current segment does not change.

Once an error occurs, the entire editing session terminates abnormally.

Input

The first line of the input contains an integer that represents the number of editing sessions. Each of the following lines contains a character sequence, where the first character is the initial character and the rest represents a command sequence processed during a session. Each session starts with a single segment consisting of the initial character in the bottom row. The initial cursor position is at the tail of the segment. The editor processes each command represented by a character one by one in the manner described above.

You may assume that each command line is non-empty and its length is at most one hundred. A command sequence ends with a newline.

Output

For each editing session specified by the input, if it terminates without errors, your program should print the current segment at the completion of the session in a line. If an error occurs during the session, just print "ERROR" in capital letters in a line.

Sample Input

3
12BC3BC4BNBBDD5
aaaBCNBBBCb
aaaBBCbNBC

Output for the Sample Input

15234
aba
ERROR

Solution in Ruby (judge verdict: Accepted)

### 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