Alice has written a program and now tries to improve its readability. One of the ways to improve readability is to give sensible names to the variables, so now Alice wants to rename some variables in her program. In her IDE there is a command called "massive refactoring", which can replace names of many variable in just one run. To use it, Alice needs to select two strings $$$s$$$ and $$$t$$$ and after that for each variable the following algorithm is performed: if the variable's name contains $$$s$$$ as a substring, then the first (and only first) occurrence of $$$s$$$ is replaced with $$$t$$$. If the name doesn't contain $$$s$$$, then this variable's name stays the same.The list of variables is known and for each variable both the initial name and the name Alice wants this variable change to are known. Moreover, for each variable the lengths of the initial name and the target name are equal (otherwise the alignment of the code could become broken). You need to perform renaming of all variables in exactly one run of the massive refactoring command or determine that it is impossible.

The first line contains the only integer $$$n$$$ ($$$1 \le n \le 3000$$$) — the number of variables in Alice's program. The following $$$n$$$ lines contain the initial names of variables $$$w_1, w_2, \ldots, w_n$$$, one per line. After that, $$$n$$$ more lines go, the $$$i$$$-th of them contains the target name $$$w'_i$$$ for the $$$i$$$-th variable. It is guaranteed that $$$1 \le |w_i| = |w'_i| \le 3000$$$. It is guaranteed that there is at least one variable having its target name different from the initial name. Both initial and target names consist of lowercase English letters only. For each variable the length of its initial name is equal to the length of its target name.

If it is impossible to rename all variables with one call of "massive refactoring", print "NO" (quotes for clarity). Otherwise, on the first line print "YES" (quotes for clarity) and on the following lines print $$$s$$$ and $$$t$$$ ($$$1 \le |s|, |t| \le 5000$$$), which should be used for replacement. Strings $$$s$$$ and $$$t$$$ should consist only of lowercase letters of English alphabet. If there are multiple ways to perform a "massive refactoring", you can use any of them.

import java.util

import scala.collection.mutable

// https://codeforces.com/contest/1055/problem/D
object DAhoCorasic extends App {

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

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine.toCharArray }

  val isDiff = Array.ofDim[Boolean](n)
  var i = 0
  var first = -1
  while (i < n) {
    if (!util.Arrays.equals(as(i), bs(i))) {
      first = i
      isDiff(i) = true
    }
    i += 1
  }

  val aFirst = as(first)
  val bFirst = bs(first)
  var lMax = 0
  while (aFirst(lMax) == bFirst(lMax)) lMax += 1
  var rMin = aFirst.length - 1
  while (aFirst(rMin) == bFirst(rMin)) rMin -= 1
  var lFirst = 0
  var rFirst = aFirst.length - 1
  var s = as(first)
  var t = bs(first)
  var ahoCorasic = AhoCorasic(s)
  val sMin = s.slice(lMax, rMin + 1)
  val tMin = t.slice(lMax, rMin + 1)

  def patch(x: Array[Char], y: Array[Char], pos: Int): Array[Char] = {
    val ss = x.clone()
    var i = 0
    while (i < y.length) {
      ss(pos + i) = y(i)
      i += 1
    }
    ss
  }

  for (i <- 0 until n) {
    if (isDiff(i)) {
      val a = as(i)
      val b = bs(i)
      val pos = ahoCorasic.findIn(a) - s.length + 1
      val mod = if (pos < 0) a
      else patch(a, t, pos)
      if (!util.Arrays.equals(mod, b)) {
        var l = 0
        while (a(l) == b(l)) l += 1
        var r = a.length - 1
        while (a(r) == b(r)) r -= 1
        if (r - l == rMin - lMax /* && util.Arrays.equals(a.slice(l, r + 1), sMin) && util.Arrays.equals(b.slice(l, r + 1), tMin)*/) {
          var ll = lMax
          while (l > 0 && ll > lFirst && a(l - 1) == aFirst(ll - 1) && b(l - 1) == bFirst(ll - 1)) {
            l -= 1
            ll -= 1
          }
          var rr = rMin
          while (r < a.length - 1 && rr < rFirst && a(r + 1) == aFirst(rr + 1) && b(r + 1) == bFirst(rr + 1)) {
            r += 1
            rr += 1
          }
          lFirst = ll
          rFirst = rr
          if (rr - ll + 1 != s.length) {
            s = aFirst.slice(ll, rr + 1)
            t = bFirst.slice(ll, rr + 1)
            ahoCorasic = AhoCorasic(s)
          }
        }
      }
    }
  }

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = ahoCorasic.findIn(as(i)) - s.length + 1
      val mod = if (pos < 0) as(i)
      else patch(as(i), t, pos)
      if (!util.Arrays.equals(mod, bs(i))) ok = false
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))
  if (ok) {
    println("YES")
    println(s.mkString)
    println(t.mkString)
  } else println("NO")
  Console.flush()

  import AhoCorasic._

  class AhoCorasic private(private val trieRoot: TrieNode) {

    def newMatcher: Matcher = new Matcher

    def findIn(t: Array[Char]): Int = {
      var currentNode = trieRoot
      var i = 0
      do {
        currentNode = currentNode.children(t(i) - 'a')
        i += 1
      } while (currentNode.matchFor < 0 && i < t.length)
      if (currentNode.matchFor == 0) i - 1 else -1
    }

    class Matcher private[AhoCorasic] () {

      private var currentNode = trieRoot

      def apply(rawCh: Byte): Int = {
        val ch = java.lang.Byte.toUnsignedInt(rawCh)
        currentNode = currentNode.children(ch)
        currentNode.matchFor
      }
    }

  }

  object AhoCorasic {

    private final val AlphabetSize = 'z' - 'a' + 1

    private class TrieNode {
      val children: Array[TrieNode] = Array.ofDim(AlphabetSize)
      var matchFor: Int = -1
      def hasChildFor(ch: Int): Boolean = children(ch) != null
    }

    def apply(searchStrings: Array[Char]*): AhoCorasic = {

      val trieRoot = buildTrie(searchStrings)

      linkSuffixes(trieRoot)

      new AhoCorasic(trieRoot)
    }

    private def buildTrie(searchStrings: Seq[Array[Char]]): TrieNode = {

      val trieRoot = new TrieNode

      for (stringId <- searchStrings.indices) {
        val str = searchStrings(stringId)
        var currentNode = trieRoot
        for (ch0 <- str) {
          val ch = ch0 - 'a'
          if (!currentNode.hasChildFor(ch)) {
            currentNode.children(ch) = new TrieNode
          }
          currentNode = currentNode.children(ch)
        }
        currentNode.matchFor = stringId
      }
      trieRoot
    }

    private def linkSuffixes(trieRoot: TrieNode): Unit = {

      val queue = mutable.Queue(trieRoot)
      val suffixLinks = mutable.AnyRefMap.empty[TrieNode, TrieNode]

      while (queue.nonEmpty) {
        val v = queue.dequeue()
        val u = suffixLinks.getOrElse(v, v)
        if (v.matchFor == -1) v.matchFor = u.matchFor
        for (ch <- 0 until AlphabetSize) {
          if (v.hasChildFor(ch)) {
            suffixLinks(v.children(ch)) = if (suffixLinks.contains(v) && u.hasChildFor(ch)) u.children(ch) else trieRoot
            queue += v.children(ch)
          } else {
            v.children(ch) = if (u.hasChildFor(ch)) u.children(ch) else trieRoot
          }
        }
      }
    }

  }

}

import java.util

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

