#include <bits/stdc++.h>
using namespace std;
template <class T>
struct SuffixAutomaton {
  static const int sigma = 26;
  struct Node {
    int len;
    int slnk;
    bool cloned;
    int son[sigma];
    Node() : len(0), slnk(-1), cloned(false) { memset(son, 0, sizeof son); }
  };
  int sz, cur;
  vector<Node> nodes;
  SuffixAutomaton() { clear(); }
  int idx(const T& c) { return c - 'a'; }
  void clear() {
    sz = cur = 0;
    nodes.clear();
    add_node();
  }
  int add_node() {
    nodes.push_back(Node());
    return sz++;
  }
  Node clone(const Node& node) {
    Node ans;
    ans.len = node.len;
    ans.slnk = node.slnk;
    ans.cloned = true;
    memcpy(ans.son, node.son, sizeof ans.son);
    return ans;
  }
  int extend(const int& c) {
    int last = cur;
    bool flg = nodes[last].son[c];
    if (!flg) {
      cur = add_node();
      nodes[cur].len = nodes[last].len + 1;
    } else
      cur = nodes[last].son[c];
    int p = last;
    for (; p != -1 && !nodes[p].son[c]; p = nodes[p].slnk)
      nodes[p].son[c] = cur;
    if (p == -1)
      nodes[cur].slnk = 0;
    else {
      int q = nodes[p].son[c];
      if (nodes[p].len + 1 == nodes[q].len) {
        if (!flg) nodes[cur].slnk = q;
      } else {
        int r = add_node();
        nodes[r] = clone(nodes[q]);
        nodes[r].len = nodes[p].len + 1;
        nodes[q].slnk = nodes[cur].slnk = r;
        for (; p != -1 && nodes[p].son[c] == q; p = nodes[p].slnk)
          nodes[p].son[c] = r;
        if (flg) cur = r;
      }
    }
    return cur;
  }
  void build(const T* first, const T* last) {
    const T*& s = first;
    const int n = last - first;
    clear();
    for (int i = 0; i < n; i++) extend(idx(s[i]));
  }
  vector<int> sorted_indexes() {
    int mxlen = 0;
    for (int u = 0; u < sz; u++) mxlen = max(mxlen, nodes[u].len);
    vector<int> occ(mxlen + 1);
    for (int u = 0; u < sz; u++) occ[nodes[u].len]++;
    for (int i = 1; i <= mxlen; i++) occ[i] += occ[i - 1];
    vector<int> ans(sz);
    for (int u = 0; u < sz; u++) ans[--occ[nodes[u].len]] = u;
    return ans;
  }
  vector<int> sorted_indexes(const vector<int>& qry) {
    int mxlen = 0;
    for (const auto& u : qry) mxlen = max(mxlen, nodes[u].len);
    vector<int> occ(mxlen + 1);
    for (const auto& u : qry) occ[nodes[u].len]++;
    for (int i = 1; i <= mxlen; i++) occ[i] += occ[i - 1];
    vector<int> ans(qry.size());
    for (const auto& u : qry) ans[--occ[nodes[u].len]] = u;
    return ans;
  }
  Node& operator[](const int& x) { return nodes[x]; }
};
const int maxs = 500000;
const int maxm = 50000;
const int maxt = 50000;
const int maxsz = (maxs + maxt) * 2 - 1;
struct LowestCommonAncestor {
  static const int maxn = ::maxsz;
  static const int maxh = 21;
  int n;
  vector<int> E[maxn];
  int dph[maxn];
  int dad[maxn];
  int gpaf[maxn][maxh];
  LowestCommonAncestor() : n(0) {}
  LowestCommonAncestor(const int& n) : n(n) {}
  void resize(const int& n) {
    for (int i = 0; i < this->n; i++) E[i].clear();
    this->n = n;
  }
  void add_edge(const int& u, const int& v) { E[u].push_back(v); }
  void build(const int& root = 0) {
    memset(dph, 0x7f, sizeof dph[0] * n);
    dad[root] = -1;
    dph[root] = 0;
    queue<int> que;
    for (que.push(root); !que.empty(); que.pop()) {
      int u = que.front();
      for (const auto& v : E[u]) {
        if (dph[v] > dph[u] + 1) {
          dad[v] = u;
          dph[v] = dph[u] + 1;
          que.push(v);
        }
      }
    }
    for (int i = 0; i < n; i++) gpaf[i][0] = dad[i];
    for (int k = 1; k < maxh; k++) {
      for (int i = 0; i < n; i++) {
        gpaf[i][k] = dph[i] >= (1 << k) ? gpaf[gpaf[i][k - 1]][k - 1] : -1;
      }
    }
  }
  int gpa(int u, int h) const {
    for (int i = 0; h > 0; h >>= 1, i++) {
      if (h & 1) u = gpaf[u][i];
    }
    return u;
  }
  int lca(int u, int v) const {
    if (dph[u] > dph[v])
      u = gpa(u, dph[u] - dph[v]);
    else if (dph[u] < dph[v])
      v = gpa(v, dph[v] - dph[u]);
    if (u == v) return u;
    for (int k = maxh - 1; k >= 0; k--) {
      if (gpaf[u][k] != gpaf[v][k]) {
        u = gpaf[u][k];
        v = gpaf[v][k];
      }
    }
    return dad[u];
  }
  int distance(const int& u, const int& v) const {
    return dph[u] + dph[v] - dph[lca(u, v)] * 2;
  }
  bool on_path(const int& x, const int& u, const int& v) {
    int l = lca(u, v);
    return dph[l] <= dph[x] && dph[x] <= dph[u] &&
               gpa(u, dph[u] - dph[x]) == x ||
           dph[l] <= dph[x] && dph[x] <= dph[v] && gpa(v, dph[v] - dph[x]) == x;
  }
  pair<int, int> intersection(const int& u, const int& v, const int& x,
                              const int& y) {
    set<int> s;
    s.insert(u);
    s.insert(v);
    s.insert(x);
    s.insert(y);
    s.insert(lca(u, v));
    s.insert(lca(x, y));
    s.insert(lca(u, x));
    s.insert(lca(u, y));
    s.insert(lca(v, x));
    s.insert(lca(v, y));
    vector<int> vtx;
    for (const auto& p : s) {
      if (on_path(p, u, v) && on_path(p, x, y)) vtx.push_back(p);
    }
    if (vtx.empty()) return make_pair(-1, -1);
    vector<int> d;
    d.resize(vtx.size());
    for (int i = 0; i < d.size(); i++) d[i] = distance(u, vtx[i]);
    return make_pair(vtx[min_element(d.begin(), d.end()) - d.begin()],
                     vtx[max_element(d.begin(), d.end()) - d.begin()]);
  }
};
template <class T, class U = T>
struct RangeMaximumQuery {
  static const int maxh = 16;
  int n;
  vector<T> a;
  vector<array<U, maxh> > f;
  U select(const U& lhs, const U& rhs) const {
    return a[lhs] >= a[rhs] ? lhs : rhs;
  }
  RangeMaximumQuery() { clear(); }
  void clear() {
    n = 0;
    a.clear();
    f.clear();
  }
  void resize(const int& n) {
    this->n = n;
    a.resize(n);
    f.resize(n);
  }
  void update(const int& i, const bool& rht = false) {
    f[i][0] = i;
    if (!rht) {
      for (int k = 1, b; (b = 1 << k) <= i + 1; k++)
        f[i + 1 - b][k] =
            select(f[i + 1 - b][k - 1], f[i + 1 - (b >> 1)][k - 1]);
    } else {
      for (int k = 1, b; (b = 1 << k) <= n - i; k++)
        f[i][k] = select(f[i][k - 1], f[i + (b >> 1)][k - 1]);
    }
  }
  void push_back(const T& x) {
    n++;
    a.push_back(x);
    f.emplace_back();
    update(n - 1, false);
  }
  void build() {
    for (int i = 0; i < n; i++) update(i, false);
  }
  U query(const int& l, const int& r) const {
    int k = 0;
    for (; 1 << k + 1 < r - l; k++)
      ;
    return select(f[l][k], f[r - (1 << k)][k]);
  }
};
char s[maxs + 1];
int m;
char t[maxt + 1];
int pos[maxm];
int sl;
int sid[maxs];
SuffixAutomaton<char> SA;
LowestCommonAncestor LCA;
vector<short> tid[maxsz];
RangeMaximumQuery<short, short> RMQ[maxsz];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  cin >> m;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    cin >> t + p;
    pos[i] = p + strlen(t + p);
  }
  int sl = strlen(s);
  for (int i = 0; i < sl; i++) SA.extend(SA.idx(s[i]));
  SA.cur = 0;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    for (int j = p; j < pos[i]; j++) SA.extend(SA.idx(t[j]));
    SA.cur = 0;
  }
  LCA.resize(SA.sz);
  for (int u = 1; u < SA.sz; u++) LCA.add_edge(SA[u].slnk, u);
  LCA.build();
  for (int i = 0, u = 0; i < sl; i++) {
    u = SA[u].son[SA.idx(s[i])];
    sid[i] = u;
  }
  vector<bool> vis(SA.sz, false);
  vector<int> occ(SA.sz, 0);
  vis[0] = true;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    queue<int> que;
    vector<int> idx;
    for (int j = p, u = 0; j < pos[i]; j++) {
      u = SA[u].son[SA.idx(t[j])];
      vis[u] = true;
      occ[u] = 1;
      que.push(u);
    }
    for (; !que.empty(); que.pop()) {
      const int& u = que.front();
      idx.push_back(u);
      if (!vis[SA[u].slnk]) {
        vis[SA[u].slnk] = true;
        que.push(SA[u].slnk);
      }
    }
    idx = SA.sorted_indexes(idx);
    for (int j = idx.size() - 1; j >= 0; j--) {
      const int& u = idx[j];
      occ[SA[u].slnk] += occ[u];
    }
    for (const auto& u : idx) {
      tid[u].push_back(i);
      RMQ[u].push_back(occ[u]);
      vis[u] = false;
      occ[u] = 0;
    }
  }
  int q;
  cin >> q;
  for (int qn = 0; qn < q; qn++) {
    int l, r, pl, pr;
    cin >> l >> r >> pl >> pr;
    l--, pl--;
    int u = sid[pr - 1];
    for (int k = 20; k >= 0; k--) {
      int v = LCA.gpaf[u][k];
      if (v >= 0 && pr - pl <= SA[v].len) u = v;
    }
    int x = lower_bound(tid[u].begin(), tid[u].end(), l) - tid[u].begin();
    int y = lower_bound(tid[u].begin(), tid[u].end(), r) - tid[u].begin();
    int ans_idx, ans_occ;
    if (x == y) {
      ans_idx = l;
      ans_occ = 0;
    } else {
      int k = RMQ[u].query(x, y);
      ans_idx = tid[u][k];
      ans_occ = RMQ[u].a[k];
    }
    cout << ans_idx + 1 << ' ' << ans_occ << '\n';
  }
  return 0;
}

