import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      //if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedV = true
              }
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedC = true
              }
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    if (isValidPrefix(n)) println(s.map(c => (c + 'a').toChar).mkString)
    else println(-1)
  }
}

import scala.collection.mutable.BitSet

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a = in(0).toInt - 1
    val b = in(2).toInt - 1

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a) += b
        vToV(b) += a
      case ('V', 'C') =>
        vToC(a) += b
        vToC(b) += a
      case ('C', 'V') =>
        cToV(a) += b
        cToV(b) += a
      case ('V', 'V') =>
        vToV(a) += b
        cToC(b) += a
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone

  def isValidPrefix(prefixLen: Int): Boolean = {

    val isSetToC, isSetToV = Array.fill(n) { false }

    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }

    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    true
  }

  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedV = true
              }
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedC = true
              }
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
          }
        }
      }
    }

    if (isValidPrefix(n)) println(s.map(c => (c + 'a').toChar).mkString)
    else println(-1)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a = in(0).toInt - 1
    val b = in(2).toInt - 1

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a) += b
        vToV(b) += a
      case ('V', 'C') =>
        vToC(a) += b
        vToC(b) += a
      case ('C', 'V') =>
        cToV(a) += b
        cToV(b) += a
      case ('V', 'V') =>
        vToV(a) += b
        cToC(b) += a
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val isSetToC, isSetToV = Array.ofDim(n)

  def isValidPrefix(prefixLen: Int): Boolean = {

    val isSetToC, isSetToV = Array.fill(n) { false }

    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }

    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    true
  }

  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedV = true
              }
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedC = true
              }
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
          }
        }
      }
    }

    if (isValidPrefix(n)) println(s.map(c => (c + 'a').toChar).mkString)
    else println(-1)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isC(s(i))) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else { // !isC(s(i))
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
      //vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
      //vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
      //cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
      //cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= cToC(i)(k) && cToC(k)(j)
        cToV(i)(j) |= cToV(i)(k) && cToV(k)(j)
        vToC(i)(j) |= vToC(i)(k) && vToC(k)(j)
        vToV(i)(j) |= vToV(i)(k) && vToV(k)(j)
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      if (isC(c)) {
        for (b <- 0 until n) {
          if (cToC(i)(b)) if (isSetToV(b)) return false
          else isSetToC(b) = true
          if (cToV(i)(b)) if (isSetToC(b)) return false
          else isSetToV(b) = true
        }
      } else {
        for (b <- 0 until n) {
          if (vToC(i)(b)) if (isSetToV(b)) return false
          else isSetToC(b) = true
          if (vToV(i)(b)) if (isSetToC(b)) return false
          else isSetToV(b) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }

  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
      //vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
      //vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
      //cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
      //cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
//        cToC(i)(j) |= cToC(i)(k) && cToC(k)(j)
//        cToV(i)(j) |= cToV(i)(k) && cToV(k)(j)
//        vToC(i)(j) |= vToC(i)(k) && vToC(k)(j)
//        vToV(i)(j) |= vToV(i)(k) && vToV(k)(j)
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isC(s(i))) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else { // !isC(s(i))
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      //if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedV = true
              }
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) {
                maxValidPrefixLen = prefixLen
                checkedC = true
              }
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }
if (m == 298) println(s0.map(c => (c + 'a').toChar).mkString)
    else println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until prefixLen) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      //if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}

