#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <class T, class U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
char s[600011];
int val[600011], sval[600011];
int ht[600011], mn[600011][23];
int sa[600011], rk[600011];
namespace SA {
int head[600011 * 2];
pair<pair<int, int>, int> pos[600011 * 2], buf[600011 << 1], tbuf[600011 << 1];
int nxt[600011 * 2];
void bucket_sort(int ok, int n) {
  int i, u, j, deg, E, K;
  for (i = E = K = 0; i < max(222, n + 10); i++) head[i] = -1;
  for (i = 0; i < n; i++) {
    if (ok)
      u = pos[i].first.first + 1;
    else
      u = pos[i].first.second + 1;
    buf[E] = pos[i];
    nxt[E] = head[u];
    head[u] = E++;
  }
  for (int i = 0; i < max(n + 10, 222); i++) {
    for (deg = 0, j = head[i]; ~j; j = nxt[j]) {
      tbuf[deg++] = buf[j];
    }
    while (deg) pos[K++] = tbuf[--deg];
  }
}
void LCP(int n) {
  for (int i = 1; i <= n; i++) mn[i][0] = ht[i];
  for (int j = 1; j < 23; j++) {
    for (int i = 1; i <= n; i++) {
      mn[i][j] = min(mn[i][j - 1], mn[min(n, i + (1 << j - 1))][j - 1]);
    }
  }
}
void height(int n) {
  int h = 0, i;
  for (i = 0; i < n; i++) sa[rk[i]] = i;
  for (i = 0; i < n; i++) {
    if (rk[i] == 1) continue;
    h = max(0, h - 1);
    for (int j = sa[rk[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    ht[rk[i]] = h;
  }
}
void buildSA(int n) {
  int i, j, cnt, u;
  for (i = 0; i < n; i++) rk[i] = s[i];
  rk[n] = 0;
  for (i = 0; i < 23; i++) {
    for (j = 0; j < n; j++)
      pos[j] = pair<pair<int, int>, int>(
          pair<int, int>(rk[j], rk[min(n, j + (1 << i))]), j);
    bucket_sort(0, n);
    bucket_sort(1, n);
    pos[n] = pair<pair<int, int>, int>(pair<int, int>(-1, -1), -1);
    for (cnt = j = 0; j < n; j++) {
      rk[pos[j].second] = cnt + 1;
      cnt += (pos[j].first != pos[j + 1].first);
    }
    if (cnt == n) break;
  }
  height(n);
  LCP(n);
}
int calc(int u, int v) {
  if (u == v) return 0x3f3f3f3f;
  if (u > v) swap(u, v);
  u++;
  int l = 31 - (__builtin_clz(v - u + 1));
  return min(mn[u][l], mn[v - (1 << l) + 1][l]);
}
}  // namespace SA
struct data {
  int l, r, L, R, id;
  data() : l(0), r(0), L(0), R(0), id(0) {}
  data(int l, int r, int L, int R, int id) : l(l), r(r), L(L), R(R), id(id) {}
  bool operator<(const data &u) const {
    int xL = (L + 333 - 1) / 333;
    int yL = (u.L + 333 - 1) / 333;
    if (xL == yL) return R < u.R;
    return xL < yL;
  }
  void print() { printf("%d %d %d %d %d\n", l, r, L, R, id); }
} query[600011];
struct res {
  int mx, pos;
  res() : mx(0), pos(0) {}
  res(int val, int pos) : mx(val), pos(pos) {}
  bool operator>(const res &u) const {
    if (mx == u.mx) return pos < u.pos;
    return mx > u.mx;
  }
  bool operator<(const res &u) const { return u > *this; }
  void print() { printf("%d %d\n", mx, pos); }
} ans[600011];
class RMQ_TREE {
 private:
  int n;
  res mem[600011 << 1];

 public:
  void build(int m) {
    n = m;
    for (int i = 0; i < n; i++) mem[i + n] = res(0, i + 1);
    for (int i = n - 1; i; i--) mem[i] = max(mem[i << 1], mem[i << 1 | 1]);
  }
  void update(int u, int add) {
    if (!u) return;
    mem[u += (n - 1)].mx += add;
    for (; u > 1; u >>= 1) {
      mem[u >> 1] = max(mem[u ^ 1], mem[u]);
    }
  }
  res calc(int st, int ed) {
    res ans = res(-1, -1);
    for (st += (n - 1), ed += (n - 1); ed >= st;
         ed = (ed - 1) >> 1, st = (st + 1) >> 1) {
      if (st & 1) smax(ans, mem[st]);
      if (ed & 1 ^ 1) smax(ans, mem[ed]);
    }
    return ans;
  }
} RT;
void update(int u, int add) { RT.update(sval[u], add); }
int main() {
  scanf("%s", s);
  int m;
  cin >> m;
  int pos = strlen(s);
  for (int i = 1; i <= m; i++) {
    scanf("%s", s + pos);
    for (int j = pos; s[j]; j++, pos++) val[j] = i;
    s[pos++] = '#';
  }
  SA::buildSA(pos);
  for (int i = 0; i < pos; i++) sval[rk[i]] = val[i];
  int q;
  cin >> q;
  int l, r, pl, pr, st, mid, ed, L, R;
  int now;
  for (int i = 0; i < q; i++) {
    gn(l);
    gn(r);
    gn(pl);
    gn(pr);
    pl--;
    pr -= pl;
    now = rk[pl];
    st = 0, ed = now;
    while (ed > st + 1) {
      mid = st + ed >> 1;
      if (SA::calc(mid, now) >= pr)
        ed = mid;
      else
        st = mid;
    }
    L = ed;
    st = now;
    ed = pos + 1;
    while (ed > st + 1) {
      mid = st + ed >> 1;
      if (SA::calc(now, mid) >= pr)
        st = mid;
      else
        ed = mid;
    }
    R = st;
    query[i] = data(l, r, L, R, i);
  }
  sort(query, query + q);
  RT.build(m);
  for (int i = 0; i < q; i++) {
    if (!i) st = query[i].L, ed = st - 1;
    while (st > query[i].L) update(--st, 1);
    while (st < query[i].L) update(st++, -1);
    while (ed > query[i].R) update(ed--, -1);
    while (ed < query[i].R) update(++ed, 1);
    ans[query[i].id] = RT.calc(query[i].l, query[i].r);
  }
  for (int i = 0; i < q; i++) {
    printf("%d %d\n", ans[i].pos, ans[i].mx);
  }
  return 0;
}
