### constant

DXYZS = {
  '+x' => [1, 0, 0], '-x' => [-1, 0, 0],
  '+y' => [0, 1, 0], '-y' => [0, -1, 0],
  '+z' => [0, 0, 1], '-z' => [0, 0, -1],
}

M_ID = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
M_RX = [[1, 0, 0], [0, 0, -1], [0, 1, 0]]
M_RY = [[0, 0, 1], [0, 1, 0], [-1, 0, 0]]
M_RZ = [[0, -1, 0], [1, 0, 0], [0, 0, 1]]

### subroutines

def read_keyops(line)
  ops = line.split
  n = ops.shift.to_i

  while ops.length < n
    ops += gets.strip.split
  end

  ops
end

def gen_key(ops)
  cpt = [0, 0, 0]
  pids = {}
  refs = {}

  pids[cpt] = 0
  npt = 1

  pts = [cpt]
  edges = []

  for op in ops
    case op
    when '+x', '-x', '+y', '-y', '+z', '-z'
      dxyz = DXYZS[op]
      pt1 = [cpt[0] + dxyz[0], cpt[1] + dxyz[1], cpt[2] + dxyz[2]]

      if pids[pt1].nil?
        pids[pt1] = npt
        npt += 1
        pts << pt1
      end

      id0 = pids[cpt]
      id1 = pids[pt1]
      edges << [id0, id1]

      cpt = pt1
    else
      if refs[op].nil?
        refs[op] = cpt
      else
        cpt = refs[op]
      end
    end
  end

  [pts, edges]
end

def mul_mat(m0, m1)
  mr = [[0] * 3] * 3
  for i in (0..2)
    for j in (0..2)
      for k in (0..2)
        mr[i][j] += m0[i][k] * m1[k][j]
      end
    end
  end
  mr
end

def mul_mat_p(m, p)
  pr = [0] * 3
  for i in (0..2)
    for j in (0..2)
      pr[i] += m[i][j] * p[j]
    end
  end
  pr
end

def key_rot(key, xi, yi, zi)
  pts0 = []

  for pt in key[0]
    pt0 = pt.clone
    xi.times.each{pt0 = mul_mat_p(M_RX, pt0)}
    yi.times.each{pt0 = mul_mat_p(M_RY, pt0)}
    zi.times.each{pt0 = mul_mat_p(M_RZ, pt0)}
    pts0 << pt0
  end

  [pts0, key[1]]
end

def key_mv(key, dx, dy, dz)
  pts0 = []

  for pt in key[0]
    pts0 << [pt[0] + dx, pt[1] + dy, pt[2] + dz]
  end

  [pts0, key[1]]
end

def key_equal?(key0, key1)
  pts0, edges0 = key0
  pts1, edges1 = key1
  return false if pts0.length != pts1.length || edges0.length != edges1.length

  rots = [0, 0, 0]

  loop do
    key1r = key_rot(key1, *rots)
    pts1r, edges1r = key1r

    for pt1r in pts1r
      key1rm = key_mv(key1r, -pt1r[0], -pt1r[1], -pt1r[2])
      pts1rm, edges1rm = key1rm

      pmap = []
      for pt1rm in pts1rm
        i = pts0.index(pt1rm)
        break if i.nil?
        pmap << i
      end

      next if pmap.length < pts0.length

      emap = []
      for e1rm in edges1rm
        ep0, ep1 = e1rm
        pep0 = pmap[ep0]
        pep1 = pmap[ep1]

        i0 = edges0.index([pep0, pep1])
        if ! i0.nil?
          emap << i0
          next
        end
        i1 = edges0.index([pep1, pep0])
        if ! i1.nil?
          emap << i1
          next
        end
        break
      end

      next if emap.length < edges0.length

      return true
    end

    k = 0
    loop do
      return false if k >= 3
      rots[k] += 1
      break if rots[k] <= 3
      rots[k] = 0
      k += 1
    end
  end

  false
end


### main

loop do
  line = gets.strip
  break if line == '0'

  keyops0 = read_keyops(line)
  keyops1 = read_keyops(gets.strip)
  #p keyops0
  #p keyops1

  key0 = gen_key(keyops0)
  key1 = gen_key(keyops1)
  #p key0
  #p key1

  puts (key_equal?(key0, key1) ? 'SAME' : 'DIFFERENT')
end