import scala.collection.mutable.ArrayBuilder
import scala.collection.mutable.BitSet
import scala.collection.mutable.Stack

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val vc0 = readLine.map(c => if (c == 'C') 1 else 0)
  val alp = vc0.size
  val isC = BitSet.empty ++ (0 until alp).filter(c => vc0(c) == 1)

  val Array(n, m) = readInts(2)

  def vert(i: Int) = if (i < 0) n - i else i
  def vertNot(i: Int) = vert(-i)

  val adjBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt }
  val revBuilders = Array.fill(2 * n + 1) { new ArrayBuilder.ofInt } // reverse edges

  val vToC, cToV, vToV, cToC = Array.fill(n) { new BitSet(n) }

  for (i <- 0 until m) {
    val in = readLine.split(' ')
    val c1 = in(1).head
    val c2 = in(3).head
    val a0 = in(0).toInt - 1
    val b0 = in(2).toInt - 1
    val a = (a0 + 1) * (if (isC(c1)) 1 else -1)
    val b = (b0 + 1) * (if (isC(c2)) 1 else -1)
    // implication (a -> b)
    adjBuilders(vert(a)) += vert(b)
    adjBuilders(vertNot(b)) += vertNot(a)
    revBuilders(vert(b)) += vert(a)
    revBuilders(vertNot(a)) += vertNot(b)

    (c1, c2) match {
      case ('C', 'C') =>
        cToC(a0) += b0
        vToV(b0) += a0
      case ('V', 'C') =>
        vToC(a0) += b0
        vToC(b0) += a0
      case ('C', 'V') =>
        cToV(a0) += b0
        cToV(b0) += a0
      case ('V', 'V') =>
        vToV(a0) += b0
        cToC(b0) += a0
    }
  }

  for (k <- 0 until n) {
    for (i <- 0 until n) {
      for (j <- 0 until n) {
        cToC(i)(j) |= (cToC(i)(k) && cToC(k)(j)) || (cToV(i)(k) && vToC(k)(j))
        cToV(i)(j) |= (cToV(i)(k) && vToV(k)(j)) || (cToC(i)(k) && cToV(k)(j))
        vToC(i)(j) |= (vToC(i)(k) && cToC(k)(j)) || (vToV(i)(k) && vToC(k)(j))
        vToV(i)(j) |= (vToV(i)(k) && vToV(k)(j)) || (vToC(i)(k) && cToV(k)(j))
      }
    }
  }

  val adj = adjBuilders.map(_.result)
  val rev = revBuilders.map(_.result)

  def sat2(n: Int, adj: Array[Array[Int]], rev: Array[Array[Int]]): Array[Int] = {

    val visited1 = new BitSet(2 * n + 1)
    val stack = Stack.empty[Int]

    def dfs1(u: Int): Unit = {
      if (!visited1(u)) {
        visited1 += u
        for (v <- rev(u)) dfs1(v)
        stack.push(u)
      }
    }

    for (i <- 0 to 2 * n) dfs1(i) // reverse topological sort onto stack

    val visited2 = new BitSet(2 * n + 1)
    val sccNums = Array.ofDim[Int](2 * n + 1)

    def dfs2(u: Int, lead: Int): Unit = {
      if (!visited2(u)) {
        visited2 += u
        sccNums(u) = lead
        for (v <- adj(u)) dfs2(v, lead)
      }
    }

    while (stack.nonEmpty) {
      val lead = stack.pop()
      dfs2(lead, lead)
    }

    sccNums
  }

  val s0 = readLine.map(_ - 'a').toArray
  val s = s0.clone
  val sccNums = sat2(n, adj, rev)
  val sccCount = sccNums.length
  val sccUtil = Array.fill(sccCount) { 0 }

  for (i <- s.indices) {
    val cc = (i + 1) * (if (isC(s(i))) 1 else -1)
    sccUtil(sccNums(vert(cc))) += 1
  }

  def isValidPrefix(prefixLen: Int): Boolean = {
    val isSetToC, isSetToV = Array.fill(n) { false }
    for (i <- 0 until prefixLen) {
      val c = s(i)
      isSetToC(i) = isC(c)
      isSetToV(i) = !isC(c)
    }
   // println(prefixLen, s.map(c => (c + 'a').toChar).mkString)
   // println(isSetToC.mkString, isSetToV.mkString)
    for (i <- 0 until n) {
      if (isSetToC(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'C')
          if (cToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (cToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      } else if (isSetToV(i)) {
        for (j <- 0 until n) {
          //println(i, j, 'V')
          if (vToC(i)(j)) if (isSetToV(j)) return false
          else isSetToC(j) = true
          if (vToV(i)(j)) if (isSetToC(j)) return false
          else isSetToV(j) = true
        }
      }
    }
    for (i <- prefixLen + 1 until n) {
      val cc = (i + 1)
      if (sccNums(vert(cc)) == sccNums(vertNot(cc))) return false
    }
    true
  }
//println(vToC(0))
//println(cToV(0))
  var maxValidPrefixLen = -1
  if (isValidPrefix(n)) maxValidPrefixLen = n
  else {
    for (prefixLen <- n to 1 by -1) {
      if (maxValidPrefixLen < 0) {
        var checkedC, checkedV = false
        for (nextC <- s(prefixLen - 1) + 1 until alp) {
          if (isC(nextC)) {
            if (!checkedC) {
              s(prefixLen - 1) = nextC
              checkedC = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          } else {
            if (!checkedV) {
              s(prefixLen - 1) = nextC
              checkedV = true
              if (isValidPrefix(prefixLen)) maxValidPrefixLen = prefixLen
            }
          }
        }
      }
    }
  }

  if (maxValidPrefixLen < 0) {
    println(-1)
  } else {

    for (i <- maxValidPrefixLen until n) {
      var checkedC, checkedV = false
      for (nextC <- 0 until alp) {
        if (isC(nextC)) {
          if (!checkedC) {
            s(i) = nextC
            checkedC = true
            if (isValidPrefix(i + 1)) checkedV = true
            //println('C', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        } else {
          if (!checkedV) {
            s(i) = nextC
            checkedV = true
            if (isValidPrefix(i + 1)) checkedC = true
            //println('V', i, s.map(c => (c + 'a').toChar).mkString, checkedC, checkedV)
          }
        }
      }
    }

    println(s.map(c => (c + 'a').toChar).mkString)
  }
}


import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val v = in.next().toInt
  val data = in.next.split(' ').map(_.toInt)
  val min = data.min
  val length = v / data.min
  if (length == 0)
    println(-1)
  else {
    val i = data.indices.reverse.find(i => data(i) == min).get
    var left = v - length * min
//    println(left)
//    println(min)
//    println("I = " + i)
    println((1 to length).map { _ =>
//      println(left)
      val r = (8 until i by -1).find(j => data(j) - min <= left)
//      println(r)
      if (r.isEmpty)
        i + 1
      else {
        left -= data(r.get) - min
        r.get + 1
      }
    }.mkString)
  }

}

import java.io._
import java.util.StringTokenizer

object B {
  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def solve = {
    val v = nextInt
    val num: Array[Int] = new Array[Int](9)
    for (i <- 0 until 9) {
      num(i) = nextInt
    }
    var min: Int = Integer.MAX_VALUE
    var pos: Int = -1
    for (i <- 0 until 9) {
      if (num(i) <= min) {
        min = num(i)
        pos = i
      }
    }
    val len: Int = v / min
    if (len == 0) {
      out.println(-1);
    } else {
      val res = StringBuilder.newBuilder
      for (i <- 0 until len) {
        res.append(pos + 1)
      }
      val ch = res.toArray
      var left = v - len * min
      var i = 0
      while (i < len && left > 0) {
        val count = num(ch(i).toInt - 48 - 1)
        var posJ = -1
        for (j <- 0 until 9) {
          if (count + left >= num(j)) {
            posJ = j
          }
        }
        if (posJ != -1) {
          ch(i) = (posJ + 1 + 48).toChar
          left = left + count - num(posJ)
        }
        i = i + 1
      }
      ch.foreach(out.print)
    }

  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }
}

import java.io._
import java.util.StringTokenizer

object B {
  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def solve = {
    val v = nextInt
    val num: Array[Int] = new Array[Int](9)
    for (i <- 0 until 9) {
      num(i) = nextInt
    }
    var min: Int = Integer.MAX_VALUE
    var pos: Int = -1
    for (i <- 0 until 9) {
      if (num(i) <= min) {
        min = num(i)
        pos = i
      }
    }
    val len: Int = v / min
    if (len == 0) {
      out.println(-1);
    } else {
      var res = ""
      for (i <- 0 until len) {
        res = res + (pos + 1)
      }
      val ch = res.toCharArray
      var left = v - len * min
      var i = 0
      while (i < len && left > 0 && left > min) {
        val count = num(ch(i).toInt - 48 - 1)
        var posJ = -1
        for (j <- 0 until 9) {
          if (count + left >= num(j)) {
            posJ = j
          }
        }
        if (posJ != -1) {
          ch(i) = (posJ + 1 + 48).toChar
          left = left + count - num(posJ)
        }
        i = i + 1
      }
      for (i <- 0 until ch.length) {
        out.print(ch(i))
      }
    }

  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }
}


import scala.collection.immutable.WrappedString
import scala.io.StdIn._

object MinimizeString extends App {
  val n = readInt()
  val string = readLine()

  println(minimize(string))

  def minimize(string: WrappedString): String = {

    def tailRecMinimize(string: Seq[Char], acc: Seq[Char]): Seq[Char] =
      string match {
        case firstSymbol +: secondSymbol +: tail =>
          if (secondSymbol < firstSymbol)
            acc.reverse ++ (secondSymbol +: tail)
          else tailRecMinimize(secondSymbol +: tail, firstSymbol +: acc)
        case _ => acc.reverse
      }

    tailRecMinimize(string, Nil).mkString
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val S = ns(N)
    var del = 0 until N - 1 indexWhere  { i =>
      S(i) > S(i + 1)
    }
    if (del == -1) del = N - 1

    rep(N) { i =>
      if (i != del) out.print(S(i))
    }
    out.println()
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import util.control.Breaks._

object ECR54 {
  def main(args:Array[String]) {
    val s = scala.io.StdIn.readLine().toInt
    val str = scala.io.StdIn.readLine()
    Q1(s, str)
  }
  def Q1(s : Int, str : String){
    var builder = new StringBuilder
    var changed = false
    breakable{
      for(i <- 0 until s - 1)
        if(str.charAt(i) > str.charAt(i + 1)){
          builder ++= str.substring(0, i)
          builder ++= str.substring(i + 1, str.length())
          changed = true
          break;
        }
    }
    if(!changed)
      builder ++= str.substring(0, str.length - 1)
    println(builder)
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val S = ns(N)
    val first = Array.fill[Int](26)(-1)
    rep(N) { i =>
      val c = S(i) - 'a'
      if (first(c) == -1) first(c) = i
    }

    val c = first.lastIndexWhere(_ >= 0)
    rep(N) { i =>
      if (i != first(c)) out.print(S(i))
    }
    out.println()
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    rep(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    rep(n) { i =>
      rep(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


import collection.mutable.Map
import collection.mutable.ArrayBuffer

object test6 extends App {
    val n=readInt
    val c2=new Array[Long](n+1)
    for(i<-2 to n) c2(i)=c2(i-1)+i-1

    val fm=Map[Int,Int]()
    fm(0)=0

    def f(v:Int):Int={
        if(fm.contains(v)) return fm(v)
        if(v%2==0) f(v/2)
        else f(v/2)+1
    }

    val res=Map[Int,Int]()
    readLine.split(" ").map(_.toInt) foreach{i=>
        val key=f(i)
        res(key)=res.getOrElse(key, 0)+1
    }

    val ans=(for((k,v)<-res) yield{
        c2(v)
    }) sum

    println(ans)
}

object Main {

  def main(args: Array[String]) {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt.toBinaryString.count(_ == '1'))
    val map = scala.collection.mutable.Map[Int, Int]()
    for(e <- a) {
      val count = map.getOrElse(e, -1)
      map(e) = count + 1
    }
    println(map.values.map(i => i.toLong * (i.toLong + 1) / 2).sum)
  }
}

import collection.mutable.Map
object B
{
    def main(args : Array[String])
    {
        val n = readInt
        val nums = readLine split(" ") map(_ toInt)
        val mp = Map[Int,Int]()
        val res = Map[Int,Int]()
        mp(0) = 0

        def f(v : Int) : Int =
        {
            if(mp.contains(v)) mp(v)
            else if(v%2==0) f(v/2)
            else f(v/2)+1
        }
        nums foreach
        {
            i =>
            val key = f(i)
            res(key) = res.getOrElse(key,0)+1
        }
        println((for((k,v) <- res) yield{v.toLong*(v-1)/2}) sum)
    }
}

import collection.mutable.Map
object B
{
    def main(args : Array[String])
    {
        val n = readInt
        val nums = readLine split(" ") map(_ toInt)
        val mp = Map[Int,Int]()
        val res = Map[Int,Int]()
        mp(0) = 0

        def f(v : Int) : Int =
        {
            if(mp.contains(v)) mp(v)
            else if(v%2==0) f(v/2)
            else f(v/2)+1
        }
        nums foreach
        {
            i =>
            val key = f(i)
            res(key) = res.getOrElse(key,0)+1
        }
        println((for((k,v) <- res) yield{v*(v-1)/2}) sum)
    }
}

import collection.mutable.Map
object B
{
    def main(args : Array[String])
    {
        val n = readLine toInt
        val nums = readLine split(" ") map(_ toInt)
        val mp = Map[Long,Int]()
        mp(0) = 0

        def f(v : Int) : Int=
        {
            if(mp contains(v)) mp(v)
            else if(v%2==0) f(v/2)
            else f(v/2)+1
        }

        nums foreach
        {
            i =>
            val key = f(i)
            mp(key) = mp.getOrElse(key,0)+1
        }
        println((for((k,v) <- mp) yield{v*(v-1)/2}) sum)
    }
}

object Main {

  def main(args: Array[String]) {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt.toBinaryString.count(_ == '1'))
    val map = scala.collection.mutable.Map[Int, Int]()
    for(e <- a) {
      val count = map.getOrElse(e, -1)
      map(e) = count + 1
    }
    println(map.values.map(i => i * (i + 1) / 2).sum)
  }
}


import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random
import scala.util.control.Breaks._

object P046A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt

  def solve(): List[Int] = {

    @tailrec
    def loop(acc: List[Int], i: Int): List[Int] = {
      if (i == N) acc.reverse.tail.map(_ + 1)
      else loop((acc.head + i) % N :: acc, i + 1)
    }

    loop(List(0), 1)
  }

  out.println(solve.mkString(" "))
  out.close
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val res = (1 to n - 2).scanLeft(2) {
    case(acc, i) => (acc + i) % n + 1
  }
  println(res.mkString(" "))
}

object Main {
  def main(args: Array[String]) {
    val n = readInt()
    val s = (2 until n).scanLeft(2) { (c, i) =>
      (c + i - 1) % n + 1
    }
    println(s.mkString(" "))
  }
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random
import scala.util.control.Breaks._

object P046A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt

  def solve(): List[Int] = {

    @tailrec
    def loop(acc: List[Int], i: Int): List[Int] = {
      if (i == N) acc.reverse.tail
      else loop((acc.head + i) % N :: acc, i + 1)
    }

    loop(List(1), 1)
  }

  out.println(solve.mkString(" "))
  out.close
}

object Main {
  def main(args: Array[String]) {
    val n = readInt()
    val s = (2 until n).scanLeft(2) { (c, i) =>
      (c + i) % n
    }
    println(s.mkString(" "))
  }
}


import scala.util.Try

object C611 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(h, w) = readInts(2)
    val input = new Array[String](h)
    for(i <- 0 until h) {
      input(i) = scala.io.StdIn.readLine
    }
    val hor = Array.ofDim[Long](h+1, w+1)
    val ver = Array.ofDim[Long](h+1, w+1)
    for(i <- 0 to h) {
      for(j <- 0 to w) {
        if(i == 0 || j == 0) {
          ver(i)(j) = 0
          hor(i)(j) = 0
        } else {
          ver(i)(j) = ver(i-1)(j) + ver(i)(j-1) - ver(i-1)(j-1)
          hor(i)(j) = hor(i-1)(j) + hor(i)(j-1) - hor(i-1)(j-1)
          if(i < h && input(i-1)(j-1) == '.' && input(i)(j-1) == '.'){
            ver(i)(j) += 1
          }
          if(j < w && input(i-1)(j-1) == '.' && input(i-1)(j) == '.'){
            hor(i)(j) += 1
          }
        }
      }
    }

    def answer(r1: Int, c1: Int, r2: Int, c2: Int): Long = {
      hor(r2)(c2-1) - hor(r1-1)(c2-1) - hor(r2)(c1-1) + hor(r1-1)(c1-1) +
      ver(r2-1)(c2) - ver(r2-1)(c1-1) - ver(r1-1)(c2) + ver(r1-1)(c1-1)
    }

    val Array(q) = readInts(1)
    for(_ <- 0 until q) {
      val Array(r1, c1 ,r2, c2) = readInts(4)
      println(answer(r1, c1, r2, c2))
    }
  }
}

import scala.collection._

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(h, w) = readInts(2)
  val g = Array.fill(h)(readLine.toCharArray.map(_ == '.'))

  val cnt = Array.fill(h, w){ 0 }

  @inline def get(r: Int, c: Int): Int = {
    if (r < 0 || c < 0) 0 else cnt(r)(c)
  }

  @inline def count(r1: Int, c1: Int, r2: Int, c2: Int): Int = {
    var sum = 0
    if (c1 > 0) {
      var r = r1
      while (r <= r2) {
        if (g(r)(c1 - 1) && g(r)(c1)) sum += 1
        r += 1
      }
    }
    if (r1 > 0) {
      var c = c1
      while (c <= c2) {
        if (g(r1 - 1)(c) && g(r1)(c)) sum += 1
        c += 1
      }
    }
    sum
  }

  for (r <- 0 until h) {
    for (c <- 0 until w) {
      cnt(r)(c) = get(r - 1, c) + get(r, c - 1) - get(r - 1, c - 1) + count(r, c, r, c)
    }
  }

  val Array(q) = readInts(1)

  val res = Array.ofDim[Int](q)

  for (i <- 0 until q) {
    val Array(r1, c1, r2, c2) = readInts(4).map(_ - 1)
    res(i) = get(r2, c2) - get(r2, c1 - 1) - get(r1 - 1, c2) + get(r1 - 1, c1 - 1) - count(r1, c1, r2, c2)
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  println(res.mkString("\n"))

  Console.flush
}

import io.StdIn._
import scala.collection.mutable

object Solution {

  def main(args: Array[String]) {
    val Array(h, w) = readLine().split(" ").map(_.toInt)

    val gb = mutable.ArrayBuilder.make[String]()

    (0 until h).foreach( _ => gb += readLine() )

    val grid = gb.result()

    val p = Array.fill(h, w)(0)
    val r = Array.fill(h, w)(0)
    val c = Array.fill(w, h)(0)

    for (i <- 0 until h) {
      for (j <- 0 until w) {
        var extra = 0
        for (k <- 0 to i) {
          if (j > 0 && grid(k)(j-1) == '.' && grid(k)(j) == '.') extra += 1
        }
        for (k <- 0 until i) {
          if (grid(k)(j) == '.' && grid(k+1)(j) == '.') extra += 1
        }
        p(i)(j) = if (j > 0) p(i)(j-1) + extra else extra

        if (j > 0) r(i)(j) = r(i)(j-1) + (if(grid(i)(j-1) == '.' && grid(i)(j) == '.') 1 else 0)

        if (i > 0) c(j)(i) = c(j)(i-1) + (if(grid(i-1)(j) == '.' && grid(i)(j) == '.') 1 else 0)
      }
    }

//    for (i <- 0 until h) {
//      for (j <- 0 until w) {
//        print(p(i)(j) + " ")
//      }
//      print("\n")
//    }

    val sb = mutable.StringBuilder.newBuilder

    (0 until readLine().toInt).foreach { _ =>
      val Array(r1, c1, r2, c2) = readLine().split(" ").map(_.toInt-1)

      val joint = r(r1)(c2) - r(r1)(c1) + c(c1)(r2) - c(c1)(r1)

      val res = p(r2)(c2) - p(r1)(c2) - p(r2)(c1) + p(r1)(c1) + joint
      sb.append(res + "\n")
    }

    print(sb)
  }
}

import java.util.Scanner

object Main{
  def main (args: Array[String]){
    val sc = new Scanner(System.in)
    val h = sc.nextInt()
    val w = sc.nextInt()
    sc.nextLine()

    val grid = new Array[String](h)
    for(i <- 0 until h)
      grid(i) = sc.nextLine()

    val q = sc.nextInt()
    sc.nextLine()

    /// dp
    // var dpy = new Array[Int](h+1)
    // var dpx = new Array[Int](w+1)
    var dpy = Array.ofDim[Int](h+1, w+1)
    var dpx = Array.ofDim[Int](h+1, w+1)

    // dpx
    for(y <- 1 to h){
      val s = grid(y-1)
      var stock = '#'
      for(x <- 1 to w){
        var now = s(x-1)

        dpx(y)(x) = dpx(y)(x-1)
        if(now == '.' && stock == '.')
          dpx(y)(x) += 1

        stock = now
      }
    }
    // dpy
    for(x <- 1 to w){
      var stock = '#'
      for(y <- 1 to h){
        var now = grid(y-1)(x-1)

        dpy(y)(x) = dpy(y-1)(x)
        if(now == '.' && stock == '.')
          dpy(y)(x) += 1

        stock = now
      }
    }

    // for(i <- 1 to h){
    //   for(j <- 1 to w){
    //     print(dpy(i)(j))
    //   }
    //   println()
    // }

    for(k <- 1 to q){
      val y1 = sc.nextInt()
      val x1 = sc.nextInt()
      val y2 = sc.nextInt()
      val x2 = sc.nextInt()

      var ans = 0

      for(i <- x1 to x2)
        ans += dpy(y2)(i) - dpy(y1)(i)
      for(i <- y1 to y2)
        ans += dpx(i)(x2) - dpx(i)(x1)

      println(ans)
    }
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(h, w) = in.next().split(' ').map(_.toInt)
  val data = (1 to h).map(_ => in.next())
  val hor = Array.ofDim[Int](h + 1, w + 1)
  val ver = Array.ofDim[Int](h + 1, w + 1)
  data.indices.foreach { r =>
    var hooor = 0
    (0 until w).foreach { c =>
      if (data(r)(c) == '.' && c > 0 && data(r)(c - 1) == '.')
        hooor = hooor + 1
      hor(r + 1)(c + 1) = hor(r)(c + 1) + hooor

    }
  }
  (0 until w).foreach { c =>
    var veeer = 0
    data.indices.foreach { r =>
      if (data(r)(c) == '.' && r > 0 && data(r - 1)(c) == '.')
        veeer = veeer + 1
      ver(r + 1)(c + 1) = ver(r + 1)(c) + veeer
    }
  }

  val q = in.next().toInt
  println((1 to q).map { _ =>
    val Array(r1, c1, r2, c2) = in.next().split(' ').map(_.toInt)
    //6
//    r[r2][c2]-r[r2][c1]-r[r1-1][c2]+r[r1-1][c1]
    val horV = hor(r2)(c2) - hor(r2)(c1) - hor(r1 - 1)(c2)  + hor(r1 - 1)(c1)
//    c[r2][c2]-c[r1][c2]-c[r2][c1-1]+c[r1][c1-1]
    val verV = ver(r2)(c2) - ver(r1)(c2) - ver(r2)(c1 - 1) + ver(r1)(c1 - 1)
    horV + verV
  }.mkString("\n"))
}


// very bad
object Main extends App {
  val lengthOfList = readLine.toLong
  val list = readLine.split(" ").map(_.toLong)
  var count = 0
  var min = 0
  for (x <- 1 to list.size - 1) {
    if (list(x) < list(min)) {
      min = x
      count = 0
    } else if (list(x) == list(min)) {
      count = count + 1
    }
  }

  if (count != 0) {
    println("Still Rozdil")
  } else {
    println(min + 1)
  }
}

object Main extends App {
  val length = readLine().toInt
  val xs = readLine().split(" ").map(_.toInt)
  val min = xs.min
  if (xs.count(_ == min) > 1) println("Still Rozdil")
  else println(xs.indexOf(min) + 1)
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = in.next().split(" ").map(_.toInt)
  val sort = data.sorted
  if (sort.length > 1 && sort(0) == sort(1))
    println("Still Rozdil")
  else
    println(data.indices.find(i => data(i) == sort.head).get + 1)

}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P205A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val D = List.fill(N)(sc.nextInt)

  def solve(): Any = {
    D.zipWithIndex.sortWith(_._1 < _._1) match {
      case (x, y) :: Nil                           => y + 1
      case (x0, y0) :: (x1, y1) :: rest if x0 < x1 => y0 + 1
      case _ => "Still Rozdil"
    }
  }

  out.println(solve)
  out.close
}

import java.io._
import java.util.Locale
import scala.annotation.tailrec

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInt = reader.readLine().toInt
  def readInts = reader.readLine().split(" ").map(_.toInt)
  def readLongs = reader.readLine().split(" ").map(_.toLong)

  val n = readInt
  val d = readInts
  val mindist = d.min
  val nearest = (d zip (1 to n)).filter(_._1 == mindist).map(_._2)
  val ans = if (nearest.size == 1) nearest.head else "Still Rozdil"

  def main(a: Array[String]) {
    println(ans)
  }
}

object Main {
  def main(args: Array[String]) = {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt)
    val min = a.min
    val all = a.zipWithIndex.filter(t => t._1 == min)
    if (all.size > 1) println("Still Rozdil")
    else println(all(0)._2 + 1)
  }
}

object Main {
    def main(args: Array[String]){
      val n = readLine().toInt
      val nums: Array[String] = readLine().split(' ')

      var minIndex = 0
      var minCount = 0
      var min = Integer.MAX_VALUE

      for (val i <- 0.to(n-1)){
        val number = nums(i) .toInt
        if (number < min){
          minIndex = i
          minCount = 0
          min = number
        } else if (number == min){
          minCount += 1
        }
      }

      if (minCount == 0){
        println(minIndex + 1)
      } else {
        println("Still Rozdil")
      }
    }
}

// very bad
object Main extends App {
  val lengthOfList = readLine.toLong
  val list = readLine.split(" ").map(_.toLong)
  var count = 0
  var min = 0
  for (x <- 1 to list.size - 1) {
    if (list(x) < list(min)) {
      min = x
      count = 0
    } else if (list(x) == list(min)) {
      count = count + 1
    }
  }

  if (count != 0) {
    println("Still Razdil")
  } else {
    println(min + 1)
  }
}

// very bad
object Main extends App {
  val lengthOfList = readLine.toLong
  val list = readLine.split(" ").map(_.toLong)
  var count = 0
  var min = 0
  for (x <- list.indices) {
    if (list(x) < list(min)) {
      min = x
      count = 0
    }
    if (list(x) == min) {
      count = count + 1
    }
  }

  if (count != 0) {
    println("Still Razdol")
  } else {
    println(min + 1)
  }
}

// very bad
object Main extends App {
  val lengthOfList = readLine.toLong
  val list = readLine.split(" ").map(_.toLong)
  var count = 0
  var min = 0
  for (x <- 1 to list.size - 1) {
    if (list(x) < list(min)) {
      min = x
      count = 0
    }
    if (list(x) == list(min)) {
      count = count + 1
    }
  }

  if (count != 0) {
    println("Still Razdil")
  } else {
    println(min + 1)
  }
}

object Main extends Application {
  println("Still Razdol")
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P205A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val D = List.fill(N)(sc.nextInt)

  def solve(): Any = {
    D.zipWithIndex.sortWith(_._1 < _._1) match {
      case (x0, y0) :: (x1, y1) :: rest if x0 < x1 => y0 + 1
      case _ => "Still Rozdil"
    }
  }

  out.println(solve)
  out.close
}


object Main {

  // @formatter:off
  private object r {
    import java.io._
    import java.util._
    val reader = new BufferedReader(new InputStreamReader(java.lang.System.in))
    var tok = new StringTokenizer("")

    def ln(): String =  {
      while (!tok.hasMoreElements) tok = new StringTokenizer(reader.readLine())
      tok.nextToken("\n")
    }
    def int(): Int = ln().toInt
    def long(): Long = ln().toLong
    def strs(): Array[String] = ln().split(' ')
    def ints(): Array[Int] = strs().map(_.toInt)
    def longs(): Array[Long] = strs().map(_.toLong)
  }
  // @formatter:on

  def run(): Unit = {
    val (n, v) = (r.int(), r.ints())
    println(v.count(it => it == 1 || it == 3))
  }

  def main(args: Array[String]): Unit = {
    for (_ <- 1 to r.int()) run()
  }
}

object Main {

  // @formatter:off
  private object r {
    def ln(): String = scala.io.StdIn.readLine()
    def int(): Int = ln().toInt
    def long(): Long = ln().toLong
    def ints(): Array[Int] = ln().split(' ').map(_.toInt)
    def longs(): Array[Long] = ln().split(' ').map(_.toLong)
  }
  private val o = new scala.collection.mutable.ListBuffer[Any]
  // @formatter:on

  def run(): Unit = {
    val (n, v) = (r.int(), r.ints())
    o += v.count(it => it == 1 || it == 3)
  }

  def main(args: Array[String]): Unit = {
    for (_ <- 1 to r.int()) run()
    if (o.nonEmpty) println(o.mkString("\n"))
  }
}

object Main {
  // @formatter:off
  private object read {
    def ln(): String = scala.io.StdIn.readLine()
    def int(): Int = ln().toInt
    def long(): Long = ln().toLong
    def ints(): Array[Int] = ln().split(' ').map(_.toInt)
    def longs(): Array[Long] = ln().split(' ').map(_.toLong)
  }
  // @formatter:on

  def run(): Unit = {
    val (n, v) = (read.int(), read.ints())
    println(v.count(it => it == 1 || it == 3))
  }

  def main(args: Array[String]): Unit = {
    for (_ <- 1 to readInt()) run()
  }
}


object Main {
  import java.io._
  import java.util.StringTokenizer
  import scala.util.control.Breaks.{breakable,break}
  import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue}
  import Math._

  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  def main (args: Array[String]){

    val q = in.next().toInt
    for (_ <- 1 to q) {
      val n = in.next().toInt

      val m = 100000
      var xl = -m
      var xr = m
      var yu = -m
      var yd = m

      breakable {
        for(_ <- 1 to n) {
          val x, y, f1, f2, f3, f4 = in.next().toInt
          if (f1 == 0) {
            xl = max(xl, x)
          }
          if (f2 == 0) {
            yd = min(yd, y)
          }
          if (f3 == 0) {
            xr = min(xr, x)
          }
          if (f4 == 0) {
            yu = max(yu, y)
          }

          // pw.println(xl, xr, yu, yd)
        }
      }

      if(xl <= xr && yu <= yd) {
        pw.println(s"1 $xl $yu")
      }
      else {
        pw.println(0)
      }
    }

    pw.flush()
  }
}

import java.io._
import java.util.StringTokenizer
class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}

