inline ll __read() { ll x(0), t(1); charo(getchar()); while (o < '0' || o > '9') { if (o == '-') t = -1; o = getchar(); } for (; o >= '0' && o <= '9'; o = getchar()) x = (x << 1) + (x << 3) + (o ^ 48); return x * t; }
ll n, m, d, q; ll a[maxn], ys[maxn]; ll f[maxn][22], log_[maxn] = {-1}; int Ch[maxn][15], end[maxn], id; int stk[maxn], top, cnt; bool t[maxn];
inlinevoidcf(ll x) { top = 0; while (x) { stk[++top] = x % d; x /= d; } }
inlinevoidInsert(ll x) { cf(x); introot(0); for (int i = top; i; --i) { int now = stk[i]; if (!Ch[root][now]) Ch[root][now] = ++id; root = Ch[root][now]; if (end[root]) return; } end[root] = ++cnt; }
inlineintQuery(ll x) { cf(x); introot(0); for (int i = top; i; --i) { int now = stk[i]; if (!Ch[root][now]) break; root = Ch[root][now]; if (end[root]) return end[root]; } return-1; }
signedmain() { n = __read(), m = __read(), d = __read(), q = __read(); for (int i = 1; i <= n; ++i) a[i] = __read(); for (int i = 1; i <= m; ++i) ys[i] = __read(); for (int i = 1; i <= m; ++i) Insert(ys[i]); for (int i = 1; i <= n; ++i) { int k = Query(a[i]); log_[i] = log_[i >> 1] + 1; if (~k) f[i][0] = (1ll << k); } for (int j = 1; j <= log_[n]; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) f[i][j] = f[i][j - 1] | f[i + (1 << j - 1)][j - 1]; while (q--) { int l = __read(), r = __read(); int len = log_[r - l + 1]; ll ans = f[l][len] | f[r - (1 << len) + 1][len]; intcnt(0); while (ans) { if (ans & 1) ++cnt; ans >>= 1; } printf ("%d\n", cnt); } system("pause"); }