MV = {}
MV["U"] = [-1, 0]; MV["D"] = [1 ,0]; MV["L"] = [0, -1]; MV["R"] = [0, 1]
Tank = {}
Tank["^"] = "U"; Tank["v"] = "D"; Tank["<"] = "L"; Tank[">"] = "R"
gets.to_i.times do |i|
  puts if i > 0
  h, w = gets.split.map(&:to_i)
  fld = (h+2).times.map{""}
  fld[0] = fld[h+1] = " "*(w+2)
  y, x = 0, 0 
  z = ""
  h.times do |i|
    f = gets.chomp
    Tank.keys.each do |t|
      if f.include?(t)
        y, x = i + 1, f.index(t) + 1
        z = Tank[t]
      end
    end
    fld[i+1] << " " + f + " "
  end 

  n = gets.to_i
  tank = gets
  n.times do |i|
    if tank[i] == "S"
      my, mx = y, x
      while fld[my][mx] != " "
        my += MV[z][0]; mx += MV[z][1]
        if fld[my][mx] == "*"
          fld[my][mx] = "."
          break
        elsif fld[my][mx] == "#"
          break
        end
      end
    else
      z = tank[i]
      if fld[y + MV[z][0]][x + MV[z][1]] == "."
        fld[y][x] = "."
        y += MV[z][0]; x += MV[z][1]
        fld[y][x] = Tank.key(z)
      end
    end
  end
  (1..h).each{ |i| puts fld[i][1,w] }
end