#include <bits/stdc++.h>
using namespace std;
struct ask {
  int id, l, r;
  ask(int Id = 0, int L = 0, int R = 0) { id = Id, l = L, r = R; }
};
struct node {
  int x, y;
  node(int xx = 0, int yy = 0) { x = xx, y = yy; }
  bool operator<(const node &A) const {
    return (x == A.x) ? (y > A.y) : (x < A.x);
  }
} t2[50005 << 5], ans[500005];
vector<ask> e[50005 << 1];
int m, q, lst = 1, tot = 1;
int l, r, pl, pr;
int v[500005], w[500005];
int f[50005 << 1][21];
int tr[50005 << 1][26];
int cnt = 0, t[50005 << 5], ls[50005 << 5], rs[50005 << 5], tt[50005 << 1];
int pre[50005 << 1], len[50005 << 1];
string s, T[50005];
struct edge {
  int nxt, v;
  edge(int Nxt = 0, int V = 0) { nxt = Nxt, v = V; }
} E[50005 << 1];
int ct, fr[50005 << 1];
void add(int x, int y) {
  ++ct;
  E[ct] = edge(fr[x], y), fr[x] = ct;
}
void update(int p) { t2[p] = max(t2[ls[p]], t2[rs[p]]); }
void modify(int &p, int l, int r, int x) {
  if (!p) p = ++cnt;
  if (l == r) {
    ++t[p];
    t2[p] = node(t[p], l);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    modify(ls[p], l, mid, x);
  else
    modify(rs[p], mid + 1, r, x);
  update(p);
}
int conbine(int x, int y, int l, int r) {
  if (!x || !y) return x | y;
  if (l == r) {
    t[x] += t[y];
    t2[x] = node(t[x], l);
    return x;
  }
  int mid = (l + r) >> 1;
  ls[x] = conbine(ls[x], ls[y], l, mid);
  rs[x] = conbine(rs[x], rs[y], mid + 1, r);
  update(x);
  return x;
}
node calc(int p, int l, int r, int x, int y) {
  if (!p) return t2[0];
  if (l == x && r == y) return t2[p];
  int mid = (l + r) >> 1;
  if (y <= mid)
    return calc(ls[p], l, mid, x, y);
  else if (x > mid)
    return calc(rs[p], mid + 1, r, x, y);
  else
    return max(calc(ls[p], l, mid, x, mid),
               calc(rs[p], mid + 1, r, mid + 1, y));
}
void ic(int c) {
  int np, p, q, g;
  if (tr[lst][c]) {
    int p = lst, q = tr[lst][c];
    if (len[p] + 1 == len[q])
      lst = q;
    else {
      g = ++tot;
      memcpy(tr[g], tr[q], sizeof(tr[q]));
      len[g] = len[p] + 1, pre[g] = pre[q];
      for (; p && tr[p][c] == q; p = pre[p]) tr[p][c] = g;
      pre[q] = g;
      lst = g;
    }
    return;
  }
  np = ++tot;
  p = lst;
  len[np] = len[p] + 1;
  for (; p && !tr[p][c]; p = pre[p]) tr[p][c] = np;
  if (!p)
    pre[np] = 1;
  else {
    q = tr[p][c];
    if (len[p] + 1 == len[q])
      pre[np] = q;
    else {
      g = ++tot;
      memcpy(tr[g], tr[q], sizeof(tr[q]));
      len[g] = len[p] + 1, pre[g] = pre[q];
      for (; p && tr[p][c] == q; p = pre[p]) tr[p][c] = g;
      pre[np] = pre[q] = g;
    }
  }
  lst = np;
}
void ins(string &s) {
  lst = 1;
  int n = s.length();
  for (int i = 0; i < n; ++i) ic(s[i] - 'a');
}
void solve(int u) {
  for (int i = fr[u]; i; i = E[i].nxt) {
    int v = E[i].v;
    solve(v);
    tt[u] = conbine(tt[u], tt[v], 1, m);
  }
  for (vector<ask>::iterator it = e[u].begin(); it != e[u].end(); ++it) {
    ans[it->id] = calc(tt[u], 1, m, it->l, it->r);
    if (ans[it->id].x == -1000000007) ans[it->id] = node(0, it->l);
  }
}
int main() {
  cin >> s;
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) cin >> T[i], ins(T[i]);
  int n = s.length();
  int p = 1, nl = 0;
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (tr[p][c])
      p = tr[p][c], ++nl;
    else {
      while (p && !tr[p][c]) p = pre[p], nl = len[p];
      if (p) p = tr[p][c], ++nl;
    }
    v[i] = p, w[i] = nl;
    if (!p) p = 1, nl = 0;
  }
  for (int i = 2; i <= tot; ++i) f[i][0] = pre[i], add(pre[i], i);
  for (int j = 1; j <= 18; ++j)
    for (int i = 1; i <= tot; ++i) f[i][j] = f[f[i][j - 1]][j - 1];
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d%d%d", &pl, &pr, &l, &r);
    --l, --r;
    int p = v[r], tl = r - l + 1;
    if (!p || w[r] < r - l + 1)
      ans[i] = node(0, pl);
    else {
      for (int j = 18; j >= 0; --j)
        if (f[p][j] && len[f[p][j]] >= tl) p = f[p][j];
      e[p].push_back(ask(i, pl, pr));
    }
  }
  t2[0] = node(-1000000007, 1000000007);
  for (int i = 1; i <= m; ++i) {
    int p = 1, n = T[i].length();
    for (int j = 0; j < n; ++j)
      p = tr[p][T[i][j] - 'a'], modify(tt[p], 1, m, i);
  }
  solve(1);
  for (int i = 1; i <= q; ++i) printf("%d %d\n", ans[i].y, ans[i].x);
  return 0;
}
