#include <bits/stdc++.h>
using namespace std;
const int S = 500000 + 5, M = 50000 + 5;
char ss[S];
int len_ss;
int m;
char sm[M];
int ed[M];
const int ND = (S + M) * 2;
int nwnd = 1;
int trans[ND][26], fa[ND], maxlen[ND];
inline int add_char(int last, int c) {
  if (trans[last][c] && maxlen[trans[last][c]] == maxlen[last] + 1)
    return trans[last][c];
  int cur = ++nwnd, p = last;
  maxlen[cur] = maxlen[last] + 1;
  for (; p && !trans[p][c]; p = fa[p]) trans[p][c] = cur;
  if (!p)
    fa[cur] = 1;
  else {
    int q = trans[p][c];
    if (maxlen[q] == maxlen[p] + 1)
      fa[cur] = q;
    else {
      int sq = ++nwnd;
      maxlen[sq] = maxlen[p] + 1;
      memcpy(trans[sq], trans[q], sizeof(trans[q]));
      fa[sq] = fa[q];
      fa[q] = fa[cur] = sq;
      for (; trans[p][c] == q; p = fa[p]) trans[p][c] = sq;
    }
  }
  return cur;
}
const int SEG = M * 30;
struct Info {
  int mx, mxid;
  inline Info() {}
  inline Info(int _mx, int _mxid) : mx(_mx), mxid(_mxid) {}
};
inline bool operator<(const Info &a, const Info &b) {
  if (a.mx != b.mx)
    return a.mx < b.mx;
  else
    return a.mxid > b.mxid;
}
int nwseg;
int lc[SEG], rc[SEG];
Info info[SEG];
int change_p(int idx0, int l, int r, int p, int v) {
  int idx = ++nwseg;
  if (l != r) {
    int mid = (l + r) >> 1;
    if (p <= mid)
      lc[idx] = change_p(lc[idx0], l, mid, p, v), rc[idx] = rc[idx0];
    else
      lc[idx] = lc[idx0], rc[idx] = change_p(rc[idx0], mid + 1, r, p, v);
    info[idx] = max(info[lc[idx]], info[rc[idx]]);
  } else {
    info[idx] = (idx0 ? info[idx0] : Info(0, l));
    info[idx].mx += v;
  }
  return idx;
}
int merge_p(int idx1, int idx2, int l, int r) {
  if (!idx1 || !idx2) return idx1 + idx2;
  int idx = ++nwseg;
  if (l == r) {
    info[idx] = Info(info[idx1].mx + info[idx2].mx, l);
  } else {
    int mid = (l + r) >> 1;
    lc[idx] = merge_p(lc[idx1], lc[idx2], l, mid);
    rc[idx] = merge_p(rc[idx1], rc[idx2], mid + 1, r);
    info[idx] = max(info[lc[idx]], info[rc[idx]]);
  }
  return idx;
}
Info query(int idx, int l, int r, int L, int R) {
  if (!idx) return Info(0, L);
  if (L <= l && r <= R) return info[idx];
  int mid = (l + r) >> 1;
  if (R <= mid)
    return query(lc[idx], l, mid, L, R);
  else if (L > mid)
    return query(rc[idx], mid + 1, r, L, R);
  else
    return max(query(lc[idx], l, mid, L, R), query(rc[idx], mid + 1, r, L, R));
}
int seg[ND];
int rnd[S];
int od[ND];
inline bool cmp_sam_nd_maxlen(int a, int b) { return maxlen[a] < maxlen[b]; }
int pf[ND][21];
int main() {
  scanf("%s%d", ss + 1, &m);
  len_ss = (int)strlen(ss + 1);
  for (int i = 1; i <= m; ++i) {
    scanf("%s", sm + ed[i - 1]);
    ed[i] = ed[i - 1];
    while (sm[ed[i]]) ++ed[i];
  }
  for (int i = 1, last = 1; i <= len_ss; ++i)
    last = add_char(last, ss[i] - 'a');
  for (int i = 1; i <= m; ++i)
    for (int j = ed[i - 1], last = 1; j < ed[i]; ++j) {
      last = add_char(last, sm[j] - 'a');
    }
  rnd[0] = 1;
  for (int i = 1; i <= len_ss; ++i) rnd[i] = trans[rnd[i - 1]][ss[i] - 'a'];
  for (int i = 1; i <= m; ++i)
    for (int j = ed[i - 1], last = 1; j < ed[i]; ++j) {
      last = trans[last][sm[j] - 'a'];
      seg[last] = change_p(seg[last], 1, m, i, 1);
    }
  for (int i = 1; i <= nwnd; ++i) od[i] = i;
  sort(od + 1, od + nwnd + 1, cmp_sam_nd_maxlen);
  for (int it = nwnd; it >= 2; --it) {
    int i = od[it];
    seg[fa[i]] = merge_p(seg[fa[i]], seg[i], 1, m);
  }
  for (int i = 1; i <= nwnd; ++i) pf[i][0] = fa[i];
  for (int p = 1; p <= 20; ++p)
    for (int i = 1; i <= nwnd; ++i) pf[i][p] = pf[pf[i][p - 1]][p - 1];
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r, pl, pr;
    scanf("%d%d%d%d", &l, &r, &pl, &pr);
    int nd = rnd[pr];
    for (int p = 20; p >= 0; --p)
      if (maxlen[pf[nd][p]] >= pr - pl + 1) nd = pf[nd][p];
    Info ret = query(seg[nd], 1, m, l, r);
    printf("%d %d\n", ret.mxid, ret.mx);
  }
  return 0;
}

