#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, y, z, L;
int l, r, pl, pr;
char s[500010], t[500010];
int fa[22][2000010];
int P[2000010];
int getint() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
pair<int, int> ret;
struct segNode {
  pair<int, int> key;
  segNode *lc, *rc;
  segNode() : lc(0), rc(0) { key.first = key.second = 0; }
  void maintain() {
    if (!this) return;
    key.first = key.second = 0;
    if (lc)
      if (lc->key.first > key.first)
        key.first = lc->key.first, key.second = lc->key.second;
      else if (lc->key.first == key.first)
        key.second = min(key.second, lc->key.second);
    if (rc)
      if (rc->key.first > key.first)
        key.first = rc->key.first, key.second = rc->key.second;
      else if (rc->key.first == key.first)
        key.second = min(key.second, rc->key.second);
  }
};
void Insert(segNode *&o, int tar, int l, int r) {
  if (!o) o = new segNode();
  if (l == r) {
    o->key.first = 1, o->key.second = tar;
    return;
  }
  int mid = (l + r) >> 1;
  if (tar <= mid)
    Insert(o->lc, tar, l, mid);
  else
    Insert(o->rc, tar, mid + 1, r);
  o->maintain();
}
int Nodecnt;
struct Node {
  Node *par, *ch[27];
  segNode *R;
  int val, num;
  Node(int _val) : R(0), par(0), val(_val), num(++Nodecnt) {
    memset(ch, 0, sizeof ch);
  }
} * root, *last, *pos[500010], *Ta[2000010], *tar;
void extend(int w) {
  Node *p = last, *np = new Node(p->val + 1);
  Ta[Nodecnt] = np, P[Nodecnt] = Nodecnt;
  while (p && !p->ch[w]) p->ch[w] = np, p = p->par;
  if (!p)
    np->par = root;
  else {
    Node *q = p->ch[w];
    if (p->val + 1 == q->val)
      np->par = q;
    else {
      Node *nq = new Node(0);
      Ta[Nodecnt] = nq, P[Nodecnt] = Nodecnt;
      memcpy(nq->ch, q->ch, sizeof q->ch);
      nq->val = p->val + 1, nq->par = q->par;
      q->par = nq, np->par = nq;
      while (p && p->ch[w] == q) p->ch[w] = nq, p = p->par;
    }
  }
  last = np;
}
Node *locate(int l, int r) {
  int p = pos[r]->num;
  for (int i = 20; i >= 0; i--)
    if (Ta[fa[i][p]])
      if (Ta[fa[i][p]]->val >= r - l + 1) p = fa[i][p];
  return Ta[p];
}
bool cmp(int i, int j) { return Ta[i]->val < Ta[j]->val; }
segNode *Merge(segNode *p, segNode *q, int l, int r) {
  if (!p)
    return q;
  else if (!q)
    return p;
  segNode *o = new segNode();
  if (l == r) {
    o->key.first = p->key.first + q->key.first;
    o->key.second = l;
    return o;
  }
  int mid = (l + r) >> 1;
  o->lc = Merge(p->lc, q->lc, l, mid);
  o->rc = Merge(p->rc, q->rc, mid + 1, r);
  o->maintain();
  return o;
}
pair<int, int> Query(segNode *o, int L, int R, int l, int r) {
  if (!o) return make_pair(0, 0);
  if (L <= l && R >= r) return o->key;
  int mid = (l + r) >> 1;
  if (R <= mid)
    return Query(o->lc, L, R, l, mid);
  else if (L > mid)
    return Query(o->rc, L, R, mid + 1, r);
  else {
    pair<int, int> key = make_pair(0, 0), k1, k2;
    k1 = Query(o->lc, L, mid, l, mid);
    k2 = Query(o->rc, mid + 1, R, mid + 1, r);
    if (k1.first < k2.first) swap(k1, k2);
    key.first = k1.first;
    if (k1.first == k2.first)
      key.second = min(k1.second, k2.second);
    else
      key.second = k1.second;
    return key;
  }
}
int main() {
  root = last = new Node(0);
  Ta[Nodecnt] = root;
  P[Nodecnt] = Nodecnt;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (i = 1; i <= n; i++) extend(s[i] - 'a'), pos[i] = last;
  extend(26);
  m = getint();
  for (i = 1; i <= m; i++) {
    scanf("%s", t + 1);
    L = strlen(t + 1);
    for (j = 1; j <= L; j++) extend(t[j] - 'a'), Insert(last->R, i, 1, m);
    extend(26);
  }
  for (i = 1; i <= Nodecnt; i++)
    if (Ta[i]->par) fa[0][i] = Ta[i]->par->num;
  for (j = 1; j <= 20; j++)
    for (i = 1; i <= Nodecnt; i++) fa[j][i] = fa[j - 1][fa[j - 1][i]];
  sort(P + 1, P + Nodecnt + 1, cmp);
  for (i = Nodecnt; i >= 1; i--)
    if (Ta[P[i]]->par)
      Ta[P[i]]->par->R = Merge(Ta[P[i]]->par->R, Ta[P[i]]->R, 1, m);
  k = getint();
  for (i = 1; i <= k; i++) {
    l = getint(), r = getint(), pl = getint(), pr = getint();
    tar = locate(pl, pr);
    ret = Query(tar->R, l, r, 1, m);
    if (!ret.first) ret.second = l;
    printf("%d %d\n", ret.second, ret.first);
  }
  return 0;
}
