#include <bits/stdc++.h>
const int maxn = 5e5 + 7, maxt = 6e5 + 3;
const unsigned hoff = 131;
int n, m, q, tl, blk, la[maxt], sa[maxt], rk[maxt], he[maxt], lg2[maxt],
    st[20][maxt], qod[maxt];
unsigned hs[maxt], hop[maxt];
char s[maxn], t[maxt];
struct qu_t {
  int l, r, ql, qr, u, v, w, ans, ansi;
  void read() { scanf("%d%d%d%d", &l, &r, &ql, &qr); }
  bool operator<(const qu_t &k) const {
    return w < k.w || (w == k.w && v < k.v);
  }
} qu[maxn];
inline bool cmpQu(int i, int j) { return qu[i] < qu[j]; }
inline bool match(int i, int j, int l) {
  --i;
  --j;
  if (std::max(i, j) + l >= tl) return false;
  return hs[i + l] - (i < 0 ? 0 : hs[i] * hop[l]) ==
         hs[j + l] - (j < 0 ? 0 : hs[j] * hop[l]);
}
inline int lcomm(int i, int j) {
  int l = 0, r = tl - std::max(i, j) + 1, mid;
  while (r - l > 1) {
    mid = (l + r) >> 1;
    if (match(i, j, mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}
inline bool cmpSuf(int i, int j) {
  int l = lcomm(i, j);
  return t[i + l] < t[j + l];
}
void readin() {
  scanf("%s%d", s, &m);
  n = strlen(s);
  char *cur = t;
  *(cur++) = '$';
  strcpy(cur, s);
  cur += n;
  *(cur++) = '$';
  for (int i = 1; i <= m; ++i) {
    scanf("%s", cur);
    for (; *cur; ++cur) la[cur - t] = i;
    *(cur++) = '$';
  }
  *cur = 0;
  tl = cur - t;
  blk = sqrt(tl);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) qu[i].read();
}
void geneHash() {
  hs[0] = t[0];
  for (int i = 1; i < tl; ++i) hs[i] = hs[i - 1] * hoff + t[i];
  hop[0] = 1;
  for (int i = 1; i < tl; ++i) hop[i] = hop[i - 1] * hoff;
}
void geneSA() {
  for (int i = 0; i < tl; ++i) sa[i] = i;
  std::sort(sa, sa + tl, cmpSuf);
  for (int i = 0; i < tl; ++i) rk[sa[i]] = i;
  for (int i = 1; i < tl; ++i) he[i] = lcomm(sa[i - 1], sa[i]);
}
void geneST() {
  for (int i = 1; i < 20; ++i)
    for (int j = 1 << (i - 1); j < (1 << i); ++j) lg2[j] = i - 1;
  for (int i = 0; i < tl; ++i) st[0][i] = he[i];
  for (int k = 1; k < 20; ++k)
    for (int i = 0; i + (1 << (k - 1)) < tl; ++i)
      st[k][i] = std::min(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
inline int lcp(int i, int j) {
  if (i == j) return he[i];
  int k = lg2[j - i + 1];
  return std::min(st[k][i], st[k][j - (1 << k) + 1]);
}
inline int divTlcp(int r, int len) {
  if (he[r] < len) return r;
  int l = -1, k = r, mid;
  while (r - l > 1) {
    mid = (l + r) >> 1;
    if (lcp(mid, k) >= len)
      r = mid;
    else
      l = mid;
  }
  return r - 1;
}
inline int divBlcp(int l, int len) {
  int r = tl, k = l, mid;
  while (r - l > 1) {
    mid = (l + r) >> 1;
    if (lcp(k + 1, mid) >= len)
      l = mid;
    else
      r = mid;
  }
  return l;
}
inline void maxSame(qu_t &p) {
  int i = rk[p.ql], len = p.qr - p.ql + 1;
  p.u = divTlcp(i, len);
  p.v = divBlcp(i, len);
  p.w = p.u / blk;
}
void geneQuery() {
  for (int i = 0; i < q; ++i) {
    maxSame(qu[i]);
    qod[i] = i;
  }
  std::sort(qod, qod + q, cmpQu);
}
const int lo = 0x100000;
struct node {
  int mx, mxi;
} seg[lo * 2];
void segBuild() {
  for (int i = 0; i < tl; ++i) seg[i + lo] = {0, i};
  for (int i = tl; i < lo; ++i) seg[i + lo] = {(int)0x80000000, -1};
  for (int i = lo - 1; i > 0; --i) {
    if (seg[((i << 1) | 1)].mx > seg[(i << 1)].mx)
      seg[i] = seg[((i << 1) | 1)];
    else
      seg[i] = seg[(i << 1)];
  }
}
void segModify(int k, int d) {
  seg[k += lo].mx += d;
  for (k >>= 1; k > 0; k >>= 1) {
    if (seg[((k << 1) | 1)].mx > seg[(k << 1)].mx)
      seg[k] = seg[((k << 1) | 1)];
    else
      seg[k] = seg[(k << 1)];
  }
}
void segQuery(int l, int r, qu_t &p) {
  int ans = -1, ansi = -1;
  for (l += lo, r += lo; l < r; l >>= 1, r >>= 1) {
    if (l & 1) {
      if (seg[l].mx > ans || seg[l].mx == ans && seg[l].mxi < ansi) {
        ans = seg[l].mx;
        ansi = seg[l].mxi;
      }
      ++l;
    }
    if (r & 1) {
      --r;
      if (seg[r].mx > ans || seg[r].mx == ans && seg[r].mxi < ansi) {
        ans = seg[r].mx;
        ansi = seg[r].mxi;
      }
    }
  }
  p.ans = ans;
  p.ansi = ansi;
}
int ml, mr;
void mdInit() {
  ml = 0;
  mr = 0;
  segModify(la[sa[0]], 1);
}
void mdTrans(int l, int r) {
  while (l < ml) {
    --ml;
    segModify(la[sa[ml]], 1);
  }
  while (r > mr) {
    ++mr;
    segModify(la[sa[mr]], 1);
  }
  while (ml < l) {
    segModify(la[sa[ml]], -1);
    ++ml;
  }
  while (mr > r) {
    segModify(la[sa[mr]], -1);
    --mr;
  }
}
void mdQuery(qu_t &p) { segQuery(p.l, p.r + 1, p); }
void mdBruteForce() {
  for (int i = 0; i < q; ++i) {
    mdTrans(qu[qod[i]].u, qu[qod[i]].v);
    mdQuery(qu[qod[i]]);
  }
}
int main() {
  readin();
  geneHash();
  geneSA();
  geneST();
  geneQuery();
  segBuild();
  mdInit();
  mdBruteForce();
  for (int i = 0; i < q; ++i) printf("%d %d\n", qu[i].ansi, qu[i].ans);
  return 0;
}
