#include <bits/stdc++.h>
using namespace std;
string s;
string t[50010];
string ist;
int H[600100], Rank[600100], sa[600100];
pair<pair<int, int>, int> pos[600100];
int g[600100][21];
int f[600100];
pair<int, int> qu[500100];
pair<pair<int, int>, int> ans[500100];
int ll[500100], rr[500100];
void buildSA(string s) {
  int n = s.size();
  for (int i = 0; i <= n; i++) Rank[i] = s[i];
  for (int j = 0; j < 19; j++) {
    for (int i = 0; i < n; i++)
      pos[i] = pair<pair<int, int>, int>(
          pair<int, int>(Rank[i], Rank[min(n, i + (1 << j))]), i);
    sort(pos, pos + n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      Rank[pos[i].second] = cnt + 1;
      cnt += (pos[i].first != pos[i + 1].first);
    }
    if (cnt == n) break;
  }
  for (int i = 0; i < n; i++) sa[Rank[i]] = i;
}
void Height(string s) {
  int h = 0, n = s.size();
  for (int i = 0; i < n; i++) {
    if (Rank[i] == 1) {
      H[Rank[i]] = 0;
      continue;
    }
    h = max(0, h - 1);
    for (int j = sa[Rank[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    H[Rank[i]] = h;
  }
}
int calc(int st, int ed) {
  int r = 31 - __builtin_clz(ed - st + 1);
  return min(g[st][r], g[ed - (1 << r) + 1][r]);
}
int N;
bool cmp(pair<pair<int, int>, int> u, pair<pair<int, int>, int> v) {
  if (u.first.first / N == v.first.first / N)
    return u.first.second < v.first.second;
  return u.first.first / N < v.first.first / N;
}
pair<int, int> val[200010];
pair<int, int> res[500100];
void push_up(int u) {
  pair<int, int> uu = max(val[(u << 1)], val[((u << 1) | 1)]);
  if (val[(u << 1)].first == val[((u << 1) | 1)].first)
    uu.second = min(val[(u << 1)].second, val[((u << 1) | 1)].second);
  val[u] = uu;
}
void update(int u, int L, int R, int pos, int first) {
  if (pos < L || pos >= R) return;
  if (L + 1 == R) {
    if (!val[u].first)
      val[u] = pair<int, int>(first, L);
    else
      val[u].first += first;
    return;
  }
  update((u << 1), L, (L + R >> 1), pos, first);
  update(((u << 1) | 1), (L + R >> 1), R, pos, first);
  push_up(u);
}
pair<int, int> As;
void calc(int u, int L, int R, int st, int ed) {
  if (st >= R || ed <= L) return;
  if (st <= L && ed >= R) {
    if (As.first == val[u].first)
      As.second = min(As.second, val[u].second);
    else
      As = max(As, val[u]);
    return;
  }
  calc((u << 1), L, (L + R >> 1), st, ed);
  calc(((u << 1) | 1), (L + R >> 1), R, st, ed);
}
int main() {
  cin >> s;
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) cin >> t[i];
  ist += s;
  for (int i = 1; i <= m; i++) {
    ist += '*';
    ist += t[i];
  }
  buildSA(ist);
  Height(ist);
  int sz = ist.size();
  int pt = s.size() + 1;
  for (int i = 1; i <= m; i++) {
    for (int j = pt; j < pt + t[i].size(); j++) f[Rank[j]] = i;
    pt += t[i].size() + 1;
  }
  for (int first = 1; first < 21; first++) {
    for (int i = 1; i <= sz; i++) {
      if (i + (1 << first) - 1 > sz) continue;
      if (first == 1)
        g[i][first] = H[i + 1];
      else
        g[i][first] =
            min(min(g[i][first - 1], g[i + (1 << (first - 1))][first - 1]),
                H[i + (1 << (first - 1))]);
    }
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int L, R;
    scanf("%d%d%d%d", &qu[i].first, &qu[i].second, &L, &R);
    int len = R - L + 1;
    int id = Rank[L - 1];
    int st, ed;
    st = 0, ed = id;
    while (st + 1 < ed) {
      int first = (st + ed) >> 1;
      if (calc(first, id) >= len)
        ed = first;
      else
        st = first;
    }
    ll[i] = ed;
    st = id, ed = sz + 1;
    while (st + 1 < ed) {
      int first = (st + ed) >> 1;
      if (calc(id, first) >= len)
        st = first;
      else
        ed = first;
    }
    rr[i] = st;
    ans[i] = pair<pair<int, int>, int>(pair<int, int>(ll[i], rr[i]), i);
  }
  N = sqrt(sz) + 100;
  sort(ans + 1, ans + Q + 1, cmp);
  int st = 1, ed = 0;
  for (int i = 1; i <= Q; i++) {
    int L = ans[i].first.first, R = ans[i].first.second, id = ans[i].second;
    while (ed < R) {
      ed++;
      update(1, 1, m + 1, f[ed], 1);
    }
    while (st > L) {
      st--;
      update(1, 1, m + 1, f[st], 1);
    }
    while (ed > R) {
      update(1, 1, m + 1, f[ed], -1);
      ed--;
    }
    while (st < L) {
      update(1, 1, m + 1, f[st], -1);
      st++;
    }
    As.first = 0, As.second = 0;
    calc(1, 1, m + 1, qu[id].first, qu[id].second + 1);
    res[id] = As;
    if (!res[id].first) res[id].second = qu[id].first;
  }
  for (int i = 1; i <= Q; i++) printf("%d %d\n", res[i].second, res[i].first);
  return 0;
}
