#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
const int M = 1000010;
vector<int> e[M];
vector<int> ups[M];
struct qry {
  int id;
  int x, y;
  int tp, len;
};
bool cmp(const qry &a, const qry &b) {
  if (a.len == b.len) return a.tp > b.tp;
  return a.len > b.len;
}
vector<qry> qrs[M];
bool saved[M];
int fa[M], sz[M], wson[M];
struct SuffixAutomaton {
  int sz, lst;
  int ch[M][26];
  int len[M], link[M];
  int ed[M];
  void init() {
    create(0);
    lst = 0;
  }
  int create(int id = 0) {
    memset(ch[id], 0, sizeof(ch[0]));
    len[id] = link[id] = ed[id] = 0;
    return sz = id;
  }
  void extend(const char *s) {
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      int cur = create(sz + 1);
      len[cur] = len[lst] + 1;
      int p = lst;
      while (ch[p][c] == 0) {
        ch[p][c] = cur;
        p = link[p];
      }
      if (ch[p][c] != cur) {
        int q = ch[p][c];
        if (len[p] + 1 == len[q]) {
          link[cur] = q;
        } else {
          int clone = create(sz + 1);
          memcpy(ch[clone], ch[q], sizeof(ch[q]));
          link[clone] = link[q];
          len[clone] = len[p] + 1;
          while (ch[p][c] == q) {
            ch[p][c] = clone;
            p = link[p];
          }
          link[q] = link[cur] = clone;
        }
      }
      ed[i + 1] = lst = cur;
    }
  }
  void build() {
    for (int i = 1; i <= sz; ++i) e[link[i]].push_back(i);
  }
  void run(const char *s, int id) {
    int x = 0, now = 0;
    for (int i = 0; s[i]; ++i) {
      int c = s[i] - 'a';
      while (x && ch[x][c] == 0) {
        x = link[x];
        now = len[x];
      }
      if (ch[x][c]) {
        x = ch[x][c];
        ++now;
      }
      while (x && len[link[x]] >= now) x = link[x];
      if (now == len[x]) {
        ups[x].push_back(id);
      } else {
        ups[link[x]].push_back(id);
        qrs[x].push_back({id, 0, 0, 1, now});
      }
    }
  }
  int fuck(int x, int le) {
    x = ed[x];
    while (len[link[x]] >= le) x = link[x];
    return x;
  }
} SAM;
char s[500010];
char t[50010];
pair<int, int> g[200040];
pair<int, int> f[200040];
pair<int, int> ans[500010];
void pre(int);
void add(int);
void sub(int);
void dfs(int, bool);
void push_up(int, int, int);
void modify(int, int, int, int, int);
pair<int, int> query(int, int, int, int, int);
void push_up2(int, int, int);
void modify2(int, int, int, int, int);
pair<int, int> query2(int, int, int, int, int);
int main() {
  int n, m;
  int x, y, l, r;
  scanf("%s", s);
  SAM.extend(s);
  SAM.build();
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    SAM.run(t, i);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d%d", &x, &y, &l, &r);
    int pt = SAM.fuck(r, r - l + 1);
    qrs[pt].push_back({i, x, y, 0, r - l + 1});
  }
  for (int i = 0; i <= M; ++i) sort(qrs[i].begin(), qrs[i].end(), cmp);
  pre(0);
  dfs(0, true);
  for (int i = 1; i <= m; ++i) printf("%d %d\n", ans[i].second, ans[i].first);
  return 0;
}
void pre(int x) {
  sz[x] = 1;
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    fa[v] = x;
    pre(v);
    if (sz[v] > sz[wson[x]]) wson[x] = v;
    sz[x] += sz[v];
  }
}
void add(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, 1);
  for (auto v : e[x]) {
    if (v == fa[x] || saved[v]) continue;
    add(v);
  }
}
void sub(int x) {
  for (auto v : ups[x]) modify(1, 1, 50000, v, -1);
  for (auto v : e[x]) {
    if (v == fa[x]) continue;
    sub(v);
  }
}
void dfs(int x, bool keep) {
  for (auto v : e[x]) {
    if (v == fa[x] || v == wson[x]) continue;
    dfs(v, false);
  }
  if (wson[x]) {
    dfs(wson[x], true);
    saved[wson[x]] = true;
  }
  add(x);
  for (auto v : qrs[x]) {
    if (v.tp == 0) {
      pair<int, int> t1 = query(1, 1, 50000, v.x, v.y);
      pair<int, int> t2 = query2(1, 1, 50000, v.x, v.y);
      if (t1.first < t2.first) {
        t1 = t2;
      } else if (t1.first == t2.first) {
        t1.second = min(t1.second, t2.second);
      }
      if (t1.first == 0) t1.second = v.x;
      ans[v.id] = t1;
    } else {
      modify2(1, 1, 50000, v.id, 1);
    }
  }
  for (auto v : qrs[x]) {
    if (v.tp == 1) modify2(1, 1, 50000, v.id, -1);
  }
  if (wson[x]) saved[wson[x]] = false;
  if (!keep) sub(x);
}
void push_up(int cur, int lc, int rc) {
  if (f[lc].first >= f[rc].first) {
    f[cur] = f[lc];
  } else {
    f[cur] = f[rc];
  }
}
void modify(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    f[cur].first += k;
    f[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify(t, l, mid, x, k);
  else
    modify(t + 1, mid + 1, r, x, k);
  push_up(cur, t, t + 1);
}
pair<int, int> query(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return f[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}
void push_up2(int cur, int lc, int rc) {
  if (g[lc].first >= g[rc].first) {
    g[cur] = g[lc];
  } else {
    g[cur] = g[rc];
  }
}
void modify2(int cur, int l, int r, int x, int k) {
  int t, mid;
  if (x == l && x == r) {
    g[cur].first += k;
    g[cur].second = l;
    return;
  }
  t = cur << 1;
  mid = l + r >> 1;
  if (x <= mid)
    modify2(t, l, mid, x, k);
  else
    modify2(t + 1, mid + 1, r, x, k);
  push_up2(cur, t, t + 1);
}
pair<int, int> query2(int cur, int l, int r, int x, int y) {
  int t, mid;
  pair<int, int> retl, retr;
  if (x == l && y == r) return g[cur];
  t = cur << 1;
  mid = l + r >> 1;
  retl = retr = {0, 0};
  if (x <= mid) retl = query2(t, l, mid, x, min(mid, y));
  if (mid < y) retr = query2(t + 1, mid + 1, r, max(mid + 1, x), y);
  if (retl.first >= retr.first) return retl;
  return retr;
}