// https://codeforces.com/contest/1055/problem/D
object DAhoCorasic extends App {

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

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine.toCharArray.map(c => (c - 'a').toByte) }

  val isDiff = Array.ofDim[Boolean](n)
  var i = 0
  var first = -1
  while (i < n) {
    if (!util.Arrays.equals(as(i), bs(i))) {
      first = i
      isDiff(i) = true
    }
    i += 1
  }

  val aFirst = as(first)
  val bFirst = bs(first)
  var lMax = 0
  while (aFirst(lMax) == bFirst(lMax)) lMax += 1
  var rMin = aFirst.length - 1
  while (aFirst(rMin) == bFirst(rMin)) rMin -= 1
  var lFirst = 0
  var rFirst = aFirst.length - 1
  var s = as(first)
  var t = bs(first)
  var ahoCorasic = AhoCorasic(s)
  val sMin = s.slice(lMax, rMin + 1)
  val tMin = t.slice(lMax, rMin + 1)

  def patch(x: Array[Byte], y: Array[Byte], pos: Int): Array[Byte] = {
    val ss = x.clone()
    var i = 0
    while (i < y.length) {
      ss(pos + i) = y(i)
      i += 1
    }
    ss
  }

  for (i <- 0 until n) {
    if (isDiff(i)) {
      val a = as(i)
      val b = bs(i)
      val pos = ahoCorasic.findIn(a) - s.length + 1
      val mod = if (pos < 0) a
      else patch(a, t, pos)
      if (!util.Arrays.equals(mod, b)) {
        var l = 0
        while (a(l) == b(l)) l += 1
        var r = a.length - 1
        while (a(r) == b(r)) r -= 1
        if (r - l == rMin - lMax /* && util.Arrays.equals(a.slice(l, r + 1), sMin) && util.Arrays.equals(b.slice(l, r + 1), tMin)*/) {
          var ll = lMax
          while (l > 0 && ll > lFirst && a(l - 1) == aFirst(ll - 1) && b(l - 1) == bFirst(ll - 1)) {
            l -= 1
            ll -= 1
          }
          var rr = rMin
          while (r < a.length - 1 && rr < rFirst && a(r + 1) == aFirst(rr + 1) && b(r + 1) == bFirst(rr + 1)) {
            r += 1
            rr += 1
          }
          lFirst = ll
          rFirst = rr
          if (rr - ll + 1 != s.length) {
            s = aFirst.slice(ll, rr + 1)
            t = bFirst.slice(ll, rr + 1)
            ahoCorasic = AhoCorasic(s)
          }
        }
      }
    }
  }

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = ahoCorasic.findIn(as(i)) - s.length + 1
      val mod = if (pos < 0) as(i)
      else patch(as(i), t, pos)
      if (!util.Arrays.equals(mod, bs(i))) ok = false
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))
  if (ok) {
    println("YES")
    println(s.map(c => (c + 'a').toChar).mkString)
    println(t.map(c => (c + 'a').toChar).mkString)
  } else println("NO")
  Console.flush()

  import AhoCorasic._

  class AhoCorasic private(
      private[AhoCorasic] val jumpTable: Array[Int],
      private[AhoCorasic] val matchFor: Array[Int]) {

    def findIn(t: Array[Byte]): Int = {
      var currentPosition = 0
      var i = 0
      do {
        currentPosition = jumpTable(currentPosition * AlphabetSize + t(i))
        i += 1
      } while (matchFor(currentPosition) < 0 && i < t.length)
      if (matchFor(currentPosition) == 0) i - 1 else -1
    }

    def newMatcher: Matcher = new Matcher

    class Matcher private[AhoCorasic]() {

      private var currentPosition = 0

      def apply(ch: Byte): Int = {
        currentPosition = jumpTable(currentPosition * AlphabetSize + ch)
        matchFor(currentPosition)
      }
    }

  }

  object AhoCorasic {

    private final val AlphabetSize = 26

    def apply(searchStrings: Array[Byte]*): AhoCorasic = {

      val (jumpTable, matchFor) = buildJumpTable(searchStrings)

      linkSuffixes(jumpTable, matchFor)

      new AhoCorasic(jumpTable, matchFor)
    }

    private def buildJumpTable(searchStrings: Seq[Array[Byte]]): (Array[Int], Array[Int]) = {

      val emptyJumpTableSegment = Array.fill(AlphabetSize)(-1)
      val jumpTableBuilder = ArrayBuffer(emptyJumpTableSegment: _*)
      val matchForBuilder = ArrayBuffer(-1)

      for (stringId <- searchStrings.indices) {
        val str = searchStrings(stringId)
        var currentPosition = 0
        for (ch <- str) {
          val next = currentPosition * AlphabetSize + ch
          if (jumpTableBuilder(next) == -1) {
            jumpTableBuilder(next) = matchForBuilder.size
            jumpTableBuilder ++= emptyJumpTableSegment
            matchForBuilder += -1
          }
          currentPosition = jumpTableBuilder(next)
        }
        matchForBuilder(currentPosition) = stringId
      }

      (jumpTableBuilder.toArray, matchForBuilder.toArray)
    }

    private def linkSuffixes(jumpTable: Array[Int], matchFor: Array[Int]): Unit = {

      val queue = mutable.Queue(0)
      val suffixLinks = Array.fill(matchFor.length)(-1)

      while (queue.nonEmpty) {
        val v = queue.dequeue()
        val u = if (v == 0) 0 else suffixLinks(v)
        if (matchFor(v) == -1) matchFor(v) = matchFor(u)
        for (ch <- 0 until AlphabetSize) {
          val vIndex = v * AlphabetSize + ch
          val uIndex = u * AlphabetSize + ch
          if (jumpTable(vIndex) != -1) {
            suffixLinks(jumpTable(vIndex)) = if (v > 0 && jumpTable(uIndex) != -1) jumpTable(uIndex) else 0
            queue += jumpTable(vIndex)
          } else {
            jumpTable(vIndex) = if (jumpTable(uIndex) != -1) jumpTable(uIndex) else 0
          }
        }
      }
    }

  }

}

import java.util

