#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
const int M = 1000010;
vector<int> e[M];
vector<int> ups[M];
struct qry {
  int id;
  int x, y;
  int tp, len;
};
bool cmp(const qry &a, const qry &b) {
  if (a.len == b.len) return a.tp > b.tp;
  return a.len > b.len;
}
vector<qry> qrs[M];
bool saved[M];
int fa[M], sz[M], wson[M];
struct SuffixAutomaton {
  int sz, lst;
  int ch[M][26];
  int len[M], link[M];
  int ed[M];
  void init() {
    create(0);
    lst = 0;
  }
  int create(int id = 0) {
    memset(ch[id], 0, sizeof(ch[0]));
    len[id] = link[id] = ed[id] = 0;
    return sz = id;
  }
  void extend(const char *s) {
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      int cur = create(sz + 1);
      len[cur] = len[lst] + 1;
      int p = lst;
      while (ch[p][c] == 0) {
        ch[p][c] = cur;
        p = link[p];
      }
      if (ch[p][c] != cur) {
        int q = ch[p][c];
        if (len[p] + 1 == len[q]) {
          link[cur] = q;
        } else {
          int clone = create(sz + 1);
          memcpy(ch[clone], ch[q], sizeof(ch[q]));
          link[clone] = link[q];
          len[clone] = len[p] + 1;
          while (ch[p][c] == q) {
            ch[p][c] = clone;
            p = link[p];
          }
          link[q] = link[cur] = clone;
        }
      }
      ed[i + 1] = lst = cur;
    }
  }
  void build() {
    for (int i = 1; i <= sz; ++i) e[link[i]].push_back(i);
  }
  void run(const char *s, int id) {
    int x = 0, now = 0;
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      while (x && ch[x][c] == 0) {
        x = link[x];
        now = len[x];
      }
      if (ch[x][c]) {
        x = ch[x][c];
        ++now;
      }
      while (x && len[link[x]] >= now) x = link[x];
      if (now == len[x]) {
        ups[x].push_back(id);
      } else {
        ups[link[x]].push_back(id);
        qrs[x].push_back({id, 0, 0, 1, now});
      }
    }
  }
  int fuck(int x, int le) {
    x = ed[x];
    while (len[link[x]] >= le) x = link[x];
    return x;
  }
} SAM;
char s[500010];
char t[50010];
pair<int, int> g[200040];
pair<int, int> f[200040];
pair<int, int> ans[500010];
void pre(int);
void add(int);
void sub(int);
void dfs(int, bool);
void push_up(int, int, int);
void modify(int, int, int, int, int);
pair<int, int> query(int, int, int, int, int);
void push_up2(int, int, int);
void modify2(int, int, int, int, int);
pair<int, int> query2(int, int, int, int, int);
int main() {
  int n, m;
  int x, y, l, r;
  scanf("%s", s);
  SAM.extend(s);
  SAM.build();
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    SAM.run(t, i);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d%d", &x, &y, &l, &r);
    int pt = SAM.fuck(r, r - l + 1);
    qrs[pt].push_back({i, x, y, 0, r - l + 1});
  }
  for (int i = 0; i <= M; ++i) sort(qrs[i].begin(), qrs[i].end(), cmp);
  pre(0);
  dfs(0, true);
  for (int i = 1; i <= m; ++i) printf("%d %d\n", ans[i].second, ans[i].first);
  return 0;
}
void pre(int x) {
  sz[x] = 1;
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    fa[v] = x;
    pre(v);
    if (sz[v] > sz[wson[x]]) wson[x] = v;
    sz[x] += sz[v];
  }
}
void add(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, 1);
  for (auto v : e[x]) {
    if (v == fa[x] || saved[v]) continue;
    add(v);
  }
}
void sub(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, -1);
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    sub(v);
  }
}
void dfs(int x, bool keep) {
  for (auto v : e[x]) {
    if (v == fa[x] || v == wson[x]) continue;
    dfs(v, false);
  }
  if (wson[x]) {
    dfs(wson[x], true);
    saved[wson[x]] = true;
  }
  add(x);
  for (auto v : qrs[x]) {
    if (v.tp == 0) {
      pair<int, int> t1 = query(1, 1, 50000, v.x, v.y);
      pair<int, int> t2 = query2(1, 1, 50000, v.x, v.y);
      if (t1.first < t2.first) {
        t1 = t2;
      } else if (t1.first == t2.first) {
        t1.second = min(t1.second, t2.second);
      }
      if (t1.first == 0) t1.second = v.x;
      ans[v.id] = t1;
    } else {
      modify2(1, 1, 50000, v.id, 1);
    }
  }
  for (auto v : qrs[x]) {
    if (v.tp == 1) modify2(1, 1, 50000, v.id, -1);
  }
  if (wson[x]) saved[wson[x]] = false;
  if (!keep) sub(x);
}
void push_up(int cur, int lc, int rc) {
  if (f[lc].first >= f[rc].first) {
    f[cur] = f[lc];
  } else {
    f[cur] = f[rc];
  }
}
void modify(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    f[cur].first += k;
    f[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify(t, l, mid, x, k);
  else
    modify(t + 1, mid + 1, r, x, k);
  push_up(cur, t, t + 1);
}
pair<int, int> query(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return f[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}
void push_up2(int cur, int lc, int rc) {
  if (g[lc].first >= g[rc].first) {
    g[cur] = g[lc];
  } else {
    g[cur] = g[rc];
  }
}
void modify2(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    g[cur].first += k;
    g[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify2(t, l, mid, x, k);
  else
    modify2(t + 1, mid + 1, r, x, k);
  push_up2(cur, t, t + 1);
}
pair<int, int> query2(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return g[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query2(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query2(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}

### Prompt

Create a solution in cpp for 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 <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
const int M = 1000010;
vector<int> e[M];
vector<int> ups[M];
struct qry {
  int id;
  int x, y;
  int tp, len;
};
bool cmp(const qry &a, const qry &b) {
  if (a.len == b.len) return a.tp > b.tp;
  return a.len > b.len;
}
vector<qry> qrs[M];
bool saved[M];
int fa[M], sz[M], wson[M];
struct SuffixAutomaton {
  int sz, lst;
  int ch[M][26];
  int len[M], link[M];
  int ed[M];
  void init() {
    create(0);
    lst = 0;
  }
  int create(int id = 0) {
    memset(ch[id], 0, sizeof(ch[0]));
    len[id] = link[id] = ed[id] = 0;
    return sz = id;
  }
  void extend(const char *s) {
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      int cur = create(sz + 1);
      len[cur] = len[lst] + 1;
      int p = lst;
      while (ch[p][c] == 0) {
        ch[p][c] = cur;
        p = link[p];
      }
      if (ch[p][c] != cur) {
        int q = ch[p][c];
        if (len[p] + 1 == len[q]) {
          link[cur] = q;
        } else {
          int clone = create(sz + 1);
          memcpy(ch[clone], ch[q], sizeof(ch[q]));
          link[clone] = link[q];
          len[clone] = len[p] + 1;
          while (ch[p][c] == q) {
            ch[p][c] = clone;
            p = link[p];
          }
          link[q] = link[cur] = clone;
        }
      }
      ed[i + 1] = lst = cur;
    }
  }
  void build() {
    for (int i = 1; i <= sz; ++i) e[link[i]].push_back(i);
  }
  void run(const char *s, int id) {
    int x = 0, now = 0;
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      while (x && ch[x][c] == 0) {
        x = link[x];
        now = len[x];
      }
      if (ch[x][c]) {
        x = ch[x][c];
        ++now;
      }
      while (x && len[link[x]] >= now) x = link[x];
      if (now == len[x]) {
        ups[x].push_back(id);
      } else {
        ups[link[x]].push_back(id);
        qrs[x].push_back({id, 0, 0, 1, now});
      }
    }
  }
  int fuck(int x, int le) {
    x = ed[x];
    while (len[link[x]] >= le) x = link[x];
    return x;
  }
} SAM;
char s[500010];
char t[50010];
pair<int, int> g[200040];
pair<int, int> f[200040];
pair<int, int> ans[500010];
void pre(int);
void add(int);
void sub(int);
void dfs(int, bool);
void push_up(int, int, int);
void modify(int, int, int, int, int);
pair<int, int> query(int, int, int, int, int);
void push_up2(int, int, int);
void modify2(int, int, int, int, int);
pair<int, int> query2(int, int, int, int, int);
int main() {
  int n, m;
  int x, y, l, r;
  scanf("%s", s);
  SAM.extend(s);
  SAM.build();
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    SAM.run(t, i);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d%d", &x, &y, &l, &r);
    int pt = SAM.fuck(r, r - l + 1);
    qrs[pt].push_back({i, x, y, 0, r - l + 1});
  }
  for (int i = 0; i <= M; ++i) sort(qrs[i].begin(), qrs[i].end(), cmp);
  pre(0);
  dfs(0, true);
  for (int i = 1; i <= m; ++i) printf("%d %d\n", ans[i].second, ans[i].first);
  return 0;
}
void pre(int x) {
  sz[x] = 1;
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    fa[v] = x;
    pre(v);
    if (sz[v] > sz[wson[x]]) wson[x] = v;
    sz[x] += sz[v];
  }
}
void add(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, 1);
  for (auto v : e[x]) {
    if (v == fa[x] || saved[v]) continue;
    add(v);
  }
}
void sub(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, -1);
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    sub(v);
  }
}
void dfs(int x, bool keep) {
  for (auto v : e[x]) {
    if (v == fa[x] || v == wson[x]) continue;
    dfs(v, false);
  }
  if (wson[x]) {
    dfs(wson[x], true);
    saved[wson[x]] = true;
  }
  add(x);
  for (auto v : qrs[x]) {
    if (v.tp == 0) {
      pair<int, int> t1 = query(1, 1, 50000, v.x, v.y);
      pair<int, int> t2 = query2(1, 1, 50000, v.x, v.y);
      if (t1.first < t2.first) {
        t1 = t2;
      } else if (t1.first == t2.first) {
        t1.second = min(t1.second, t2.second);
      }
      if (t1.first == 0) t1.second = v.x;
      ans[v.id] = t1;
    } else {
      modify2(1, 1, 50000, v.id, 1);
    }
  }
  for (auto v : qrs[x]) {
    if (v.tp == 1) modify2(1, 1, 50000, v.id, -1);
  }
  if (wson[x]) saved[wson[x]] = false;
  if (!keep) sub(x);
}
void push_up(int cur, int lc, int rc) {
  if (f[lc].first >= f[rc].first) {
    f[cur] = f[lc];
  } else {
    f[cur] = f[rc];
  }
}
void modify(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    f[cur].first += k;
    f[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify(t, l, mid, x, k);
  else
    modify(t + 1, mid + 1, r, x, k);
  push_up(cur, t, t + 1);
}
pair<int, int> query(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return f[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}
void push_up2(int cur, int lc, int rc) {
  if (g[lc].first >= g[rc].first) {
    g[cur] = g[lc];
  } else {
    g[cur] = g[rc];
  }
}
void modify2(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    g[cur].first += k;
    g[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify2(t, l, mid, x, k);
  else
    modify2(t + 1, mid + 1, r, x, k);
  push_up2(cur, t, t + 1);
}
pair<int, int> query2(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return g[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query2(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query2(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}

```