Cosider a sequence, consisting of n integers: a1, a2, ..., an. Jeff can perform the following operation on sequence a:

  * take three integers v, t, k (1 ≤ v, t ≤ n; 0 ≤ k; v + tk ≤ n), such that av = av + t, av + t = av + 2t, ..., av + t(k - 1) = av + tk; 
  * remove elements av, av + t, ..., av + t·k from the sequence a, the remaining elements should be reindexed a1, a2, ..., an - k - 1. 
  * permute in some order the remaining elements of sequence a. 



A beauty of a sequence a is the minimum number of operations that is needed to delete all elements from sequence a.

Jeff's written down a sequence of m integers b1, b2, ..., bm. Now he wants to ask q questions. Each question can be described with two integers li, ri. The answer to the question is the beauty of sequence bli, bli + 1, ..., bri. You are given the sequence b and all questions. Help Jeff, answer all his questions.

Input

The first line contains integer m (1 ≤ m ≤ 105). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 105). 

The third line contains integer q (1 ≤ q ≤ 105) — the number of questions. The next q lines contain pairs of integers, i-th of them contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ m) — the description of i-th question.

Output

In q lines print the answers to Jeff's queries. Print the answers according to the order of questions in input.

Examples

Input

5
2 2 1 1 2
5
1 5
1 1
2 2
1 3
2 3


Output

2
1
1
2
2


Input

10
2 1 3 3 3 3 1 3 1 1
10
4 8
2 10
1 10
4 4
1 3
2 4
6 7
1 9
2 5
1 1


Output

