import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const N = readInt(); auto H = new long[N]; foreach (k; 0 .. N) { H[k] = readLong(); } auto A = new int[N]; auto B = new int[N]; auto C = new int[N]; auto D = new int[N]; auto E = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); B[i] = readInt(); C[i] = readInt(); D[i] = readInt(); E[i] = readInt() - 1; } auto Q = readInt(); auto X = new int[Q]; auto Y = new int[Q]; foreach (q; 0 .. Q) { X[q] = readInt(); Y[q] = readInt(); } alias Entry = Tuple!(int, "pos", int, "id"); auto xs = new Entry[N + N]; auto ys = new Entry[N + N]; foreach (i; 0 .. N) { xs[i] = Entry(A[i], i); ys[i] = Entry(B[i], i); xs[N + i] = Entry(C[i], N + i); ys[N + i] = Entry(D[i], N + i); } xs.sort; ys.sort; auto as = new int[N]; auto bs = new int[N]; auto cs = new int[N]; auto ds = new int[N]; foreach (e; 0 .. N + N) { if (xs[e].id < N) { as[xs[e].id] = e; } else { cs[xs[e].id - N] = e; } } foreach (f; 0 .. N + N) { if (ys[f].id < N) { bs[ys[f].id] = f; } else { ds[ys[f].id - N] = f; } } debug { writeln("xs = ", xs); writeln("ys = ", ys); writeln("as = ", as); writeln("bs = ", bs); writeln("cs = ", cs); writeln("ds = ", ds); } // const L = cast(int)(sqrt(N + N)); const L = 2; alias Query = Tuple!(int, "e", int, "f", int, "id"); auto queries = new Query[Q]; foreach (q; 0 .. Q) { const e = xs.upperBound(Entry(X[q], N + N)); const f = ys.upperBound(Entry(Y[q], N + N)); queries[q] = Query(e, f, q); } queries.sort!((a, b) => ((a.e / L != b.e / L) ? (a.e / L < b.e / L) : (a.e / L % 2 == 0) ? (a.f < b.f) : (a.f > b.f))); auto cnt = new int[N]; long now; void add(int i) { if (cnt[E[i]]++ == 0) { now ^= H[E[i]]; } } void rem(int i) { if (--cnt[E[i]] == 0) { now ^= H[E[i]]; } } auto ans = new long[Q]; { int e, f; foreach (qry; queries) { /* | | f-------+-- | (e,f) | | e */ for (; e < qry.e; ++e) { if (xs[e].id < N) { const i = xs[e].id; if (bs[i] < f && f <= ds[i]) add(i); } else { const i = xs[e].id - N; if (bs[i] < f && f <= ds[i]) rem(i); } debug { writeln("++e: ", cnt); } } for (; e > qry.e; --e) { if (xs[e - 1].id < N) { const i = xs[e - 1].id; if (bs[i] < f && f <= ds[i]) rem(i); } else { const i = xs[e - 1].id - N; if (bs[i] < f && f <= ds[i]) add(i); } debug { writeln("--e: ", cnt); } } for (; f < qry.f; ++f) { if (ys[f].id < N) { const i = ys[f].id; if (as[i] < e && e <= cs[i]) add(i); } else { const i = ys[f].id - N; if (as[i] < e && e <= cs[i]) rem(i); } debug { writeln("++f: ", cnt); } } for (; f > qry.f; --f) { if (ys[f - 1].id < N) { const i = ys[f - 1].id; if (as[i] < e && e <= cs[i]) rem(i); } else { const i = ys[f - 1].id - N; if (as[i] < e && e <= cs[i]) add(i); } debug { writeln("--f: ", cnt); } } debug { writeln("qry = ", qry); } ans[qry.id] = now; } } foreach (q; 0 .. Q) { writeln(ans[q]); } } } catch (EOFException e) { } }