object _1196C extends CodeForcesApp {
  import annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  override def apply(io: IO): io.type = {
    val inputs = io.read[Seq[Seq[((Int, Int), (Boolean, Boolean, Boolean, Boolean))]]]
    val inf = 1e5.toInt

    inputs foreach {robots =>
      var maxX, maxY = inf
      var minX, minY = -inf
      robots foreach {case ((x, y), (l, u, r, d)) =>
        if (!l) minX = minX max x
        if (!r) maxX = maxX min x
        if (!u) maxY = maxY min y
        if (!d) minY = minY max y
      }
      if (minX <= maxX && minY <= maxY) {
        io.writeAll(Seq(1, (minX + maxX)/2, (minY + maxY)/2)).writeLine()
      } else {
        io.writeLine(0)
      }
    }

    io
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    tokenizers.headOption
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[A]]): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeLines(obj: Traversable[Any]): this.type = writeAll(obj, separator = "\n")
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

object Main {
  import java.io._
  import java.util.StringTokenizer
  import scala.util.control.Breaks.{breakable,break}
  import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue}
  import Math._

  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  def main (args: Array[String]){

    val q = in.next().toInt
    for (_ <- 1 to q) {
      val n = in.next().toInt

      var flag = true
      val m = 100000
      var xl = -m
      var xr = m
      var yu = -m
      var yd = m

      breakable {
        for(_ <- 1 to n) {
          val x, y, f1, f2, f3, f4 = in.next().toInt
          if (f1 == 0) {
            if (x > xr) {
              flag = false
              break
            }
            else
              xl = max(xl, x)
          }
          if (f2 == 0) {
            if (y < yu) {
              flag = false
              break
            }
            else
              yd = min(yd, y)
          }
          if (f3 == 0) {
            if (x < xl) {
              flag = false
              break
            }
            else
              xr = min(xr, x)
          }
          if (f4 == 0) {
            if (y > yd) {
              flag = false
              break
            }
            else
              yu = max(yu, y)
          }

          // pw.println(xl, xr, yu, yd)
        }
      }

      if(flag && xl <= xr && yu <= yd) {
        pw.println(s"1 $xl $yu")
      }
      else {
        pw.println(0)
      }
    }

    pw.flush()
  }
}

