Overlapping Line Ordering (Inspired while drawing on a dry erase board)

Challenge:

Given an input string containing characters that represent different colors of dry erase markers on a white board, output the order in which they were drawn, from first to last.

Input:

A string which contains dry erase marker colors which are represented by alphabetical letters (upper ones are different than lowercase, you may substitute any characters used in my examples as long as each color has a distinct letter).  The rest of the whiteboard will be white-space.  There will only be one line of each color per board. There won't be inputs where all lines overlap each other (See test case 4).  All lines will be straight and either horizontal or vertical.

Output:

The order in which the lines were drawn on the board, from the first one drawn to the last one.  If there are multiple solutions for any input, you may output any one of them.  The output can be formatted anyway you'd prefer:  single string of characters or separated by spaces, newlines, etc. as long as the characters used match the ones used in your input.

Test Cases:

Input 1:

  R
  R
BBRBB
  R


Output 1:

BR


Input 2:

    GY
    GY
RRRRGYRRR
    GY
    GY
BBBBBBBB
    GY
    GY


Output 2:

RGYB // or RYGB


Input 3:

    R    P
    R    P
AAAARAAAAPA
    R    P
    R    P
GGGGRGGG P
    R


Output 3:

AGPR // or APGR


Input 4:

 O Y
RRRYR
 O Y
GOGGG
 O Y


Output 4:

// Undefined, does not need to be handled by your program


Input 5:

YYYB
   B
   B


Output 5:

// YB or BY


Rules:

This is code-golf, so shortest code in bytes wins.
Perl, 103+2 = 105 bytes

s/$/$&quot;x y===c/gem;$a=$_;$_.=$&quot;while$a=~s/^./!($_.=$&amp;)/gem;s/$1/-/g,$b=&quot;$&amp;$b&quot;while/\s(\w)(\1|-)+ /;say$b

Run with -n0 (2 byte penalty).
Explanation:
# -n0: read entire input into `$_` at start of program
# (technically speaking it reads to the first NUL byte, but there aren't any)

# We want to be able to extract columns from the input, so we need to add spaces
# to the ends of each line such that each column is complete. Adding too many
# space is OK, so to ensure we have enough, we add a number of spaces equal to the
# length of the input.
s/$/             # At the end of {something},
$&quot; x             # append a number of spaces ($&quot; is a space by default)
y===c            # obtained by counting the characters in $_
/gem;            # where {something} is each (g) line (m)

$a = $_;         # store a copy of the transformed input in $a

# The next step is to create a transposition of the input. To do that, we
# repeatedly extract the first column of $a and append it to $_. This will lead to
# a bunch of junk whitespace at the end of $_ (of varying lengths, because once a
# line is empty it's omitted from the extracted column), but we're OK with that.
# To transpose properly, we'd want to place newlines between the extracted
# columns; however, it happens that the rest of the program treats space the same
# way it would newline, and separating via spaces is shorter, so we do that.

while (          # keep looping as long as there are matches
  $a =~ s/^./    # replace the first character of {something related to $a}
  !(             # with the null string (NOT of something truthy)
    $_.=$&amp;)      # but append that character ($&amp;) to $_
  /gem) {        # {something} is each (g) line (m) of $a
  $_.=$&quot;         # append a space ($&quot;, equivalent to newline here) to $_
}

# Finally, we repeatedly replace every character in the topmost line with the -
# character (treating a line as continuous through the - character but not through
# other characters), thus finding the lines from top to bottom. Because we
# appended the transpose of $_ to $_ above, each line appears twice: once
# horizontally, once vertically. We find only the horizontal copy, but replace
# both with hyphens.
# (Note: I rewrote the regex into a bit more readable of a form in this ungolfed
# version, because the original version wouldn't allow me room to write comments
# inside it. The two should be equivalent; I tested the golfed version.)
while (          # keep looping as long as there are matches
  /\s(\w)        # match a space or newline, $1 (a letter/digit/underscore),
    (\1|-)+      # any positive number of $1s and hyphens,
    \ /x) {      # and a space
  s/$1/-/g,      # changes all $1s to spaces; set $&amp; to $1, $1 becomes invalid
  $b = &quot;$&amp;$b&quot;    # prepend $&amp; to $b
}

# We need to output the lines from first (i.e. bottom) to last (i.e. top).
# We found them in the opposite order, but reversed them via prepending
# (not appending) the partial results to $b.
say $b           # output $b

One slight subtlety here comes with input like this:

   ABC
DDDDDDDDD
   ABC
   ABC
   ABC

Look at the fourth line here. If the order of writing were BACBD, there could genuinely be a horizontal line of Bs along there without violating any of the assumptions of the problem (other than that there's only one line of each colour, something that we don't check). In order to get around this, we ensure in the last regex that each line starts with a letter (or digit or underscore, but those are impossible), and rely on the fact that parallel lines will be found left-to-right and top-to-bottom (because the regex will find the first match within the string). As such, the first character of each ambiguous line here gets overwritten before the line itself is seen as a match, and that prevents the regex matching.