object D extends App {

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

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine.toCharArray }

  val isDiff = Array.ofDim[Boolean](n)
  var i = 0
  var first = -1
  while (i < n) {
    if (!util.Arrays.equals(as(i), bs(i))) {
      first = i
      isDiff(i) = true
    }
    i += 1
  }

  val aFirst = as(first)
  val bFirst = bs(first)
  var lMax = 0
  while (aFirst(lMax) == bFirst(lMax)) lMax += 1
  var rMin = aFirst.length - 1
  while (aFirst(rMin) == bFirst(rMin)) rMin -= 1
  var lFirst = 0
  var rFirst = aFirst.length - 1
  var s = as(first)
  var t = bs(first)
  val sMin = s.slice(lMax, rMin + 1)
  val tMin = t.slice(lMax, rMin + 1)

  def patch(x: Array[Char], y: Array[Char], pos: Int): Array[Char] = {
    val ss = x.clone()
    var i = 0
    while (i < y.length) {
      ss(pos + i) = y(i)
      i += 1
    }
    ss
  }

  def kmp(t: Array[Char], p: Array[Char]): Int = {
    val m = p.length
    val next = Array.fill(m + 1)(0)
    var k = 0
    var j = 1
    while (j < m) {
      while (k > 0 && p(k) != p(j)) k = next(k)
      if (p(k) == p(j)) k += 1
      next(j + 1) = k
      j += 1
    }

    var pos = -1
    j = 0
    var i = 0
    while (i < t.length && pos < 0) {
      while (j > 0 && p(j) != t(i)) j = next(j)
      if (p(j) == t(i)) j += 1
      if (j == m) {
        pos = i - m + 1
        j = next(j)
      }
      i += 1
    }

    pos
  }

  for (i <- 0 until n) {
    if (isDiff(i)) {
      val a = as(i)
      val b = bs(i)
      val pos = kmp(a, s)
      val mod = if (pos < 0) a
      else patch(a, t, pos)
      if (!util.Arrays.equals(mod, b)) {
        var l = 0
        while (a(l) == b(l)) l += 1
        var r = a.length - 1
        while (a(r) == b(r)) r -= 1
        if (r - l == rMin - lMax /* && util.Arrays.equals(a.slice(l, r + 1), sMin) && util.Arrays.equals(b.slice(l, r + 1), tMin)*/) {
          var ll = lMax
          while (l > 0 && ll > lFirst && a(l - 1) == aFirst(ll - 1) && b(l - 1) == bFirst(ll - 1)) {
            l -= 1
            ll -= 1
          }
          var rr = rMin
          while (r < a.length - 1 && rr < rFirst && a(r + 1) == aFirst(rr + 1) && b(r + 1) == bFirst(rr + 1)) {
            r += 1
            rr += 1
          }
          lFirst = ll
          rFirst = rr
          if (rr - ll + 1 != s.length) {
            s = aFirst.slice(ll, rr + 1)
            t = bFirst.slice(ll, rr + 1)
          }
        }
      }
    }
  }

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = kmp(as(i), s)
      val mod = if (pos < 0) as(i)
      else patch(as(i), t, pos)
      if (!util.Arrays.equals(mod, bs(i))) ok = false
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))
  if (ok) {
    println("YES")
    println(s.mkString)
    println(t.mkString)
  } else println("NO")
  Console.flush()
}

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 A, B = Array.ofDim[String](N)
    rep(N) { i =>
      A(i) = ns()
    }
    rep(N) { i =>
      B(i) = ns()
    }

    /**
      * [l, r)
      */
    case class Replace(l: Int, r: Int, i: Int)

    def calc(i: Int): Option[Replace] = {
      var l, r = -1
      rep(A(i).length) { j =>
        if (A(i).charAt(j) != B(i).charAt(j)) {
          if (l == -1) l = j
          r = j
        }
      }
      if (r == -1) None
      else Some(Replace(l, r + 1, i))
    }

    def test(s: String, t: String, kmp: KMP): Boolean = {
      0 until N forall { i =>
        val ix = kmp.findFirst(A(i))
        if (ix == -1)
          A(i) == B(i)
        else {
          var ok = true
          rep(A(i).length) { j =>
            if (j < ix || j >= ix + t.length) {
              ok &&= B(i).charAt(j) == A(i).charAt(j)
            } else {
              ok &&= B(i).charAt(j) == t(j - ix)
            }
          }
          ok
        }
      }
    }

    val diffs = map(N)(calc).flatten
    var lastReplace: Option[Replace] = None
    var cntL, cntR = -1 // 左右の同じものが続く回数
    diffs.foreach { replace =>
      val l = replace.l
      val r = replace.r
      val i = replace.i
        // cntL, cntRはできるだけ広くとるほうがSameとバッティングしにくいので真に有利になる
        lastReplace match {
          case None =>
            cntL = l
            cntR = A(i).length - r

          case Some(last: Replace) =>
            var j = 1
            val A0 = A(i)
            val A1 = A(last.i)
            while(l - j >= 0 && j <= cntL && A0.charAt(l - j) == A1.charAt(last.l - j)) {
              j += 1
            }
            cntL = j - 1

            j = 0
            while(r + j < A0.length && j < cntR && A0.charAt(r + j) == A1.charAt(last.r + j)) {
              j += 1
            }
            cntR = j
        }

        lastReplace = Some(replace)
    }

    val Some(last: Replace) = lastReplace
    val s = A(last.i).substring(last.l - cntL, last.r + cntR)
    val t = B(last.i).substring(last.l - cntL, last.r + cntR)

    if (!test(s, t, new KMP(s))) {
      out.println("NO")
    } else {
      out.println("YES")
      out.println(s)
      out.println(t)
    }
  }

  class KMP(word: String) {
    val kmp: Array[Int] = Array.ofDim[Int](word.length + 1)
    2 to word.length foreach { i =>
      var j = kmp(i - 1)
      var continues = true
      while(continues) {
        if (word(j) == word(i - 1)) {
          j += 1
          continues = false
        } else if (j == 0) continues = false
        else j = kmp(j)
      }
      kmp(i) = j
    }

    def findFirst(text: String): Int = {
      var j = 0
      rep(text.length) { i =>
        var continues = true
        while(continues) {
          if (word(j) == text(i)) {
            j += 1
            continues = false
          } else if (j == 0) continues = false
          else j = kmp(j)
        }
        if (j == word.length) return i - word.length + 1
      }
      -1
    }
  }

  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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    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 java.util

import scala.collection.mutable

// https://codeforces.com/contest/1055/problem/D
object DAhoCorasic extends App {

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

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine.toCharArray.map(c => (c - 'a').toByte) }

  val isDiff = Array.ofDim[Boolean](n)
  var i = 0
  var first = -1
  while (i < n) {
    if (!util.Arrays.equals(as(i), bs(i))) {
      first = i
      isDiff(i) = true
    }
    i += 1
  }

  val aFirst = as(first)
  val bFirst = bs(first)
  var lMax = 0
  while (aFirst(lMax) == bFirst(lMax)) lMax += 1
  var rMin = aFirst.length - 1
  while (aFirst(rMin) == bFirst(rMin)) rMin -= 1
  var lFirst = 0
  var rFirst = aFirst.length - 1
  var s = as(first)
  var t = bs(first)
  var ahoCorasic = AhoCorasic(s)
  val sMin = s.slice(lMax, rMin + 1)
  val tMin = t.slice(lMax, rMin + 1)

  def patch(x: Array[Byte], y: Array[Byte], pos: Int): Array[Byte] = {
    val ss = x.clone()
    var i = 0
    while (i < y.length) {
      ss(pos + i) = y(i)
      i += 1
    }
    ss
  }

  for (i <- 0 until n) {
    if (isDiff(i)) {
      val a = as(i)
      val b = bs(i)
      val pos = ahoCorasic.findIn(a) - s.length + 1
      val mod = if (pos < 0) a
      else patch(a, t, pos)
      if (!util.Arrays.equals(mod, b)) {
        var l = 0
        while (a(l) == b(l)) l += 1
        var r = a.length - 1
        while (a(r) == b(r)) r -= 1
        if (r - l == rMin - lMax /* && util.Arrays.equals(a.slice(l, r + 1), sMin) && util.Arrays.equals(b.slice(l, r + 1), tMin)*/) {
          var ll = lMax
          while (l > 0 && ll > lFirst && a(l - 1) == aFirst(ll - 1) && b(l - 1) == bFirst(ll - 1)) {
            l -= 1
            ll -= 1
          }
          var rr = rMin
          while (r < a.length - 1 && rr < rFirst && a(r + 1) == aFirst(rr + 1) && b(r + 1) == bFirst(rr + 1)) {
            r += 1
            rr += 1
          }
          lFirst = ll
          rFirst = rr
          if (rr - ll + 1 != s.length) {
            s = aFirst.slice(ll, rr + 1)
            t = bFirst.slice(ll, rr + 1)
            ahoCorasic = AhoCorasic(s)
          }
        }
      }
    }
  }

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = ahoCorasic.findIn(as(i)) - s.length + 1
      val mod = if (pos < 0) as(i)
      else patch(as(i), t, pos)
      if (!util.Arrays.equals(mod, bs(i))) ok = false
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))
  if (ok) {
    println("YES")
    println(s.map(c => (c + 'a').toChar).mkString)
    println(t.map(c => (c + 'a').toChar).mkString)
  } else println("NO")
  Console.flush()

  import AhoCorasic._

  class AhoCorasic private(private val trieRoot: TrieNode) {

    def newMatcher: Matcher = new Matcher

    def findIn(t: Array[Byte]): Int = {
      var currentNode = trieRoot
      var i = 0
      do {
        currentNode = currentNode.children(t(i))
        i += 1
      } while (currentNode.matchFor < 0 && i < t.length)
      if (currentNode.matchFor == 0) i - 1 else -1
    }

    class Matcher private[AhoCorasic] () {

      private var currentNode = trieRoot

      def apply(rawCh: Byte): Int = {
        val ch = java.lang.Byte.toUnsignedInt(rawCh)
        currentNode = currentNode.children(ch)
        currentNode.matchFor
      }
    }

  }

  object AhoCorasic {

    private final val AlphabetSize = 'z' - 'a' + 1

    private class TrieNode {
      val children: Array[TrieNode] = Array.ofDim(AlphabetSize)
      var matchFor: Int = -1
      def hasChildFor(ch: Int): Boolean = children(ch) != null
    }

    def apply(searchStrings: Array[Byte]*): AhoCorasic = {

      val trieRoot = buildTrie(searchStrings)

      linkSuffixes(trieRoot)

      new AhoCorasic(trieRoot)
    }

    private def buildTrie(searchStrings: Seq[Array[Byte]]): TrieNode = {

      val trieRoot = new TrieNode

      for (stringId <- searchStrings.indices) {
        val str = searchStrings(stringId)
        var currentNode = trieRoot
        for (ch0 <- str) {
          val ch = ch0
          if (!currentNode.hasChildFor(ch)) {
            currentNode.children(ch) = new TrieNode
          }
          currentNode = currentNode.children(ch)
        }
        currentNode.matchFor = stringId
      }
      trieRoot
    }

    private def linkSuffixes(trieRoot: TrieNode): Unit = {

      val queue = mutable.Queue(trieRoot)
      val suffixLinks = mutable.AnyRefMap.empty[TrieNode, TrieNode]

      while (queue.nonEmpty) {
        val v = queue.dequeue()
        val u = suffixLinks.getOrElse(v, v)
        if (v.matchFor == -1) v.matchFor = u.matchFor
        for (ch <- 0 until AlphabetSize) {
          if (v.hasChildFor(ch)) {
            suffixLinks(v.children(ch)) = if (suffixLinks.contains(v) && u.hasChildFor(ch)) u.children(ch) else trieRoot
            queue += v.children(ch)
          } else {
            v.children(ch) = if (u.hasChildFor(ch)) u.children(ch) else trieRoot
          }
        }
      }
    }

  }

}

