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.Random;
import java.util.ArrayList;
import java.util.Map;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
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.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<RectCoverProblem.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 RectCoverProblem.Rect(first + 1, indices[i], indices[i], m - 1, 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 RectCoverProblem.Rect(first + 1, indices[j], indices[j], last - 1, 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].x = l;
                qs[i].y = r;
                qs[i].ans = ans;
            }

            long[] res = RectCoverProblem.solve(rects.toArray(new RectCoverProblem.Rect[0]), qs);
            for (int i = 0; i < q; i++) {
                if (res[i] == 0) {
                    qs[i].ans++;
                }
            }

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

    }

    static class SortUtils {
        private static final int THRESHOLD = 8;

        private SortUtils() {
        }

        public static void insertSort(int[] data, IntegerComparator cmp, int l, int r) {
            for (int i = l + 1; i <= r; i++) {
                int j = i;
                int val = data[i];
                while (j > l && cmp.compare(data[j - 1], val) > 0) {
                    data[j] = data[j - 1];
                    j--;
                }
                data[j] = val;
            }
        }

        public static void quickSort(int[] data, IntegerComparator cmp, int f, int t) {
            if (t - f <= THRESHOLD) {
                insertSort(data, cmp, f, t - 1);
                return;
            }
            SequenceUtils.swap(data, f, RandomWrapper.INSTANCE.nextInt(f, t - 1));
            int l = f;
            int r = t;
            int m = l + 1;
            while (m < r) {
                int c = cmp.compare(data[m], data[l]);
                if (c == 0) {
                    m++;
                } else if (c < 0) {
                    SequenceUtils.swap(data, l, m);
                    l++;
                    m++;
                } else {
                    SequenceUtils.swap(data, m, --r);
                }
            }
            quickSort(data, cmp, f, l);
            quickSort(data, cmp, m, t);
        }

    }

    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 RectCoverProblem {
        private static void update(LongBIT bit, int l, int r, long x) {
            bit.update(l, x);
            if (bit.size() >= r + 1) {
                bit.update(r + 1, -x);
            }
        }

        private static long query(LongBIT bit, int x) {
            return bit.query(x);
        }

        public static long[] solve(RectCoverProblem.Rect[] rects, RectCoverProblem.Point[] pts) {
            if (rects.length == 0 || pts.length == 0) {
                return new long[pts.length];
            }

            int[] indices = IntStream.range(0, pts.length).toArray();
            SortUtils.quickSort(indices, (i, j) -> Long.compare(pts[i].x, pts[j].x), 0, pts.length);
            RectCoverProblem.Rect[] sortByL = rects.clone();
            RectCoverProblem.Rect[] sortByR = rects.clone();
            Arrays.sort(sortByL, Comparator.comparingLong(x -> x.l));
            Arrays.sort(sortByR, Comparator.comparingLong(x -> x.r));
            LongArrayList list = new LongArrayList(2 * rects.length + pts.length);
            for (RectCoverProblem.Rect rect : rects) {
                list.add(rect.b);
                list.add(rect.t);
            }
            for (RectCoverProblem.Point pt : pts) {
                list.add(pt.y);
            }
            list.unique();
            int sortByLHead = 0;
            int sortByRHead = 0;
            int indicesHead = 0;

            long[] ans = new long[pts.length];
            LongBIT bit = new LongBIT(list.size());
            for (int i = 0; i < list.size(); i++) {
                long v = list.get(i);
                while (sortByLHead < sortByL.length && sortByL[sortByLHead].l <= v) {
                    RectCoverProblem.Rect head = sortByL[sortByLHead++];
                    update(bit, list.binarySearch(head.b) + 1, list.binarySearch(head.t) + 1, head.val);
                }
                while (sortByRHead < sortByR.length && sortByR[sortByRHead].r < v) {
                    RectCoverProblem.Rect head = sortByR[sortByRHead++];
                    update(bit, list.binarySearch(head.b) + 1, list.binarySearch(head.t) + 1, -head.val);
                }
                while (indicesHead < indices.length && pts[indices[indicesHead]].x <= v) {
                    RectCoverProblem.Point pt = pts[indices[indicesHead]];
                    ans[indices[indicesHead]] = query(bit, list.binarySearch(pt.y) + 1);
                    indicesHead++;
                }
            }

            return ans;
        }

        public static class Rect {
            public long l;
            public long r;
            public long b;
            public long t;
            public long val;

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

        }

        public static class Point {
            public long x;
            public long y;

        }

    }

    static class SequenceUtils {
        public static void swap(int[] data, int i, int j) {
            int tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
        }

        public static boolean equal(long[] a, int al, int ar, long[] b, int bl, int br) {
            if ((ar - al) != (br - bl)) {
                return false;
            }
            for (int i = al, j = bl; i <= ar; i++, j++) {
                if (a[i] != b[j]) {
                    return false;
                }
            }
            return true;
        }

    }

    static class DigitUtils {
        private DigitUtils() {
        }

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

    }

    static class LongArrayList implements Cloneable {
        private int size;
        private int cap;
        private long[] data;
        private static final long[] EMPTY = new long[0];

        public LongArrayList(int cap) {
            this.cap = cap;
            if (cap == 0) {
                data = EMPTY;
            } else {
                data = new long[cap];
            }
        }

        public LongArrayList(long[] data) {
            this(0);
            addAll(data);
        }

        public LongArrayList(LongArrayList list) {
            this.size = list.size;
            this.cap = list.cap;
            this.data = Arrays.copyOf(list.data, size);
        }

        public LongArrayList() {
            this(0);
        }

        public void ensureSpace(int req) {
            if (req > cap) {
                while (cap < req) {
                    cap = Math.max(cap + 10, 2 * cap);
                }
                data = Arrays.copyOf(data, cap);
            }
        }

        private void checkRange(int i) {
            if (i < 0 || i >= size) {
                throw new ArrayIndexOutOfBoundsException("Access [" + i + "]");
            }
        }

        public long get(int i) {
            checkRange(i);
            return data[i];
        }

        public void add(long x) {
            ensureSpace(size + 1);
            data[size++] = x;
        }

        public void addAll(long[] x) {
            addAll(x, 0, x.length);
        }

        public void addAll(long[] x, int offset, int len) {
            ensureSpace(size + len);
            System.arraycopy(x, offset, data, size, len);
            size += len;
        }

        public void addAll(LongArrayList list) {
            addAll(list.data, 0, list.size);
        }

        public void sort() {
            if (size <= 1) {
                return;
            }
            Randomized.shuffle(data, 0, size);
            Arrays.sort(data, 0, size);
        }

        public void unique() {
            if (size <= 1) {
                return;
            }

            sort();
            int wpos = 1;
            for (int i = 1; i < size; i++) {
                if (data[i] != data[wpos - 1]) {
                    data[wpos++] = data[i];
                }
            }
            size = wpos;
        }

        public int binarySearch(long x) {
            return Arrays.binarySearch(data, 0, size, x);
        }

        public int size() {
            return size;
        }

        public long[] toArray() {
            return Arrays.copyOf(data, size);
        }

        public String toString() {
            return Arrays.toString(toArray());
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof LongArrayList)) {
                return false;
            }
            LongArrayList other = (LongArrayList) obj;
            return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
        }

        public int hashCode() {
            int h = 1;
            for (int i = 0; i < size; i++) {
                h = h * 31 + Long.hashCode(data[i]);
            }
            return h;
        }

        public LongArrayList clone() {
            LongArrayList ans = new LongArrayList();
            ans.addAll(this);
            return ans;
        }

    }

    static class Randomized {
        public static void shuffle(long[] data, int from, int to) {
            to--;
            for (int i = from; i <= to; i++) {
                int s = nextInt(i, to);
                long tmp = data[i];
                data[i] = data[s];
                data[s] = tmp;
            }
        }

        public static int nextInt(int l, int r) {
            return RandomWrapper.INSTANCE.nextInt(l, r);
        }

    }

    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 Query extends RectCoverProblem.Point {
        int ans;

    }

    static class RandomWrapper {
        private Random random;
        public static final RandomWrapper INSTANCE = new RandomWrapper();

        public RandomWrapper() {
            this(new Random());
        }

        public RandomWrapper(Random random) {
            this.random = random;
        }

        public RandomWrapper(long seed) {
            this(new Random(seed));
        }

        public int nextInt(int l, int r) {
            return random.nextInt(r - l + 1) + l;
        }

    }

    static interface IntegerComparator {
        public int compare(int a, int b);

    }

    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 LongBIT {
        private long[] data;
        private int n;

        public LongBIT(int n) {
            this.n = n;
            data = new long[n + 1];
        }

        public long query(int i) {
            i = Math.min(i, data.length - 1);
            long sum = 0;
            for (; i > 0; i -= i & -i) {
                sum += data[i];
            }
            return sum;
        }

        public void update(int i, long mod) {
            if (i <= 0) {
                return;
            }
            for (; i <= n; i += i & -i) {
                data[i] += mod;
            }
        }

        public int size() {
            return n;
        }

        public String toString() {
            StringBuilder builder = new StringBuilder();
            for (int i = 1; i <= n; i++) {
                builder.append(query(i) - query(i - 1)).append(' ');
            }
            return builder.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);
        }

    }
}