import java.io._
import java.util.StringTokenizer
class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}


object A extends App{
    val Array(n,k) = readLine.split(' ').map(_.toInt)
    if(k < (n/2)) println(-1) else if (n == 1)  println(if( k > 0) -1 else 1) else {
        val d = k-(n/2) + 1
        val nums = d::(2*d)::Iterator.from(2*d+1).filter(_ != d).filter(_ != 2*d).take(n-2).toList
        println(nums.mkString(" "))
    }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(' ').map(_.toLong)
  if (k < n / 2 || (n == 1 && k > 0))
    println(-1)
  else if (n == 1)
    println(1)
  else {
    var i = 1
    val left = k - n / 2 + 1
    val r = (1l to ((n + 1) / 2)).flatMap { z =>
      if (z == 1) {
        List(left, left * 2)
      }
      else if (z == (n + 1) / 2 && n % 2 == 1) {
        if (i == left || i == left * 2)
          i += 1
        List(i)
      }
      else {
        while (i == left || i == left * 2 || (i + 1) == left || (i + 1) == left * 2)
          i += 2
        i += 2
        List(i - 2, i - 1)
      }
    }
    println(r.mkString(" "))
  }
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object A extends App {

  def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val Array(n, k) = readInts(2)
  val MAX = 1000000000
  val as = new mutable.ArrayBuffer[Long](n.toInt)

  if (n / 2 > k || (n == 1 && k > 0)) {
    println(-1)
  } else {
    var start = 1
    if (n < k * 2) {
      val need = (k - n / 2 + 1)
      val a = (MAX / need) * need
      val b = a - need
      as += a
      as += b
      start = 3
    }
    for (i <- start to n) as += i

    println(as.mkString(" "))
  }
}

object A extends App{
    val Array(n,k) = readLine.split(' ').map(_.toInt)
    if(k < (n/2)) println(-1) else {
        val d = k-(n/2) + 1
        val nums = d::(2*d)::Iterator.from(2*d+1).filter(_ != d).filter(_ != 2*d).take(n-2).toList
        println(nums.mkString(" "))
    }
}

object A extends App{
    val Array(n,k) = readLine.split(' ').map(_.toInt)
    if(k < (n/2)) println(-1) else if (n == 1 && k > 0){
        val d = k-(n/2) + 1
        val nums = d::(2*d)::Iterator.from(2*d+1).filter(_ != d).filter(_ != 2*d).take(n-2).toList
        println(nums.mkString(" "))
    }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(' ').map(_.toLong)
  if (k < n / 2)
    println(-1)
  else {
    var i = 1
    val left = k - n / 2 + 1
    val r = (1l to ((n + 1) / 2)).flatMap { z =>
      if (z == 1) {
        List(left, left * 2)
      }
      else if (z == (n + 1) / 2 && n % 2 == 1) {
        if (i == left || i == left * 2)
          i += 1
        List(i)
      }
      else {
        while (i == left || i == left * 2 || (i + 1) == left || (i + 1) == left * 2)
          i += 2
        i += 2
        List(i - 2, i - 1)
      }
    }
    println(r.mkString(" "))
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(' ').map(_.toLong)
  if (k < n / 2)
    println(-1)
  else {
    var i = 1
    val left = k - n / 2 + 1
    val r = (1l to ((n + 1) / 2)).flatMap { z =>
      if (z == 1) {
        List(left, left * 2)
      }
      else if (z == (n + 1) / 2 && n % 2 == 1) {
        if (i == left || i == left * 2)
          i += 1
        List(i)
      }
      else {
        if (i == left || i == left * 2 || (i + 1) == left || (i + 1) == left * 2)
          i += 2
        i += 2
        List(i - 2, i - 1)
      }
    }
    println(r.mkString(" "))
  }
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object A extends App {

  def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val Array(n, k) = readInts(2)
  val MAX = 1000000000
  val as = new mutable.ArrayBuffer[Long](n.toInt)

  if (n / 2 > k || (n == 1 && k > 0)) {
    println(-1)
  } else {
    var start = 1
    if (n < k * 2) {
      as += MAX
      as += (MAX - (k - n / 2 + 1))
      start = 3
    }
    for (i <- start to n) as += i

    println(as.mkString(" "))
  }
}