import java.util

// https://codeforces.com/contest/1055/problem/D
object DKmp extends App {

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

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine.toCharArray }

  val isDiff = Array.ofDim[Boolean](n)
  var i = 0
  var first = -1
  while (i < n) {
    if (!util.Arrays.equals(as(i), bs(i))) {
      first = i
      isDiff(i) = true
    }
    i += 1
  }

  val aFirst = as(first)
  val bFirst = bs(first)
  var lMax = 0
  while (aFirst(lMax) == bFirst(lMax)) lMax += 1
  var rMin = aFirst.length - 1
  while (aFirst(rMin) == bFirst(rMin)) rMin -= 1
  var lFirst = 0
  var rFirst = aFirst.length - 1
  var s = as(first)
  var t = bs(first)
  var kmpNext = kmpPattern(s)
  val sMin = s.slice(lMax, rMin + 1)
  val tMin = t.slice(lMax, rMin + 1)

  def patch(x: Array[Char], y: Array[Char], pos: Int): Array[Char] = {
    val ss = x.clone()
    var i = 0
    while (i < y.length) {
      ss(pos + i) = y(i)
      i += 1
    }
    ss
  }

  def kmpPattern(p: Array[Char]): Array[Int] = {
    val m = p.length
    val next = Array.fill(m + 1)(0)
    var k = 0
    var j = 1
    while (j < m) {
      while (k > 0 && p(k) != p(j)) k = next(k)
      if (p(k) == p(j)) k += 1
      next(j + 1) = k
      j += 1
    }
    next
  }

  def kmp(t: Array[Char], p: Array[Char], next: Array[Int]): Int = {
    val m = p.length

    var pos = -1
    var j = 0
    var i = 0
    while (i < t.length && pos < 0) {
      while (j > 0 && p(j) != t(i)) j = next(j)
      if (p(j) == t(i)) j += 1
      if (j == m) {
        pos = i - m + 1
        j = next(j)
      }
      i += 1
    }

    pos
  }

  for (i <- 0 until n) {
    if (isDiff(i)) {
      val a = as(i)
      val b = bs(i)
      val pos = kmp(a, s, kmpNext)
      val mod = if (pos < 0) a
      else patch(a, t, pos)
      if (!util.Arrays.equals(mod, b)) {
        var l = 0
        while (a(l) == b(l)) l += 1
        var r = a.length - 1
        while (a(r) == b(r)) r -= 1
        if (r - l == rMin - lMax /* && util.Arrays.equals(a.slice(l, r + 1), sMin) && util.Arrays.equals(b.slice(l, r + 1), tMin)*/) {
          var ll = lMax
          while (l > 0 && ll > lFirst && a(l - 1) == aFirst(ll - 1) && b(l - 1) == bFirst(ll - 1)) {
            l -= 1
            ll -= 1
          }
          var rr = rMin
          while (r < a.length - 1 && rr < rFirst && a(r + 1) == aFirst(rr + 1) && b(r + 1) == bFirst(rr + 1)) {
            r += 1
            rr += 1
          }
          lFirst = ll
          rFirst = rr
          if (rr - ll + 1 != s.length) {
            s = aFirst.slice(ll, rr + 1)
            t = bFirst.slice(ll, rr + 1)
            kmpNext = kmpPattern(s)
          }
        }
      }
    }
  }

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = kmp(as(i), s, kmpNext)
      val mod = if (pos < 0) as(i)
      else patch(as(i), t, pos)
      if (!util.Arrays.equals(mod, bs(i))) ok = false
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))
  if (ok) {
    println("YES")
    println(s.mkString)
    println(t.mkString)
  } else println("NO")
  Console.flush()
}

object Main {
  import java.io.PrintStream