2
3
3
1
3
2
2
3
2
1
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.Deque;
import java.util.ArrayList;
import java.util.Map;
import java.io.OutputStreamWriter;
import java.util.function.IntFunction;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Collection;
import java.io.IOException;
import java.util.stream.Collectors;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.stream.Stream;
import java.io.Closeable;
import java.io.Writer;
import java.util.Comparator;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            DJeffAndRemovingPeriods solver = new DJeffAndRemovingPeriods();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DJeffAndRemovingPeriods {
        int N = (int) 1e5;

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int m = in.ri();
            int[] b = in.ri(m);

            Map<Integer, List<Integer>> group = IntStream.range(0, m).boxed().collect(Collectors.groupingBy(i -> b[i]));
            int[] reg = new int[N + 1];
            Arrays.fill(reg, -1);
            long[] prev = new long[m];
            for (int i = 0; i < m; i++) {
                prev[i] = reg[b[i]];
                reg[b[i]] = i;
            }
            List<Rect> rects = new ArrayList<>(2 * m);
            for (List<Integer> list : group.values()) {
                int[] indices = list.stream().mapToInt(Integer::intValue).toArray();
                for (int i = 0; i < indices.length; i++) {
                    int first = i == 0 ? -1 : indices[i - 1];
                    if (i == indices.length - 1) {
                        rects.add(new Rect(first + 1, indices[i], indices[i], m - 1));
                        continue;
                    }
                    int l = i;
                    int r = i + 1;
                    int d = indices[r] - indices[l];
                    while (r + 1 < indices.length && indices[r + 1] - indices[r] == d) {
                        r++;
                    }
                    i = r - 1;
                    int last = r == indices.length - 1 ? m : indices[r + 1];
                    for (int j = l; j <= r; j++) {
                        rects.add(new Rect(first + 1, indices[j], indices[j], last - 1));
                    }
                }
            }


            WaveletTrees wt = new WaveletTrees(prev);
            int q = in.ri();
            Query[] qs = new Query[q];
            for (int i = 0; i < q; i++) {
                qs[i] = new Query();
                int l = in.ri() - 1;
                int r = in.ri() - 1;
                int ans = wt.range(l, r, -1, l - 1);
                qs[i].l = l;
                qs[i].r = r;
                qs[i].ans = ans;
            }

//        Query[] sortedByR = qs.clone();
//        Arrays.sort(sortedByR, Comparator.comparingInt(x -> x.r));
//        int offset = 2;
//        IntegerBIT bit = new IntegerBIT(N + offset);
//        for (int i = 0, head = 0; i < m; i++) {
//            bit.update(prev[i] + offset, 1);
//            while (head < sortedByR.length && sortedByR[head].r == i) {
//                sortedByR[head].ans += sortedByR[head].r - sortedByR[head].l + 1 -
//                        bit.query(sortedByR[head].l + offset, sortedByR[head].r + offset);
//                head++;
//            }
//        }
            Query[] sortedByL = qs.clone();
            Arrays.sort(sortedByL, Comparator.comparingInt(x -> x.l));
            Range2DequeAdapter<Query> dq = new Range2DequeAdapter<>(i -> sortedByL[i], 0, q - 1);
            int offset = 1;
            IntegerBITExt range = new IntegerBITExt(m);
            rects.sort(Comparator.comparingInt(x -> x.l));
            Deque<Rect> rectSortByLDq = new ArrayDeque<>(rects);
            rects.sort(Comparator.comparingInt(x -> x.r));
            Deque<Rect> rectSortByRDq = new ArrayDeque<>(rects);
            for (int i = 0; i < m; i++) {
                while (!rectSortByLDq.isEmpty() && rectSortByLDq.peekFirst().l <= i) {
                    Rect head = rectSortByLDq.removeFirst();
                    range.update(head.b + offset, head.t + offset, 1);
                }
                while (!rectSortByRDq.isEmpty() && rectSortByRDq.peekFirst().r < i) {
                    Rect head = rectSortByRDq.removeFirst();
                    range.update(head.b + offset, head.t + offset, -1);
                }
                while (!dq.isEmpty() && dq.peekFirst().l == i) {
                    Query head = dq.removeFirst();
                    if (range.query(head.r + offset, head.r + offset) == 0) {
                        head.ans++;
                    }
                }
            }

            for (Query query : qs) {
                out.println(query.ans);
            }
        }

    }

    static class BitPreSum {
        private long[] bits;
        private int[] ps;
        private int n;

        public BitPreSum(int n) {
            bits = new long[(n + 63) / 64];
            ps = new int[(n + 63) / 64];
            this.n = n;
        }

        public void set(int i) {
            bits[i >> 6] |= 1L << (i & 63);
            ps[i >> 6]++;
        }

        public void build() {
            for (int i = 1, to = (n + 63) / 64; i < to; i++) {
                ps[i] += ps[i - 1];
            }
        }

        public int total() {
            return ps[((n + 63) >> 6) - 1];
        }

        public int prefix(int m) {
            if (m > n) {
                m = n - 1;
            }
            if (m < 0) {
                return 0;
            }
            int head = m >> 6;
            int tail = m & 63;
            return (head == 0 ? 0 : ps[head - 1]) + Long.bitCount(bits[head] & Bits.headMask(tail + 1));
        }

        public String toString() {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(Bits.get(bits[i >> 6], i & 63));
            }
            return list.toString();
        }

    }

    static class Range2DequeAdapter<T> implements SimplifiedDeque<T> {
        IntFunction<T> data;
        int l;
        int r;

        public Range2DequeAdapter(IntFunction<T> data, int l, int r) {
            this.data = data;
            this.l = l;
            this.r = r;
        }

        public boolean isEmpty() {
            return l > r;
        }

        public T peekFirst() {
            return data.apply(l);
        }

        public T removeFirst() {
            return data.apply(l++);
        }

        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int iter = l;


                public boolean hasNext() {
                    return iter <= r;
                }


                public T next() {
                    return data.apply(iter++);
                }
            };
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        public void populate(int[] data) {
            for (int i = 0; i < data.length; i++) {
                data[i] = readInt();
            }
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int ri() {
            return readInt();
        }

        public int[] ri(int n) {
            int[] ans = new int[n];
            populate(ans);
            return ans;
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private static final int THRESHOLD = 32 << 10;
        private final Writer os;
        private StringBuilder cache = new StringBuilder(THRESHOLD * 2);

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        private void afterWrite() {
            if (cache.length() < THRESHOLD) {
                return;
            }
            flush();
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput println(int c) {
            return append(c).println();
        }

        public FastOutput println() {
            return append('\n');
        }

        public FastOutput flush() {
            try {
//            boolean success = false;
//            if (stringBuilderValueField != null) {
//                try {
//                    char[] value = (char[]) stringBuilderValueField.get(cache);
//                    os.write(value, 0, cache.length());
//                    success = true;
//                } catch (Exception e) {
//                }
//            }
//            if (!success) {
                os.append(cache);
//            }
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class DigitUtils {
        private DigitUtils() {
        }

        public static long floorAverage(long x, long y) {
            return (x & y) + ((x ^ y) >> 1);
        }

    }

    static interface SimplifiedStack<T> extends Iterable<T> {
    }

    static class Rect {
        int l;
        int r;
        int b;
        int t;

        public Rect(int l, int r, int b, int t) {
            this.l = l;
            this.r = r;
            this.b = b;
            this.t = t;
        }

    }

    static class IntegerBITExt {
        int[] delta;
        int[] idelta;
        int n;

        public IntegerBITExt(int n) {
            this.n = n;
            delta = new int[n + 1];
            idelta = new int[n + 1];
        }

        private void update(int i, int x) {
            int x1 = x;
            int x2 = x * i;
            for (; i <= n; i += i & -i) {
                delta[i] += x1;
                idelta[i] += x2;
            }
        }

        public void update(int l, int r, int x) {
            update(l, x);
            update(r + 1, -x);
        }

        public int query(int x) {
            int ans1 = 0;
            int ans2 = 0;
            for (int i = x; i > 0; i -= (i & -i)) {
                ans1 += delta[i];
                ans2 += idelta[i];
            }
            return (x + 1) * ans1 - ans2;
        }

        public int query(int l, int r) {
            return query(r) - query(l - 1);
        }

        public String toString() {
            StringBuilder ans = new StringBuilder("[");
            for (int i = 1; i <= n; i++) {
                ans.append(query(i, i)).append(',');
            }
            if (ans.length() > 1) {
                ans.setLength(ans.length() - 1);
            }
            ans.append("]");
            return ans.toString();
        }

    }

    static class Bits {
        private Bits() {
        }

        public static int get(long x, int i) {
            return (int) ((x >>> i) & 1);
        }

        public static long headMask(int n) {
            if (n == 0) {
                return 0;
            }
            return -1L >>> (64 - n);
        }

    }

    static class WaveletTrees {
        private WaveletTrees.Node root;
        private long L;
        private long R;
        int[] indices;

        public WaveletTrees(long[] data) {
            indices = IntStream.range(0, data.length).toArray();
            L = Long.MAX_VALUE;
            R = Long.MIN_VALUE;
            for (long x : data) {
                L = Math.min(x, L);
                R = Math.max(x, R);
            }
            root = new WaveletTrees.Node(data, indices, new int[data.length], 0, data.length - 1, L, R);
        }

        public int leq(int l, int r, long k) {
            if (k < L) {
                return 0;
            }
            if (k > R) {
                k = R;
            }
            return root.leq(0, indices.length - 1, l, r, L, R, k);
        }

        public int range(int l, int r, int L, int R) {
            return leq(l, r, R) - leq(l, r, L - 1);
        }

        private static class Node {
            BitPreSum ps;
            WaveletTrees.Node left;
            WaveletTrees.Node right;

            public int leq(int l, int r, int rangeL, int rangeR, long L, long R, long key) {
                if (ps == null) {
                    return rangeR - rangeL + 1;
                }
                assert l <= r;
                assert rangeL >= 0 && rangeR <= r - l;
                int m = l + ps.total() - 1;
                long M = DigitUtils.floorAverage(L, R);
                int pr = ps.prefix(rangeR);
                int pl = ps.prefix(rangeL - 1);
                if (M >= key) {
                    return left.leq(l, m, pl, pr - 1, L, M, key);
                }
                int inLeft = pr;
                if (rangeL > 0) {
                    inLeft -= pl;
                }
                return inLeft + right.leq(m + 1, r, rangeL == 0 ? 0 : rangeL - pl, rangeR - pr, M + 1, R, key);
            }

            public Node(long[] data, int[] indices, int[] buf, int l, int r, long L, long R) {
                if (L == R || l > r) {
                    //stop here
                    return;
                }
                long M = DigitUtils.floorAverage(L, R);
                ps = new BitPreSum(r - l + 1);
                int indicesTail = l;
                int bufTail = 0;
                for (int i = l; i <= r; i++) {
                    if (data[indices[i]] <= M) {
                        indices[indicesTail++] = indices[i];
                        ps.set(i - l);
                    } else {
                        buf[bufTail++] = indices[i];
                    }
                }
                System.arraycopy(buf, 0, indices, indicesTail, bufTail);
                ps.build();

                left = new WaveletTrees.Node(data, indices, buf, l, indicesTail - 1, L, M);
                right = new WaveletTrees.Node(data, indices, buf, indicesTail, r, M + 1, R);
            }

        }

    }

    static class Query {
        int l;
        int r;
        int ans;

    }

    static interface SimplifiedDeque<T> extends SimplifiedStack<T> {
    }
}