### Prompt

Your challenge is to write a cpp solution to the following problem:

The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.

Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.

All the m leaflets that are brought by the spy are numbered from 1 to m. After that it's needed to get the answer to q queries of the following kind: "In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?".

The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him!

Input

The first line contains the string s (1 ≤ |s| ≤ 5·105) — the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters.

The second line contains the only integer m (1 ≤ m ≤ 5·104) — the number of texts of leaflets for expertise.

Each of the next m lines contains the only string ti — the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5·104. The text of the leaflets consists of only lowercase English letters.

The next line contains integer q (1 ≤ q ≤ 5·105) — the number of queries for expertise.

Finally, each of the last q lines contains four integers l, r, pl, pr (1 ≤ l ≤ r ≤ m, 1 ≤ pl ≤ pr ≤ |s|), where |s| is the length of the Absolutely Inadmissible Swearword.

Output

Print q lines. The i-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one.

Examples

Input

suffixtree
3
suffixtreesareawesome
cartesiantreeisworsethansegmenttree
nyeeheeheee
2
1 2 1 10
1 3 9 10


Output

1 1
3 4

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
struct SuffixAutomaton {
  static const int sigma = 26;
  struct Node {
    int len;
    int slnk;
    bool cloned;
    int son[sigma];
    Node() : len(0), slnk(-1), cloned(false) { memset(son, 0, sizeof son); }
  };
  int sz, cur;
  vector<Node> nodes;
  SuffixAutomaton() { clear(); }
  int idx(const T& c) { return c - 'a'; }
  void clear() {
    sz = cur = 0;
    nodes.clear();
    add_node();
  }
  int add_node() {
    nodes.push_back(Node());
    return sz++;
  }
  Node clone(const Node& node) {
    Node ans;
    ans.len = node.len;
    ans.slnk = node.slnk;
    ans.cloned = true;
    memcpy(ans.son, node.son, sizeof ans.son);
    return ans;
  }
  int extend(const int& c) {
    int last = cur;
    bool flg = nodes[last].son[c];
    if (!flg) {
      cur = add_node();
      nodes[cur].len = nodes[last].len + 1;
    } else
      cur = nodes[last].son[c];
    int p = last;
    for (; p != -1 && !nodes[p].son[c]; p = nodes[p].slnk)
      nodes[p].son[c] = cur;
    if (p == -1)
      nodes[cur].slnk = 0;
    else {
      int q = nodes[p].son[c];
      if (nodes[p].len + 1 == nodes[q].len) {
        if (!flg) nodes[cur].slnk = q;
      } else {
        int r = add_node();
        nodes[r] = clone(nodes[q]);
        nodes[r].len = nodes[p].len + 1;
        nodes[q].slnk = nodes[cur].slnk = r;
        for (; p != -1 && nodes[p].son[c] == q; p = nodes[p].slnk)
          nodes[p].son[c] = r;
        if (flg) cur = r;
      }
    }
    return cur;
  }
  void build(const T* first, const T* last) {
    const T*& s = first;
    const int n = last - first;
    clear();
    for (int i = 0; i < n; i++) extend(idx(s[i]));
  }
  vector<int> sorted_indexes() {
    int mxlen = 0;
    for (int u = 0; u < sz; u++) mxlen = max(mxlen, nodes[u].len);
    vector<int> occ(mxlen + 1);
    for (int u = 0; u < sz; u++) occ[nodes[u].len]++;
    for (int i = 1; i <= mxlen; i++) occ[i] += occ[i - 1];
    vector<int> ans(sz);
    for (int u = 0; u < sz; u++) ans[--occ[nodes[u].len]] = u;
    return ans;
  }
  vector<int> sorted_indexes(const vector<int>& qry) {
    int mxlen = 0;
    for (const auto& u : qry) mxlen = max(mxlen, nodes[u].len);
    vector<int> occ(mxlen + 1);
    for (const auto& u : qry) occ[nodes[u].len]++;
    for (int i = 1; i <= mxlen; i++) occ[i] += occ[i - 1];
    vector<int> ans(qry.size());
    for (const auto& u : qry) ans[--occ[nodes[u].len]] = u;
    return ans;
  }
  Node& operator[](const int& x) { return nodes[x]; }
};
const int maxs = 500000;
const int maxm = 50000;
const int maxt = 50000;
const int maxsz = (maxs + maxt) * 2 - 1;
struct LowestCommonAncestor {
  static const int maxn = ::maxsz;
  static const int maxh = 21;
  int n;
  vector<int> E[maxn];
  int dph[maxn];
  int dad[maxn];
  int gpaf[maxn][maxh];
  LowestCommonAncestor() : n(0) {}
  LowestCommonAncestor(const int& n) : n(n) {}
  void resize(const int& n) {
    for (int i = 0; i < this->n; i++) E[i].clear();
    this->n = n;
  }
  void add_edge(const int& u, const int& v) { E[u].push_back(v); }
  void build(const int& root = 0) {
    memset(dph, 0x7f, sizeof dph[0] * n);
    dad[root] = -1;
    dph[root] = 0;
    queue<int> que;
    for (que.push(root); !que.empty(); que.pop()) {
      int u = que.front();
      for (const auto& v : E[u]) {
        if (dph[v] > dph[u] + 1) {
          dad[v] = u;
          dph[v] = dph[u] + 1;
          que.push(v);
        }
      }
    }
    for (int i = 0; i < n; i++) gpaf[i][0] = dad[i];
    for (int k = 1; k < maxh; k++) {
      for (int i = 0; i < n; i++) {
        gpaf[i][k] = dph[i] >= (1 << k) ? gpaf[gpaf[i][k - 1]][k - 1] : -1;
      }
    }
  }
  int gpa(int u, int h) const {
    for (int i = 0; h > 0; h >>= 1, i++) {
      if (h & 1) u = gpaf[u][i];
    }
    return u;
  }
  int lca(int u, int v) const {
    if (dph[u] > dph[v])
      u = gpa(u, dph[u] - dph[v]);
    else if (dph[u] < dph[v])
      v = gpa(v, dph[v] - dph[u]);
    if (u == v) return u;
    for (int k = maxh - 1; k >= 0; k--) {
      if (gpaf[u][k] != gpaf[v][k]) {
        u = gpaf[u][k];
        v = gpaf[v][k];
      }
    }
    return dad[u];
  }
  int distance(const int& u, const int& v) const {
    return dph[u] + dph[v] - dph[lca(u, v)] * 2;
  }
  bool on_path(const int& x, const int& u, const int& v) {
    int l = lca(u, v);
    return dph[l] <= dph[x] && dph[x] <= dph[u] &&
               gpa(u, dph[u] - dph[x]) == x ||
           dph[l] <= dph[x] && dph[x] <= dph[v] && gpa(v, dph[v] - dph[x]) == x;
  }
  pair<int, int> intersection(const int& u, const int& v, const int& x,
                              const int& y) {
    set<int> s;
    s.insert(u);
    s.insert(v);
    s.insert(x);
    s.insert(y);
    s.insert(lca(u, v));
    s.insert(lca(x, y));
    s.insert(lca(u, x));
    s.insert(lca(u, y));
    s.insert(lca(v, x));
    s.insert(lca(v, y));
    vector<int> vtx;
    for (const auto& p : s) {
      if (on_path(p, u, v) && on_path(p, x, y)) vtx.push_back(p);
    }
    if (vtx.empty()) return make_pair(-1, -1);
    vector<int> d;
    d.resize(vtx.size());
    for (int i = 0; i < d.size(); i++) d[i] = distance(u, vtx[i]);
    return make_pair(vtx[min_element(d.begin(), d.end()) - d.begin()],
                     vtx[max_element(d.begin(), d.end()) - d.begin()]);
  }
};
template <class T, class U = T>
struct RangeMaximumQuery {
  static const int maxh = 16;
  int n;
  vector<T> a;
  vector<array<U, maxh> > f;
  U select(const U& lhs, const U& rhs) const {
    return a[lhs] >= a[rhs] ? lhs : rhs;
  }
  RangeMaximumQuery() { clear(); }
  void clear() {
    n = 0;
    a.clear();
    f.clear();
  }
  void resize(const int& n) {
    this->n = n;
    a.resize(n);
    f.resize(n);
  }
  void update(const int& i, const bool& rht = false) {
    f[i][0] = i;
    if (!rht) {
      for (int k = 1, b; (b = 1 << k) <= i + 1; k++)
        f[i + 1 - b][k] =
            select(f[i + 1 - b][k - 1], f[i + 1 - (b >> 1)][k - 1]);
    } else {
      for (int k = 1, b; (b = 1 << k) <= n - i; k++)
        f[i][k] = select(f[i][k - 1], f[i + (b >> 1)][k - 1]);
    }
  }
  void push_back(const T& x) {
    n++;
    a.push_back(x);
    f.emplace_back();
    update(n - 1, false);
  }
  void build() {
    for (int i = 0; i < n; i++) update(i, false);
  }
  U query(const int& l, const int& r) const {
    int k = 0;
    for (; 1 << k + 1 < r - l; k++)
      ;
    return select(f[l][k], f[r - (1 << k)][k]);
  }
};
char s[maxs + 1];
int m;
char t[maxt + 1];
int pos[maxm];
int sl;
int sid[maxs];
SuffixAutomaton<char> SA;
LowestCommonAncestor LCA;
vector<short> tid[maxsz];
RangeMaximumQuery<short, short> RMQ[maxsz];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  cin >> m;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    cin >> t + p;
    pos[i] = p + strlen(t + p);
  }
  int sl = strlen(s);
  for (int i = 0; i < sl; i++) SA.extend(SA.idx(s[i]));
  SA.cur = 0;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    for (int j = p; j < pos[i]; j++) SA.extend(SA.idx(t[j]));
    SA.cur = 0;
  }
  LCA.resize(SA.sz);
  for (int u = 1; u < SA.sz; u++) LCA.add_edge(SA[u].slnk, u);
  LCA.build();
  for (int i = 0, u = 0; i < sl; i++) {
    u = SA[u].son[SA.idx(s[i])];
    sid[i] = u;
  }
  vector<bool> vis(SA.sz, false);
  vector<int> occ(SA.sz, 0);
  vis[0] = true;
  for (int i = 0, p = 0; i < m; p = pos[i++]) {
    queue<int> que;
    vector<int> idx;
    for (int j = p, u = 0; j < pos[i]; j++) {
      u = SA[u].son[SA.idx(t[j])];
      vis[u] = true;
      occ[u] = 1;
      que.push(u);
    }
    for (; !que.empty(); que.pop()) {
      const int& u = que.front();
      idx.push_back(u);
      if (!vis[SA[u].slnk]) {
        vis[SA[u].slnk] = true;
        que.push(SA[u].slnk);
      }
    }
    idx = SA.sorted_indexes(idx);
    for (int j = idx.size() - 1; j >= 0; j--) {
      const int& u = idx[j];
      occ[SA[u].slnk] += occ[u];
    }
    for (const auto& u : idx) {
      tid[u].push_back(i);
      RMQ[u].push_back(occ[u]);
      vis[u] = false;
      occ[u] = 0;
    }
  }
  int q;
  cin >> q;
  for (int qn = 0; qn < q; qn++) {
    int l, r, pl, pr;
    cin >> l >> r >> pl >> pr;
    l--, pl--;
    int u = sid[pr - 1];
    for (int k = 20; k >= 0; k--) {
      int v = LCA.gpaf[u][k];
      if (v >= 0 && pr - pl <= SA[v].len) u = v;
    }
    int x = lower_bound(tid[u].begin(), tid[u].end(), l) - tid[u].begin();
    int y = lower_bound(tid[u].begin(), tid[u].end(), r) - tid[u].begin();
    int ans_idx, ans_occ;
    if (x == y) {
      ans_idx = l;
      ans_occ = 0;
    } else {
      int k = RMQ[u].query(x, y);
      ans_idx = tid[u][k];
      ans_occ = RMQ[u].a[k];
    }
    cout << ans_idx + 1 << ' ' << ans_occ << '\n';
  }
  return 0;
}

```