  def main(args: Array[String]): Unit = {
    val s = new Main()
    val ps = new PrintStream(Console.out)
    Console.withOut(ps) {
      s.solve()
    }
    ps.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
  import scala.Console

  val MOD = 1000000007

  def solve(): Unit = {
    val N = ni()
    val A, B = Array.ofDim[String](N)
    rep(N) { i =>
      A(i) = ns()
    }
    rep(N) { i =>
      B(i) = ns()
    }

    sealed trait Diff
    case class Never() extends Diff
    case class Same() extends Diff
    case class One(l: Int, r: Int) extends Diff

    def calc(i: Int): Diff = {
      var state = 0
      var ok = true
      var l, r = -1
      rep(N) { j =>
        state match {
          case 0 =>
            if (A(i)(j) != B(i)(j)) {
              l = j
              state = 1
            }
          case 1 =>
            if (A(i)(j) == B(i)(j)) {
              r = j
              state = 2
            }

          case 2 =>
            if (A(i)(j) != B(i)(j)) ok = false
        }
      }

      if (!ok) Never()
      else if (state == 0) Same()
      else if (state == 1) One(l, A(i).length)
      else One(l, r)
    }

    val diffs = map(N)(calc)
    var s: String = ""
    var t: String = ""
    var ok = true
    rep(N) { i =>
      diffs(i) match {
        case Never() => ok = false
        case Same() =>
        case One(l, r) =>
          val s1 = A(i).substring(l, r)
          val t1 = B(i).substring(l, r)
          if (s.isEmpty) s = s1
          if (t.isEmpty) t = t1

          ok &&= s == s1 && t == t1
      }
    }

    if (!ok) {
      println("NO")
    } else {
      println("YES")
      println(s)
      println(t)
    }

  }

  class InputReader(reader: BufferedReader) {
    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(Console.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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    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
  }
}

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 A, B = Array.ofDim[String](N)
    rep(N) { i =>
      A(i) = ns()
    }
    rep(N) { i =>
      B(i) = ns()
    }

    sealed trait Diff
    case class Never() extends Diff
    case class Same() extends Diff
    case class One(l: Int, r: Int) extends Diff

    def calc(i: Int): Diff = {
      var state = 0
      var ok = true
      var l, r = -1
      rep(N) { j =>
        state match {
          case 0 =>
            if (A(i)(j) != B(i)(j)) {
              l = j
              state = 1
            }
          case 1 =>
            if (A(i)(j) == B(i)(j)) {
              r = j
              state = 2
            }

          case 2 =>
            if (A(i)(j) != B(i)(j)) ok = false
        }
      }

      if (!ok) Never()
      else if (state == 0) Same()
      else if (state == 1) One(l, A(i).length)
      else One(l, r)
    }

    val diffs = map(N)(calc)
    var s: String = ""
    var t: String = ""
    var ok = true
    rep(N) { i =>
      diffs(i) match {
        case Never() => ok = false
        case Same() =>
        case One(l, r) =>
          val s1 = A(i).substring(l, r)
          val t1 = B(i).substring(l, r)
          if (s.isEmpty) s = s1
          if (t.isEmpty) t = t1

          ok &&= s == s1 && t == t1
      }
    }

    rep(N) { i =>
      if (diffs(i) == Same()) {
        val ix = A(i).indexOf(s)
        ok &&= ix == -1 || {
          B(i).substring(ix, s.length) == t
        }
      }
    }

    if (!ok) {
      out.println("NO")
    } else {
      out.println("YES")
      out.println(s)
      out.println(t)
    }

  }

  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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    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
  }
}

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 A, B = Array.ofDim[String](N)
    rep(N) { i =>
      A(i) = ns()
    }
    rep(N) { i =>
      B(i) = ns()
    }

    sealed trait Diff
    case class Same() extends Diff
    case class Replace(l: Int, r: Int, i: Int) extends Diff

    def calc(i: Int): Diff = {
      var l, r = -1
      rep(A(i).length) { j =>
        if (A(i)(j) != B(i)(j)) {
          if (l == -1) l = j
          r = j
        }
      }
      if (r == -1) Same()
      else Replace(l, r + 1, i)
    }

    val diffs = map(N)(calc)
    var minS: String = ""
    var minT: String = ""
    var lastDiff: Option[Replace] = None
    var cntL, cntR = -1 // 左右の同じものが続く回数
    var ok = true
    rep(N) { i =>
      diffs(i) match {
        case Same() =>
        case replace @ Replace(l, r, _) =>
          val s = A(i).substring(l, r)
          val t = B(i).substring(l, r)
          if (minS.isEmpty) minS = s
          if (minT.isEmpty) minT = t

          // cntL, cntRはできるだけ広くとるほうがSameとバッティングしにくいので有利になる
          lastDiff match {
            case None =>
              lastDiff = Some(replace)
              cntL = l
              cntR = A(i).length - r

            case Some(last: Replace) =>
              var j = 1
              val A0 = A(i)
              val A1 = A(last.i)
              val B0 = B(i)
              val B1 = B(last.i)
              while(l - j >= 0 && j <= cntL && A0(l - j) == A1(last.l - j) && B0(l - j) == B1(last.l - j)) {
                j += 1
              }
              cntL = j - 1

              j = 0
              while(r + j < A0.length && j < cntR && A0(r + j) == A1(last.r + j) && B0(r + j) == B1(last.r + j)) {
                j += 1
              }
              cntR = j
          }

          lastDiff = Some(replace)
          ok &&= minS == s && minT == t
      }
    }

    val Some(last: Replace) = lastDiff
    val s = A(last.i).substring(last.l - cntL, last.r + cntR)
    val t = B(last.i).substring(last.l - cntL, last.r + cntR)

    rep(N) { i =>
      if (diffs(i) == Same()) {
        val ix = A(i).indexOf(s)
        ok &&= ix == -1 || {
          B(i).substring(ix, ix + s.length) == t
        }
      }
    }

    if (!ok) {
      out.println("NO")
    } else {
      out.println("YES")
      out.println(s)
      out.println(t)
    }
  }

  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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    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
  }
}

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 A, B = Array.ofDim[String](N)
    rep(N) { i =>
      A(i) = ns()
    }
    rep(N) { i =>
      B(i) = ns()
    }

    sealed trait Diff
    case class Same() extends Diff
    case class Replace(l: Int, r: Int, i: Int) extends Diff

    def calc(i: Int): Diff = {
      var l, r = -1
      rep(A(i).length) { j =>
        if (A(i)(j) != B(i)(j)) {
          if (l == -1) l = j
          r = j
        }
      }
      if (r == -1) Same()
      else Replace(l, r + 1, i)
    }

    val diffs = map(N)(calc)
    var minS: String = ""
    var minT: String = ""
    var lastDiff: Option[Replace] = None
    var cntL, cntR = -1 // 左右の同じものが続く回数
    var ok = true
    rep(N) { i =>
      diffs(i) match {
        case Same() =>
        case replace @ Replace(l, r, _) =>
          val s = A(i).substring(l, r)
          val t = B(i).substring(l, r)
          if (minS.isEmpty) minS = s
          if (minT.isEmpty) minT = t

          // cntL, cntRはできるだけ広くとるほうがSameとバッティングしにくいので有利になる
          lastDiff match {
            case None =>
              lastDiff = Some(replace)
              cntL = l
              cntR = A(i).length - r

            case Some(last: Replace) =>
              var j = 1
              val A0 = A(i)
              val A1 = A(last.i)
              val B0 = B(i)
              val B1 = B(last.i)
              while(l - j >= 0 && j < cntL && A0(l - j) == A1(last.l - j) && B0(l - j) == B1(last.l - j)) {
                j += 1
              }
              cntL = j - 1

              j = 0
              while(r + j < A0.length && j < cntR && A0(r + j) == A1(last.r + j) && B0(r + j) == B1(last.r + j)) {
                j += 1
              }
              cntR = j
          }

          lastDiff = Some(replace)
          ok &&= minS == s && minT == t
      }
    }

    val Some(last: Replace) = lastDiff
    val s = A(last.i).substring(last.l - cntL, last.r + cntR)
    val t = B(last.i).substring(last.l - cntL, last.r + cntR)

    rep(N) { i =>
      if (diffs(i) == Same()) {
        val ix = A(i).indexOf(s)
        ok &&= ix == -1 || {
          B(i).substring(ix, ix + s.length) == t
        }
      }
    }

    if (!ok) {
      out.println("NO")
    } else {
      out.println("YES")
      out.println(s)
      out.println(t)
    }
  }

  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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    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
  }
}

object D extends App {

