Refactoring

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.

Input

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.

Output

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.

Examples

input

1
topforces
codecoder

output

YES
topforces
codecoder

input

3
bab
cac
cdc
bdb
cdc
cdc

output

YES
a
d

input

2
you
shal
not
pass

output

NO

Solution:

#include <bits/stdc++.h>

using namespace std;

template <typename T>
vector<int> kmp_table(int n, const T &s) {
vector<int> p(n, 0);
int k = 0;
for (int i = 1; i < n; i++) {
    while (k > 0 && !(s[i] == s[k])) {
k = p[k - 1];
}
if (s[i] == s[k]) {
k++;
}
p[i] = k;
}
return p;
}

template <typename T>
vector<int> kmp_table(const T &s) {
return kmp_table((int) s.size(), s);
}

template <typename T>
vector<int> kmp_search(int n, const T &s, int m, const T &w, const vector<int> &p) {
assert(n >= 1 && (int) p.size() == n);
vector<int> res;
int k = 0;
for (int i = 0; i < m; i++) {
    while (k > 0 && (k == n || !(w[i] == s[k]))) {
k = p[k - 1];
}
if (w[i] == s[k]) {
k++;
}
if (k == n) {
res.push_back(i - n + 1);
}
}
return res;
// returns 0-indexed positions of occurrences of s in w
}

template <typename T>
vector<int> kmp_search(const T &s, const T &w, const vector<int> &p) {
return kmp_search((int) s.size(), s, (int) w.size(), w, p);
}

void err() {
cout << "NO" << '\n';
  exit(0);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
vector<string> have(n), want(n);
for (int i = 0; i < n; i++) {
    cin >> have[i];
}
for (int i = 0; i < n; i++) {
    cin >> want[i];
}
vector<int> first(n, -1), last(n, -1);
int mai = -1;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int) have[i].size(); j++) {
      if (have[i][j] != want[i][j]) {
        if (first[i] == -1) {
          first[i] = j;
        }
        last[i] = j;
      }
    }
    if (first[i] != -1) {
      mai = i;
    }
  }
  for (int i = 0; i < n; i++) {
    if (first[i] == -1) {
      continue;
    }
    if (last[i] - first[i] != last[mai] - first[mai]) {
      err();
    }
    for (int j = first[i]; j <= last[i]; j++) {
      if (have[i][j] != have[mai][j - first[i] + first[mai]]) {
        err();
      }
      if (want[i][j] != want[mai][j - first[i] + first[mai]]) {
        err();
      }
    }
  }
  while (true) {
    char c = '?';
    int ok = 1;
    for (int i = 0; i < n; i++) {
      if (first[i] == -1) {
        continue;
      }
      if (first[i] == 0) {
        ok = 0;
        break;
      }
      if (have[i][first[i] - 1] != c && c != '?') {
        ok = 0;
        break;
      }
      c = have[i][first[i] - 1];
    }
    if (!ok) {
      break;
    }
    for (int i = 0; i < n; i++) {
      if (first[i] == -1) {
        continue;
      }
      first[i]--;
    }
  }
  while (true) {
    char c = '?';
    int ok = 1;
    for (int i = 0; i < n; i++) {
      if (first[i] == -1) {
        continue;
      }
      if (last[i] == (int) have[i].size() - 1) {
        ok = 0;
        break;
      }
      if (have[i][last[i] + 1] != c && c != '?') {
        ok = 0;
        break;
      }
      c = have[i][last[i] + 1];
    }
    if (!ok) {
      break;
    }
    for (int i = 0; i < n; i++) {
      if (first[i] == -1) {
        continue;
      }
      last[i]++;
    }
  }
  string s = have[mai].substr(first[mai], last[mai] - first[mai] + 1);
  string t = want[mai].substr(first[mai], last[mai] - first[mai] + 1);
  vector<int> p = kmp_table(s);
for (int i = 0; i < n; i++) {
    vector<int> pos = kmp_search(s, have[i], p);
string get = have[i];
if (!pos.empty()) {
int x = pos[0];
for (int j = 0; j < (int) t.size(); j++) {
        get[x + j] = t[j];
      }
    }
    if (get != want[i]) {
      err();
    }
  }
  cout << "YES" << '\n';
  cout << s << '\n';
  cout << t << '\n';
  return 0;
}