### Prompt

Your task is to create 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;
const int S = 500000 + 5, M = 50000 + 5;
char ss[S];
int len_ss;
int m;
char sm[M];
int ed[M];
const int ND = (S + M) * 2;
int nwnd = 1;
int trans[ND][26], fa[ND], maxlen[ND];
inline int add_char(int last, int c) {
  if (trans[last][c] && maxlen[trans[last][c]] == maxlen[last] + 1)
    return trans[last][c];
  int cur = ++nwnd, p = last;
  maxlen[cur] = maxlen[last] + 1;
  for (; p && !trans[p][c]; p = fa[p]) trans[p][c] = cur;
  if (!p)
    fa[cur] = 1;
  else {
    int q = trans[p][c];
    if (maxlen[q] == maxlen[p] + 1)
      fa[cur] = q;
    else {
      int sq = ++nwnd;
      maxlen[sq] = maxlen[p] + 1;
      memcpy(trans[sq], trans[q], sizeof(trans[q]));
      fa[sq] = fa[q];
      fa[q] = fa[cur] = sq;
      for (; trans[p][c] == q; p = fa[p]) trans[p][c] = sq;
    }
  }
  return cur;
}
const int SEG = M * 30;
struct Info {
  int mx, mxid;
  inline Info() {}
  inline Info(int _mx, int _mxid) : mx(_mx), mxid(_mxid) {}
};
inline bool operator<(const Info &a, const Info &b) {
  if (a.mx != b.mx)
    return a.mx < b.mx;
  else
    return a.mxid > b.mxid;
}
int nwseg;
int lc[SEG], rc[SEG];
Info info[SEG];
int change_p(int idx0, int l, int r, int p, int v) {
  int idx = ++nwseg;
  if (l != r) {
    int mid = (l + r) >> 1;
    if (p <= mid)
      lc[idx] = change_p(lc[idx0], l, mid, p, v), rc[idx] = rc[idx0];
    else
      lc[idx] = lc[idx0], rc[idx] = change_p(rc[idx0], mid + 1, r, p, v);
    info[idx] = max(info[lc[idx]], info[rc[idx]]);
  } else {
    info[idx] = (idx0 ? info[idx0] : Info(0, l));
    info[idx].mx += v;
  }
  return idx;
}
int merge_p(int idx1, int idx2, int l, int r) {
  if (!idx1 || !idx2) return idx1 + idx2;
  int idx = ++nwseg;
  if (l == r) {
    info[idx] = Info(info[idx1].mx + info[idx2].mx, l);
  } else {
    int mid = (l + r) >> 1;
    lc[idx] = merge_p(lc[idx1], lc[idx2], l, mid);
    rc[idx] = merge_p(rc[idx1], rc[idx2], mid + 1, r);
    info[idx] = max(info[lc[idx]], info[rc[idx]]);
  }
  return idx;
}
Info query(int idx, int l, int r, int L, int R) {
  if (!idx) return Info(0, L);
  if (L <= l && r <= R) return info[idx];
  int mid = (l + r) >> 1;
  if (R <= mid)
    return query(lc[idx], l, mid, L, R);
  else if (L > mid)
    return query(rc[idx], mid + 1, r, L, R);
  else
    return max(query(lc[idx], l, mid, L, R), query(rc[idx], mid + 1, r, L, R));
}
int seg[ND];
int rnd[S];
int od[ND];
inline bool cmp_sam_nd_maxlen(int a, int b) { return maxlen[a] < maxlen[b]; }
int pf[ND][21];
int main() {
  scanf("%s%d", ss + 1, &m);
  len_ss = (int)strlen(ss + 1);
  for (int i = 1; i <= m; ++i) {
    scanf("%s", sm + ed[i - 1]);
    ed[i] = ed[i - 1];
    while (sm[ed[i]]) ++ed[i];
  }
  for (int i = 1, last = 1; i <= len_ss; ++i)
    last = add_char(last, ss[i] - 'a');
  for (int i = 1; i <= m; ++i)
    for (int j = ed[i - 1], last = 1; j < ed[i]; ++j) {
      last = add_char(last, sm[j] - 'a');
    }
  rnd[0] = 1;
  for (int i = 1; i <= len_ss; ++i) rnd[i] = trans[rnd[i - 1]][ss[i] - 'a'];
  for (int i = 1; i <= m; ++i)
    for (int j = ed[i - 1], last = 1; j < ed[i]; ++j) {
      last = trans[last][sm[j] - 'a'];
      seg[last] = change_p(seg[last], 1, m, i, 1);
    }
  for (int i = 1; i <= nwnd; ++i) od[i] = i;
  sort(od + 1, od + nwnd + 1, cmp_sam_nd_maxlen);
  for (int it = nwnd; it >= 2; --it) {
    int i = od[it];
    seg[fa[i]] = merge_p(seg[fa[i]], seg[i], 1, m);
  }
  for (int i = 1; i <= nwnd; ++i) pf[i][0] = fa[i];
  for (int p = 1; p <= 20; ++p)
    for (int i = 1; i <= nwnd; ++i) pf[i][p] = pf[pf[i][p - 1]][p - 1];
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r, pl, pr;
    scanf("%d%d%d%d", &l, &r, &pl, &pr);
    int nd = rnd[pr];
    for (int p = 20; p >= 0; --p)
      if (maxlen[pf[nd][p]] >= pr - pl + 1) nd = pf[nd][p];
    Info ret = query(seg[nd], 1, m, l, r);
    printf("%d %d\n", ret.mxid, ret.mx);
  }
  return 0;
}

```