  @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)) }

  val Array(n) = readInts(1)
  val as, bs = Array.fill(n){ readLine }

  var maxD, maxDPos = -1

  for (i <- 0 until n) {
    if (as(i) != bs(i)) {
      val a = as(i)
      val b = bs(i)
      var l = 0
      while (a(l) == b(l)) l += 1
      var r = a.length - 1
      while (a(r) == b(r)) r -= 1
      if (r - l > maxD) {
        maxD = r - l
        maxDPos = i
      }
    }
  }

  val a = as(maxDPos)
  val b = bs(maxDPos)
  var l = 0
  while (a(l) == b(l)) l += 1
  var r = a.length - 1
  while (a(r) == b(r)) r -= 1

  val s = a.substring(l, r + 1)
  val t = b.substring(l, r + 1)

  var ok = true
  for (i <- 0 until n) {
    if (ok) {
      val pos = as(i).indexOfSlice(s)
      val mod = if (pos < 0) as(i)
      else as(i).patch(pos, t, t.length)
      if (mod != bs(i)) ok = false
    }
  }

  if (ok) {
    println("YES")
    println(s)
    println(t)
  } else println("NO")
}


You are given a huge decimal number consisting of $$$n$$$ digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.You are also given two integers $$$0 \le y < x < n$$$. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.

The first line of the input contains three integers $$$n, x, y$$$ ($$$0 \le y < x < n \le 2 \cdot 10^5$$$) — the length of the number and the integers $$$x$$$ and $$$y$$$, respectively. The second line of the input contains one decimal number consisting of $$$n$$$ digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.

Print one integer — the minimum number of operations you should perform to obtain the number having remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.

NoteIn the first example the number will be $$$11010100100$$$ after performing one operation. It has remainder $$$100$$$ modulo $$$100000$$$.In the second example the number will be $$$11010100010$$$ after performing three operations. It has remainder $$$10$$$ modulo $$$100000$$$.

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

  override def apply(io: IO): io.type = {
    val _, x, y = io.read[Int]
    val num = io.read[String].reverse.padTo(x, '0')

    val target = Array.fill(x)('0')
    target(y) = '1'

    val ans = num.zip(target).count({case (i, j) => i != j})

    io.write(ans)
  }
}
/****************************[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 extends CodeForcesApp {

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

  override def apply(io: IO): io.type = {
    val _, x, y = io.read[Int]
    val num = io.read[String].reverse

    val target = Array.fill(x)('0')
    target(y) = '1'

    val ans = num.zip(target).count({ case (i, j) => i != j })

    io.write(ans)
  }
}

/** **************************[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
      }
    }
  }

}

import java.util.Scanner

import scala.io.StdIn

object Atask560 extends App {
  var line = new Scanner(StdIn.readLine())
  val n = line.nextInt()
  val x = line.nextInt()
  val y = line.nextInt()
  line = new Scanner(StdIn.readLine())
  val digit = line.next.chars().toArray

  var result = 0

  for (i <- 0 until x) {
    if (digit(n - 1 - i) == '1' && y != i) {
      digit(n - 1 -i) = '0'
      result += 1
    }
    if (digit(n - 1 - i) == '0' && y == i) {
      digit(n - 1 -i) = '1'
      result += 1
    }
  }

  println(result)
}


One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.Now the children wonder, how to divide the orange so as to meet these conditions:  each child gets exactly n orange segments;  the i-th child gets the segment with number ai for sure;  no segment goes to two children simultaneously. Help the children, divide the orange and fulfill the requirements, described above.

The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get. It is guaranteed that all numbers ai are distinct.

Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces. You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

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

object P244A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)
  val N, K = sc.nextInt
  val A = List.fill(K)(sc.nextInt)

  def solve(): List[List[Int]] = {
    val segments = List.range(1, N * K + 1)
    val rest = segments.toSet -- A.toSet

    @tailrec
    def loop(acc: List[List[Int]], requests: List[Int], rest: List[Int]): List[List[Int]] = {
      if (requests == Nil) acc
      else loop(acc ++ List(requests.head :: rest.take(N - 1)), requests.tail, rest.drop(N - 1))
    }

    loop(Nil, A, rest.toList)
  }

  solve.foreach { lst =>
    out.println(lst.mkString(" "))
  }
  out.close
}

object Apelsin extends App {
    var ss = (Console.readLine.split(" ")) map (_.toInt)
    val (n,k) = (ss(0), ss(1))
    val aa0 = (Console.readLine.split(" ")) map (_.toInt)
    val aa = aa0 map (x => x-1)
    val nk = n*k
    var dl = Array.fill(nk)(0)

    def iniFill() = {
      for {
        i <- Range(0, k)
        j <- Range(0, n)
        val idx = n*i+j
      } dl(idx) = i
    }

    def printOneDol(i:Int) = {
      for {
        j <- Range(0, nk)
        if dl(j) == i
      } print((j+1) + " ")
      println
    }

    def printDl = {
      for {
        i <- Range(0, k)
      } printOneDol(i)
    }

    def findI(i:Int):Int = {
      val seq = for {
        ii <- Range(0, nk)
        if dl(ii) == i
      } yield ii
      seq(0)
    }

    def fixOne(i:Int, ai:Int) = {
      val idx = findI(i)
      if (ai != idx) {
        dl(idx) = dl(ai)
        dl(ai) = i
      }
    }

    def fixAll = {
      for {
        i<- Range(0, k)
      } fixOne(i, aa(i))
    }

    iniFill()
    fixAll
    printDl
}

import java.io._

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

  val Array(n, k) = readInts
  val a = readInts
  def ans = if(n == 1) {
    a.mkString(" ")
  } else {
    (1 to n * k).filterNot(a.contains).grouped(n - 1).toArray.zip(a).map(x => (x._1 ++ Array(x._2)).mkString(" ")).mkString("\n")
  }

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

object Main {
  def main(args: Array[String]) = {
    val nk = readLine().split(" ").map(_.toInt)
    val n = nk(0)
    val k = nk(1)
    val a = readLine().split(" ").map(_.toInt)
    val s = scala.collection.mutable.Set() ++ (1 to n * k)
    s --= a

    if (n == 1) {
      for(i <- 0 until k) println(a(i))
    } else {
      s.grouped(n - 1).zipWithIndex.foreach { t =>
        println((t._1 ++ List(a(t._2))).mkString(" "))
      }
    }
  }
}

/**
 * Created by slie742 on 9/09/2015.
 */

import scala.io.StdIn.readLine
import scala.io.StdIn.readInt

import Math.abs
import Math.max
import Math.min

object DividingOrange {

  def readInts() : Array[Int] =
    readLine.split(' ').map(_.toInt)

  def readLongs() : Array[Long] =
    readLine.split(' ').map(_.toLong)

  def readTuple() : (Int,Int) = {
    val line = readInts
    (line(0),line(1))
  }
  def readTupleLong() : (Long,Long) = {
    val line = readLongs
    (line(0),line(1))
  }

  def readTriple() : (Int,Int,Int) = {
    val line = readInts
    (line(0),line(1),line(2))
  }
  def readQuad() : (Int,Int,Int,Int) = {
    val line = readInts
    (line(0),line(1),line(2),line(3))
  }

  def divideOranges(n: Int, k: Int, as: IndexedSeq[Int]) : List[List[Int]] = {
    def go(rem: List[Int], child: Int, acc: List[List[Int]]) : List[List[Int]] = {
        if (child == k) acc.reverse
        else {
          go(rem.drop(n-1), child + 1,(as(child) :: rem.take(n-1))  :: acc)
        }
    }
    go( ((1 to n*k).toSet[Int] -- as).toList , 0, List())
  }

  def main(args: Array[String]) {
    val (n,k) = readTuple()
    val as = readInts()
    println(divideOranges(n,k,as).map(_.mkString(" ")).mkString("\n"))

  }

}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(" ").map(_.toInt)
  val data = in.next().split(" ").map(_.toInt)
  val set = data.toSet
  var j = 0

  val res = (0 until k).map{ i =>
    (2 to n).foldLeft(List(data(i))) {
      case(list, _) =>
        j += 1
        while (set.contains(j))
          j += 1
        j :: list
    }
  }
  println(res.map(_.mkString(" ")).mkString("\n"))
}

