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 |j|
    if tank[j] == '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[j]
      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)
      else
        fld[y][x] = Tank.key(z)
      end
    end
  end
  (1..h).each{ |j| puts fld[j][1,w] }
end