#include <bits/stdc++.h>
using namespace std;
namespace SGT {
int ls[4000005], rs[4000005], tot;
pair<int, int> mx[4000005];
void update(int k) {
  if (!ls[k] || !rs[k])
    mx[k] = mx[ls[k] | rs[k]];
  else
    mx[k] = max(mx[ls[k]], mx[rs[k]]);
}
void add(int &k, int l, int r, int p) {
  if (!k) k = ++tot;
  if (l == r) {
    mx[k].first++, mx[k].second = -l;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    add(ls[k], l, mid, p);
  else
    add(rs[k], mid + 1, r, p);
  update(k);
}
int merge(int x, int y, int l, int r) {
  if (!x || !y) return x | y;
  int k = ++tot, mid = (l + r) >> 1;
  if (l == r)
    mx[k].first = mx[x].first + mx[y].first, mx[k].second = -l;
  else
    ls[k] = merge(ls[x], ls[y], l, mid),
    rs[k] = merge(rs[x], rs[y], mid + 1, r), update(k);
  return k;
}
pair<int, int> qry(int k, int l, int r, int x, int y) {
  if (!k) return pair<int, int>(0, -x);
  if (x <= l && r <= y) return mx[k];
  int mid = (l + r) >> 1;
  if (y <= mid) return qry(ls[k], l, mid, x, y);
  if (x > mid) return qry(rs[k], mid + 1, r, x, y);
  return max(qry(ls[k], l, mid, x, y), qry(rs[k], mid + 1, r, x, y));
}
}  // namespace SGT
int rt[100005];
char str[50005], s[500005];
int ed[500005], lcs[500005];
int tot = 1, len[100005], fat[100005], son[100005][26], n, m;
inline int insert(int lst, int x, vector<int> id) {
  int p = lst, q, k, u = ++tot;
  len[u] = len[p] + 1;
  for (int asd : id) SGT::add(rt[u], 1, n, asd);
  while (p && !son[p][x]) son[p][x] = u, p = fat[p];
  if (!p) {
    fat[u] = 1;
    return u;
  }
  if (len[p] + 1 == len[q = son[p][x]]) {
    fat[u] = q;
    return u;
  }
  len[k = ++tot] = len[p] + 1;
  fat[k] = fat[q];
  fat[u] = fat[q] = k;
  memcpy(son[k], son[q], sizeof(son[q]));
  while (p && son[p][x] == q) son[p][x] = k, p = fat[p];
  return u;
}
vector<int> e[100005];
int ch[50005][26], lst[50005], sz = 1;
vector<int> id[50005];
void ins(char *s, int T) {
  for (int i = 0, u = 1, c; s[i]; u = ch[u][c], id[u].push_back(T), i++)
    if (!ch[u][c = s[i] - 'a']) ch[u][c] = ++sz;
}
void dfs(int u) {
  for (int i = 0; i < 26; i++)
    if (ch[u][i])
      lst[ch[u][i]] = insert(lst[u], i, id[ch[u][i]]), dfs(ch[u][i]);
}
int fa[100005][17];
void DFS(int u) {
  fa[u][0] = fat[u];
  for (int i = 1; i < 17; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int v : e[u]) DFS(v), rt[u] = SGT::merge(rt[u], rt[v], 1, n);
}
int main() {
  scanf("%s%d", s + 1, &n);
  m = strlen(s + 1);
  for (int i = 1; i <= n; i++) scanf("%s", str), ins(str, i);
  lst[1] = 1;
  dfs(1);
  for (int i = 2; i <= tot; i++) e[fat[i]].emplace_back(i);
  DFS(1);
  for (int i = 1, u = 1, le = 0; i <= m; i++) {
    while (u && !son[u][s[i] - 'a']) u = fat[u], le = len[u];
    if (!u)
      u = 1, le = 0;
    else
      u = son[u][s[i] - 'a'], le++;
    lcs[i] = le;
    ed[i] = u;
  }
  scanf("%d", &m);
  for (int ql, qr, l, r; m--;) {
    scanf("%d%d%d%d", &l, &r, &ql, &qr);
    int le = qr - ql + 1, u = ed[qr];
    if (lcs[qr] < le) {
      printf("%d 0\n", l);
      continue;
    }
    for (int i = 16; ~i; i--)
      if (len[fa[u][i]] >= le) u = fa[u][i];
    pair<int, int> res = SGT::qry(rt[u], 1, n, l, r);
    printf("%d %d\n", -res.second, res.first);
  }
}