import java.io._

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

  val Array(n, k) = readInts
  val a = readInts
  def ans = if(n == 1) {
    (1 to k).mkString(" ")
  } else {
    (1 to n * k).filterNot(a.contains).grouped(n - 1).toArray.zip(a).map(x => (x._1 ++ Array(x._2)).mkString(" ")).mkString("\n")
  }

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


Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot.He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a.Will the robot be able to build the fence Emuskald wants? In other words, is there a regular polygon which angles are equal to a?

The first line of input contains an integer t (0 < t < 180) — the number of tests. Each of the following t lines contains a single integer a (0 < a < 180) — the angle the robot can make corners at measured in degrees.

For each test, output on a single line "YES" (without quotes), if the robot can build a fence Emuskald wants, and "NO" (without quotes), if it is impossible.

NoteIn the first test case, it is impossible to build the fence, since there is no regular polygon with angle .In the second test case, the fence is a regular triangle, and in the last test case — a square.

object cf165a extends App {
  val t = readLine.toInt
  (1 to t).map { qq =>
     val n = readLine.toInt
     println(if ((3 to 360).filter(i => 180 * (i - 2) == n * i).isEmpty) "NO" else "YES")
  }
}

object A {
	def main(args: Array[String]) {
		val n = readInt()

		for (i <- 1 to n) {
			val alpha = readInt()
			println(if (360 % (180 - alpha) == 0) "YES" else "NO")
		}
	}
}

object CF270A {
    def main(argv: Array[String]) {
        def judge(n: Int) {
            if (n > 0) {
                if (360 % (180 - readLine.toInt) == 0) println("YES")
                else println("NO")
                judge(n - 1)
            }
        }
        judge(readLine.toInt)
    }
}

import java.util.StringTokenizer

object _270A extends App {
  var token = new StringTokenizer("")
  def next = {
    while (!token.hasMoreTokens()) token = new StringTokenizer(readLine)
    token.nextToken()
  }
  for (t <- 1 to next.toInt) {
    val a = next.toInt
    if (360 % (180 - a) == 0) println("YES")
    else println("NO")
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val t = in.next().toInt
  Range(0, t).foreach { _ =>
    val a = in.next().toInt
    if (360 % (180 - a) == 0) println("YES")
    else println("NO")

  }
}

object Main {
  def main(args : Array[String]) = {
    val t = readLine.toInt
    for(i <- 1 to t) {
      val a = readLine.toInt
      val ans = (3 to 1000).view.map({n => a * n == 180 * (n - 2)}).dropWhile(!_).isEmpty
      if(ans) println("NO") else println("YES")
    }
  }
}

object A270 extends App {
  val k = readLine.toInt

  for (a <- 1 to k) {
    val a = readLine.toInt
    var n: Int = 3
    var isSolve = false

    if (a < 60) {
      println("NO")
      isSolve = true
    }

    while (n < 1000 && !isSolve) {
      if (a * n == (n - 2) * 180) {
        println("YES")
        isSolve = true
      } else {
        n += 1
      }
    }
    if (!isSolve)
      println("NO")
  }

}

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

object P270A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)
  val N = sc.nextInt
  def solve(): String = if (360 % (180 - sc.nextInt) == 0) "YES"
                        else "NO"

  for (i <- 0 until N)
    out.println(solve)
 out.close
}

object Main extends App {
	    def judge(n: Int) {
	        if (n > 0) {
		        if (360 % (180 - readLine.toInt) == 0) println("YES")
		        else println("NO")
		        judge(n - 1)
	        }
	    }
	    judge(readLine.toInt)
	}

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

object A extends App {
  val cin = new Scanner(new BufferedInputStream(System.in))
  for (i <- 0 until cin.nextInt) {
    var tak = false
    val n = cin.nextInt
    for (i <- 3 to 360)
      if (360 % i == 0 && 180 - 360 / i == n) {
        println("YES")
        tak = true
      }
    if (! tak) println("NO")
  }
}

import annotation.tailrec
import java.io._

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

  def ans = (for (_ <- 1 to readInt) yield if (360 % (180 - readInt) == 0) "YES" else "NO").mkString("\n")

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

import java.util._

object Test {

    def main(args: Array[String]) {
        val in = new Scanner(System.in)
        var n = in.nextInt
        for (i <- 0 to n - 1) {
            if (check(in.nextInt)) {
                println("YES")
            } else {
                println("NO")
            }
        }
    }

    def check(a: Int): Boolean = {
        val azChange = 180 - a
        val sides = 360 / azChange
        sides * azChange == 360
    }

}

import scala.io.Source

object test6 extends App {
    val t=readLine.toInt
    for(i<-1 to t){
        val a=readLine.toInt
        println(if(360%(180-a)==0) "YES" else "NO")
    }

}

import scala.io.Source

object test6 extends App {
    val t=readInt
    for(i<-1 to t){
        val a=readInt
        println(if(360%(180-a)==0) "YES" else "NO")
    }

}

object Main {
  val angles: Stream[Double] = {
    def angle(n: Int): Stream[Double] = {
      (180.0 * (n - 2) / n) #:: angle(n + 1)
    }
    angle(3)
  }

  def main(args: Array[String]) {
    val num = readInt()
    for(_ <- 1 to num) {
      val a = readInt()
      if (angles.takeWhile(_ <= a).filter(_ == a).size == 1) println("YES")
      else println("NO")
    }
  }
}

object FancyFence extends App {

  val t = readInt //num tests

  for (i <- Range(0, t)) {
    checkAngle(readDouble)
  }

  def checkAngle(angle: Double) {
    var sides = 2 / (1 - (angle / 180))
    if (Math.abs(sides - Math.round(sides)) < 0.000000001) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

object main extends App with fastIO {

  (1 to nextInt) foreach { (t) => println(if (360 % (180 - nextInt) == 0) "YES" else "NO") }

  flush
}

trait fastIO {
  import java.io._

  val isFile = false
  val input = "io.in"
  val output = "io.out"

  val bf = new BufferedReader(if(isFile) new FileReader(input) else new InputStreamReader(System.in))
  val in = new StreamTokenizer(bf)
  val out = new PrintWriter(if(isFile)new FileWriter(output) else new OutputStreamWriter(System.out))

  def next: String = {
    in.ordinaryChars('0', '9')
    in.wordChars('0', '9')
    in.nextToken()
    in.sval
  }

  def nextInt: Int = {
    in.nextToken()
    in.nval.toInt
  }

  def nextLong:Long ={
    in.nextToken()
    in.nval.toLong
  }

  def nextDouble: Double = {
    in.nextToken()
    in.nval
  }

  def readLine: String = bf.readLine()
  def println(o:Any) = out.println(o)
  def print(o:Any) = out.print(o)
  def printf(s:String,o:Any*) = out.printf(s,o)
  def flush() = out.flush()
}

object main extends App with fastIO {

  def check(n: Int) = { if (360 % n != 0) 0 else 180 - 360 / n }

  (1 to nextInt) foreach { (t) =>
    var f = false
    var angle = nextInt
    for (n <- 1 to 360)
      f = f || check(n) == angle
    println(if (f) "YES" else "NO")
  }

  flush
}

trait fastIO {
  import java.io._

  val isFile = false
  val input = "io.in"
  val output = "io.out"

  val bf = new BufferedReader(if(isFile) new FileReader(input) else new InputStreamReader(System.in))
  val in = new StreamTokenizer(bf)
  val out = new PrintWriter(if(isFile)new FileWriter(output) else new OutputStreamWriter(System.out))

  def next: String = {
    in.ordinaryChars('0', '9')
    in.wordChars('0', '9')
    in.nextToken()
    in.sval
  }

  def nextInt: Int = {
    in.nextToken()
    in.nval.toInt
  }

  def nextLong:Long ={
    in.nextToken()
    in.nval.toLong
  }

  def nextDouble: Double = {
    in.nextToken()
    in.nval
  }

  def readLine: String = bf.readLine()
  def println(o:Any) = out.println(o)
  def print(o:Any) = out.print(o)
  def printf(s:String,o:Any*) = out.printf(s,o)
  def flush() = out.flush()
}

object A{
    def main(args:Array[String]){
        val n=readLine.toInt

        for(_<-1 to n){
            val a=readLine.toInt
            if(360%(180-a)==0){
                println("YES")
            }else{
                println("NO")
            }
        }
    }
}

object cf165a extends App {
  val t = readLine.toInt
  (1 to t).map { qq =>
     val n = readLine.toInt
     println(if ((3 to 180).filter(i => 180 * (i - 2) == n * i).isEmpty) "NO" else "YES")
  }
}

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

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

  val N = sc.nextInt
  val a = Array.fill(N)(sc.nextInt)

  def solve(x: Int): String =
    if (x >= 60 && 360 % x == 0) "YES"
    else "NO"

  for (i <- 0 until N)
    out.println(solve(a(i)))

  out.close
}

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

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

