#include <bits/stdc++.h>
using namespace std;
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
  return BigMod(b, m - 2, m);
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
T Dis(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T>
void ia(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
template <class T, class first>
inline T togglebit(T a, first i) {
  T t = 1;
  return (a ^ (t << i));
}
double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); }
double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); }
const int N = (int)6e5 + 7;
char s[600007];
string ss[600007];
int SA[600007], tmpSA[600007], Grp[600007], tmpGrp[600007], GrpSt[600007],
    Counting[600007], Rank[600007], LCP[600007];
void ComputeLCP(int L) {
  int k = 0;
  for (int i = 0; i < L; i++) {
    if (k) k--;
    int j = SA[Rank[i] - 1];
    while (i + k < L && j + k < L && s[i + k] == s[j + k]) k++;
    LCP[Rank[i]] = k;
  }
}
int SuffixArray(int mxAlpha) {
  int L = 0;
  for (int i = 0; i < mxAlpha; i++) Counting[i] = 0;
  for (L = 0; s[L]; L++) Counting[s[L]]++;
  for (int i = 1; i < mxAlpha; i++) Counting[i] += Counting[i - 1];
  for (int i = L - 1; i >= 0; i--)
    SA[Counting[s[i]]--] = i, Rank[i] = Counting[s[i]] + 1;
  int gNo = 0;
  GrpSt[0] = 0;
  tmpSA[0] = SA[0] = L;
  Grp[L] = 0;
  tmpGrp[L] = 0;
  for (int i = 1; i <= L; i++) {
    if (s[SA[i]] != s[SA[i - 1]]) gNo++, GrpSt[gNo] = i;
    Grp[SA[i]] = gNo;
  }
  int x = 0;
  for (int d = 1; d < L; d <<= 1) {
    x++;
    for (int i = L - d; i < L; i++) tmpSA[GrpSt[Grp[i]]++] = i;
    for (int i = 1; i <= L; i++)
      if (SA[i] >= d) tmpSA[GrpSt[Grp[SA[i] - d]]++] = SA[i] - d;
    gNo = 0;
    GrpSt[0] = 0;
    for (int i = 1; i <= L; i++) {
      if (Grp[tmpSA[i]] != Grp[tmpSA[i - 1]])
        gNo++, GrpSt[gNo] = i;
      else if (tmpSA[i] + d >= L)
        gNo++, GrpSt[gNo] = i;
      else if (tmpSA[i - 1] + d >= L)
        gNo++, GrpSt[gNo] = i;
      else if (Grp[tmpSA[i] + d] != Grp[tmpSA[i - 1] + d])
        gNo++, GrpSt[gNo] = i;
      SA[i] = tmpSA[i];
      Rank[SA[i]] = i;
      tmpGrp[SA[i]] = gNo;
    }
    if (gNo == L) break;
    for (int i = 0; i < L; i++) Grp[i] = tmpGrp[i];
  }
  ComputeLCP(L);
  return L;
}
int sqz;
struct Query {
  int l, r, id;
  int ll, rr;
  Query() {}
  Query(int _l, int _r, int _id, int _ll, int _rr) {
    l = _l;
    r = _r;
    id = _id;
    ll = _ll;
    rr = _rr;
  }
  bool operator<(const Query &p) const {
    if (l / sqz < p.l / sqz)
      return true;
    else if (l / sqz == p.l / sqz)
      return r < p.r;
  }
};
Query q[N];
pair<int, int> ans[N];
int id[N];
pair<int, int> Tree[4 * N];
int cntt;
void Build(int idx, int l, int r) {
  if (l == r) {
    Tree[idx] = make_pair(LCP[l], l);
    return;
  }
  Build((idx << 1), l, ((l + r) >> 1));
  Build(((idx << 1) | 1), ((l + r) >> 1) + 1, r);
  Tree[idx] = min(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
void Clear(int idx, int l, int r) {
  if (l == r) {
    Tree[idx] = make_pair(0, -l);
    return;
  }
  Clear((idx << 1), l, ((l + r) >> 1));
  Clear(((idx << 1) | 1), ((l + r) >> 1) + 1, r);
  Tree[idx] = max(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
void Update(int idx, int l, int r, int p, int v) {
  if (l == r) {
    Tree[idx].first += v;
    return;
  }
  if (p <= ((l + r) >> 1))
    Update((idx << 1), l, ((l + r) >> 1), p, v);
  else
    Update(((idx << 1) | 1), ((l + r) >> 1) + 1, r, p, v);
  Tree[idx] = max(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
bool flg;
int FindLeft(int idx, int l, int r, int s, int e, int v) {
  if (s > e) return 0;
  if (l == s && r == e) {
    if (Tree[idx].first >= v) return 0;
    if (l == r) {
      while (flg)
        ;
      return l;
    }
    if (Tree[((idx << 1) | 1)].first < v)
      return FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                      ((l + r) >> 1) + 1, e, v);
    if (Tree[(idx << 1)].first < v)
      return FindLeft((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    return 0;
  }
  if (e <= ((l + r) >> 1))
    return FindLeft((idx << 1), l, ((l + r) >> 1), s, e, v);
  else if (s > ((l + r) >> 1))
    return FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e, v);
  else {
    int q = FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                     ((l + r) >> 1) + 1, e, v);
    if (q == 0)
      q = FindLeft((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    return q;
  }
}
int FindRight(int idx, int l, int r, int s, int e, int v) {
  if (s > e) return 1000000007;
  if (l == s && r == e) {
    if (Tree[idx].first >= v) return 1000000007;
    if (l == r) return l;
    if (Tree[(idx << 1)].first < v)
      return FindRight((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    if (Tree[((idx << 1) | 1)].first < v)
      return FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                       ((l + r) >> 1) + 1, e, v);
    return 1000000007;
  }
  if (e <= ((l + r) >> 1))
    return FindRight((idx << 1), l, ((l + r) >> 1), s, e, v);
  else if (s > ((l + r) >> 1))
    return FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e, v);
  else {
    int q = FindRight((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    if (q == 1000000007)
      q = FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1,
                    e, v);
    return q;
  }
}
pair<int, int> QueryMX(int idx, int l, int r, int s, int e) {
  if (l == s && r == e) return Tree[idx];
  if (e <= ((l + r) >> 1))
    return QueryMX((idx << 1), l, ((l + r) >> 1), s, e);
  else if (s > ((l + r) >> 1))
    return QueryMX(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e);
  else {
    pair<int, int> q1 =
        QueryMX((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1));
    pair<int, int> q2 =
        QueryMX(((idx << 1) | 1), ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, e);
    return max(q1, q2);
  }
}
Query tq[N];
int countt[N];
void RadixSort2(int n, int m) {
  for (int i = 0; i <= m; i++) countt[i] = 0;
  for (int i = 1; i <= n; i++) countt[q[i].r]++;
  for (int i = 1; i <= m; i++) countt[i] += countt[i - 1];
  for (int i = n; i >= 1; i--) tq[countt[q[i].r]--] = q[i];
  for (int i = 0; i <= m; i++) countt[i] = 0;
  for (int i = 1; i <= n; i++) countt[tq[i].l / sqz]++;
  for (int i = 1; i <= m; i++) countt[i] += countt[i - 1];
  for (int i = n; i >= 1; i--) q[countt[tq[i].l / sqz]--] = tq[i];
}
int main() {
  scanf("%s", s);
  ss[0] = s;
  int(n);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    ss[i] = s;
  }
  int l = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (int)ss[i].length(); j++, l++) {
      s[l] = ss[i][j];
      id[l] = i;
    }
    s[l] = '#';
    l++;
  }
  s[l] = 0;
  SuffixArray(300);
  Build(1, 1, l);
  int(m);
  scanf("%d", &m);
  sqz = sqrt(l) + 3;
  for (int i = 1; i <= m; i++) {
    int l2, r2;
    scanf("%d%d", &l2, &r2);
    int l1, r1;
    scanf("%d%d", &l1, &r1);
    int len = r1 - l1 + 1;
    l1--;
    cntt = 0;
    int x = FindLeft(1, 1, l, 1, Rank[l1], len);
    x = max(x, 1);
    int y = FindRight(1, 1, l, Rank[l1] + 1, l, len) - 1;
    y = min(l, y);
    q[i] = Query(x, y, i, l2, r2);
  }
  RadixSort2(m, l + 3);
  Clear(1, 1, l);
  int ii = 1, jj = 0;
  for (int i = 1; i <= m; i++) {
    while (jj < q[i].r) {
      jj++;
      if (id[SA[jj]]) Update(1, 1, l, id[SA[jj]], 1);
    }
    while (jj > q[i].r) {
      if (id[SA[jj]]) Update(1, 1, l, id[SA[jj]], -1);
      jj--;
    }
    while (ii < q[i].l) {
      if (id[SA[ii]]) Update(1, 1, l, id[SA[ii]], -1);
      ii++;
    }
    while (ii > q[i].l) {
      ii--;
      if (id[SA[ii]]) Update(1, 1, l, id[SA[ii]], 1);
    }
    ans[q[i].id] = QueryMX(1, 1, l, q[i].ll, q[i].rr);
  }
  for (int i = 1; i <= m; i++) printf("%d %d\n", -ans[i].second, ans[i].first);
  return 0;
}

### 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>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
  return BigMod(b, m - 2, m);
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
T Dis(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T>
void ia(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
template <class T, class first>
inline T togglebit(T a, first i) {
  T t = 1;
  return (a ^ (t << i));
}
double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); }
double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); }
const int N = (int)6e5 + 7;
char s[600007];
string ss[600007];
int SA[600007], tmpSA[600007], Grp[600007], tmpGrp[600007], GrpSt[600007],
    Counting[600007], Rank[600007], LCP[600007];
void ComputeLCP(int L) {
  int k = 0;
  for (int i = 0; i < L; i++) {
    if (k) k--;
    int j = SA[Rank[i] - 1];
    while (i + k < L && j + k < L && s[i + k] == s[j + k]) k++;
    LCP[Rank[i]] = k;
  }
}
int SuffixArray(int mxAlpha) {
  int L = 0;
  for (int i = 0; i < mxAlpha; i++) Counting[i] = 0;
  for (L = 0; s[L]; L++) Counting[s[L]]++;
  for (int i = 1; i < mxAlpha; i++) Counting[i] += Counting[i - 1];
  for (int i = L - 1; i >= 0; i--)
    SA[Counting[s[i]]--] = i, Rank[i] = Counting[s[i]] + 1;
  int gNo = 0;
  GrpSt[0] = 0;
  tmpSA[0] = SA[0] = L;
  Grp[L] = 0;
  tmpGrp[L] = 0;
  for (int i = 1; i <= L; i++) {
    if (s[SA[i]] != s[SA[i - 1]]) gNo++, GrpSt[gNo] = i;
    Grp[SA[i]] = gNo;
  }
  int x = 0;
  for (int d = 1; d < L; d <<= 1) {
    x++;
    for (int i = L - d; i < L; i++) tmpSA[GrpSt[Grp[i]]++] = i;
    for (int i = 1; i <= L; i++)
      if (SA[i] >= d) tmpSA[GrpSt[Grp[SA[i] - d]]++] = SA[i] - d;
    gNo = 0;
    GrpSt[0] = 0;
    for (int i = 1; i <= L; i++) {
      if (Grp[tmpSA[i]] != Grp[tmpSA[i - 1]])
        gNo++, GrpSt[gNo] = i;
      else if (tmpSA[i] + d >= L)
        gNo++, GrpSt[gNo] = i;
      else if (tmpSA[i - 1] + d >= L)
        gNo++, GrpSt[gNo] = i;
      else if (Grp[tmpSA[i] + d] != Grp[tmpSA[i - 1] + d])
        gNo++, GrpSt[gNo] = i;
      SA[i] = tmpSA[i];
      Rank[SA[i]] = i;
      tmpGrp[SA[i]] = gNo;
    }
    if (gNo == L) break;
    for (int i = 0; i < L; i++) Grp[i] = tmpGrp[i];
  }
  ComputeLCP(L);
  return L;
}
int sqz;
struct Query {
  int l, r, id;
  int ll, rr;
  Query() {}
  Query(int _l, int _r, int _id, int _ll, int _rr) {
    l = _l;
    r = _r;
    id = _id;
    ll = _ll;
    rr = _rr;
  }
  bool operator<(const Query &p) const {
    if (l / sqz < p.l / sqz)
      return true;
    else if (l / sqz == p.l / sqz)
      return r < p.r;
  }
};
Query q[N];
pair<int, int> ans[N];
int id[N];
pair<int, int> Tree[4 * N];
int cntt;
void Build(int idx, int l, int r) {
  if (l == r) {
    Tree[idx] = make_pair(LCP[l], l);
    return;
  }
  Build((idx << 1), l, ((l + r) >> 1));
  Build(((idx << 1) | 1), ((l + r) >> 1) + 1, r);
  Tree[idx] = min(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
void Clear(int idx, int l, int r) {
  if (l == r) {
    Tree[idx] = make_pair(0, -l);
    return;
  }
  Clear((idx << 1), l, ((l + r) >> 1));
  Clear(((idx << 1) | 1), ((l + r) >> 1) + 1, r);
  Tree[idx] = max(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
void Update(int idx, int l, int r, int p, int v) {
  if (l == r) {
    Tree[idx].first += v;
    return;
  }
  if (p <= ((l + r) >> 1))
    Update((idx << 1), l, ((l + r) >> 1), p, v);
  else
    Update(((idx << 1) | 1), ((l + r) >> 1) + 1, r, p, v);
  Tree[idx] = max(Tree[(idx << 1)], Tree[((idx << 1) | 1)]);
}
bool flg;
int FindLeft(int idx, int l, int r, int s, int e, int v) {
  if (s > e) return 0;
  if (l == s && r == e) {
    if (Tree[idx].first >= v) return 0;
    if (l == r) {
      while (flg)
        ;
      return l;
    }
    if (Tree[((idx << 1) | 1)].first < v)
      return FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                      ((l + r) >> 1) + 1, e, v);
    if (Tree[(idx << 1)].first < v)
      return FindLeft((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    return 0;
  }
  if (e <= ((l + r) >> 1))
    return FindLeft((idx << 1), l, ((l + r) >> 1), s, e, v);
  else if (s > ((l + r) >> 1))
    return FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e, v);
  else {
    int q = FindLeft(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                     ((l + r) >> 1) + 1, e, v);
    if (q == 0)
      q = FindLeft((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    return q;
  }
}
int FindRight(int idx, int l, int r, int s, int e, int v) {
  if (s > e) return 1000000007;
  if (l == s && r == e) {
    if (Tree[idx].first >= v) return 1000000007;
    if (l == r) return l;
    if (Tree[(idx << 1)].first < v)
      return FindRight((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    if (Tree[((idx << 1) | 1)].first < v)
      return FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r,
                       ((l + r) >> 1) + 1, e, v);
    return 1000000007;
  }
  if (e <= ((l + r) >> 1))
    return FindRight((idx << 1), l, ((l + r) >> 1), s, e, v);
  else if (s > ((l + r) >> 1))
    return FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e, v);
  else {
    int q = FindRight((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1), v);
    if (q == 1000000007)
      q = FindRight(((idx << 1) | 1), ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1,
                    e, v);
    return q;
  }
}
pair<int, int> QueryMX(int idx, int l, int r, int s, int e) {
  if (l == s && r == e) return Tree[idx];
  if (e <= ((l + r) >> 1))
    return QueryMX((idx << 1), l, ((l + r) >> 1), s, e);
  else if (s > ((l + r) >> 1))
    return QueryMX(((idx << 1) | 1), ((l + r) >> 1) + 1, r, s, e);
  else {
    pair<int, int> q1 =
        QueryMX((idx << 1), l, ((l + r) >> 1), s, ((l + r) >> 1));
    pair<int, int> q2 =
        QueryMX(((idx << 1) | 1), ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, e);
    return max(q1, q2);
  }
}
Query tq[N];
int countt[N];
void RadixSort2(int n, int m) {
  for (int i = 0; i <= m; i++) countt[i] = 0;
  for (int i = 1; i <= n; i++) countt[q[i].r]++;
  for (int i = 1; i <= m; i++) countt[i] += countt[i - 1];
  for (int i = n; i >= 1; i--) tq[countt[q[i].r]--] = q[i];
  for (int i = 0; i <= m; i++) countt[i] = 0;
  for (int i = 1; i <= n; i++) countt[tq[i].l / sqz]++;
  for (int i = 1; i <= m; i++) countt[i] += countt[i - 1];
  for (int i = n; i >= 1; i--) q[countt[tq[i].l / sqz]--] = tq[i];
}
int main() {
  scanf("%s", s);
  ss[0] = s;
  int(n);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    ss[i] = s;
  }
  int l = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (int)ss[i].length(); j++, l++) {
      s[l] = ss[i][j];
      id[l] = i;
    }
    s[l] = '#';
    l++;
  }
  s[l] = 0;
  SuffixArray(300);
  Build(1, 1, l);
  int(m);
  scanf("%d", &m);
  sqz = sqrt(l) + 3;
  for (int i = 1; i <= m; i++) {
    int l2, r2;
    scanf("%d%d", &l2, &r2);
    int l1, r1;
    scanf("%d%d", &l1, &r1);
    int len = r1 - l1 + 1;
    l1--;
    cntt = 0;
    int x = FindLeft(1, 1, l, 1, Rank[l1], len);
    x = max(x, 1);
    int y = FindRight(1, 1, l, Rank[l1] + 1, l, len) - 1;
    y = min(l, y);
    q[i] = Query(x, y, i, l2, r2);
  }
  RadixSort2(m, l + 3);
  Clear(1, 1, l);
  int ii = 1, jj = 0;
  for (int i = 1; i <= m; i++) {
    while (jj < q[i].r) {
      jj++;
      if (id[SA[jj]]) Update(1, 1, l, id[SA[jj]], 1);
    }
    while (jj > q[i].r) {
      if (id[SA[jj]]) Update(1, 1, l, id[SA[jj]], -1);
      jj--;
    }
    while (ii < q[i].l) {
      if (id[SA[ii]]) Update(1, 1, l, id[SA[ii]], -1);
      ii++;
    }
    while (ii > q[i].l) {
      ii--;
      if (id[SA[ii]]) Update(1, 1, l, id[SA[ii]], 1);
    }
    ans[q[i].id] = QueryMX(1, 1, l, q[i].ll, q[i].rr);
  }
  for (int i = 1; i <= m; i++) printf("%d %d\n", -ans[i].second, ans[i].first);
  return 0;
}

```