  val N = sc.nextInt
  val a = Array.fill(N)(sc.nextInt)

  def solve(x: Int): String =
    if (x >= 60 && (360 % x == 0 || 360 % (180 - x) == 0)) "YES"
    else "NO"

  for (i <- 0 until N)
    out.println(solve(a(i)))

  out.close
}

import scala.math.BigDecimal

object FancyFence extends App {

  val t = readInt //num tests

  for (i <- Range(0, t)) {
    val angle = BigDecimal(readLine)
    val sides = 2.0 / (1.0 - (angle / 180.0))
    if ((sides - sides.intValue) == 0) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

import scala.math.BigDecimal

object FancyFence extends App {

  val t = readInt //num tests

  for (i <- Range(0, t)) {
    val angle = BigDecimal(readLine)
    val sides = 2.0 / (1.0 - (angle / 180.0))
    println(sides)
    if ((sides - sides.intValue) == 0) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

import scala.collection.immutable.HashMap

object FancyFence extends App {

  val t = readInt //num tests

  var set = Set[Int]()
  var n = 3
  var angle = ((n - 2) * 180) / n
  while (angle < 179) {
    set = set + angle
    n += 1
    angle = ((n - 2) * 180) / n
  }

//  println(set)

  for (i <- Range(0, t)) {
    val angle = readInt
    val sides = 2.0 / (1.0 - (angle / 180.0))
    if (set.contains(angle)) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

import scala.collection.immutable.HashMap

object FancyFence extends App {

  val t = readInt //num tests

  var set = Set[Int]()
  var n = 3
  var angle = ((n - 2) * 180) / n
  while (angle < 179) {
    set = set + angle
    n += 1
    angle = ((n - 2) * 180) / n
  }

    println(set)

  for (i <- Range(0, t)) {
    val angle = readInt
    if (set.contains(angle)) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

import scala.collection.immutable.HashMap

object FancyFence extends App {

  val t = readInt //num tests

  for (i <- Range(0, t)) {
    val angle = readInt
    val sides = 2.0 / (1.0 - (angle / 180.0))
    if (sides.isWhole()) {
      println("YES")
    } else {
      println("NO")
    }
  }

}

object A{
    def main(args:Array[String]){
        val n=readLine.toInt

        for(_<-1 to n){
            val a=readLine.toInt
            if(180%(180-a)==0 || 180%a==0){
                println("YES")
            }else{
                println("NO")
            }
        }
    }
}

object A{
    def main(args:Array[String]){
        val n=readLine.toInt

        for(_<-1 to n){
            val a=readLine.toInt
            if(180%(180-a)==0){
                println("YES")
            }else{
                println("NO")
            }
        }
    }
}


You are given $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. You choose any subset of the given numbers (possibly, none or all numbers) and negate these numbers (i. e. change $$$x \to (-x)$$$). What is the maximum number of different values in the array you can achieve?

The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The next lines contain the description of the $$$t$$$ test cases, two lines per a test case. In the first line you are given one integer $$$n$$$ ($$$1 \leq n \leq 100$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-100 \leq a_i \leq 100$$$).

For each test case, print one integer: the maximum number of different elements in the array that you can achieve negating numbers in the array.

NoteIn the first example we can, for example, negate the first and the last numbers, achieving the array $$$[-1, 1, 2, -2]$$$ with four different values.In the second example all three numbers are already different.In the third example negation does not change anything.

import java.io._
import java.util.StringTokenizer
import scala.+:
import scala.collection.mutable
import scala.collection.mutable._
import scala.collection.immutable._
import scala.util.Sorting.quickSort
import scala.math._

object Main extends App {
    val writer =  new PrintWriter(System.out, false)
    val reader = new BufferedReader(new InputStreamReader(System.in))
    var st = new StringTokenizer("")
    def next(): String = {while (!st.hasMoreTokens) {st = new StringTokenizer(reader.readLine())};st.nextToken()}
    def readInt(): Int = next().toInt
    def readLong(): Long = next().toLong
    def readString(): String = next()

    case class fenwick(n: Int) {
        val tree = new Array[Long](n+1)
        def query(ind: Int): Long = {
            var idx = ind
            var ans = 0L
            while (idx > 0) {
                ans += tree(idx)
                idx -= (idx & -idx)
            }
            return ans
        }
        def update(ind: Int, value: Long): Unit = {
            var idx = ind
            while (idx <= n) {
                tree(idx) += value
                idx += (idx & -idx)
            }
        }
    }

    case class pair(val l: Int, val r: Int)extends Ordered[pair] {
        override def compare(that: pair): Int = {
            return (r-l).compareTo(that.r-that.l)
        }
    }

    case class node(val l: Int, val r: Int, val c: Char, var d: Boolean, var lc: Char, var mx: Char, var sz: Int){
    }

    def gcd(a: Int, b: Int): Int = b match {
        case 0 => a
        case n => gcd(b, a % b)
    }
    val rem = 1000000007
    val maxn = 200010
    val mp = mutable.Map[Char, Int]()

    val t = readInt()

    for (tt <- 1 to t) {
        val n = readInt()
        var ans = 0
        val arr = new Array[Boolean](210)
        for (i <- 1 to n) {
            val k = readInt()
            if (!arr(k+100)) {
                arr(k+100) = true
                ans += 1
            } else if (!arr(-1*k+100)) {
                arr(-1*k+100) = true
                ans += 1
            }
        }
        writer.println(ans)

        def check(kk: Long): Boolean = {
            return true
        }
        def bs(st:Long, en:Long): Long = {
            if (en - st <= 1L) {
                if (check(en))
                    return en
                else
                    return st
            }
            val mid = (st + en) / 2L
            if (check(mid))
                return bs(mid, en)
            else
                return bs(st, mid)
        }
    }
    writer.flush()
}

import scala.collection.mutable

object A {
  import InOut._
  def main(args: Array[String]): Unit = {

    for (_ <- 1 to nextInt) {
      val n = nextInt
      val as = nextInts(n)
      val have = mutable.Set.empty[Int]

      for (a <- as) {
        if (!have(a)) {
          have += a
        } else if (!have(-a)) {
          have += -a
        }
      }

      out.println(have.size)
    }

    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}