Problem: Write a program which manipulates a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$ by the following operations:

* min($b, e$): report the minimum element in $a_b, a_{b+1}, ..., a_{e-1}$
* max($b, e$): report the maximum element in $a_b, a_{b+1}, ..., a_{e-1}$

Constraints

* $1 \leq n \leq 1,000$
* $-1,000,000,000 \leq a_i \leq 1,000,000,000$
* $1 \leq q \leq 1,000$
* $0 \leq b < e \leq n$

Input

The input is given in the following format.


$n$
$a_0 \; a_1, ..., \; a_{n-1}$
$q$
$com_1 \; b_1 \; e_1$
$com_2 \; b_2 \; e_2$
:
$com_{q} \; b_{q} \; e_{q}$


In the first line, $n$ (the number of elements in $A$) is given. In the second line, $a_i$ (each element in $A$) are given. In the third line, the number of queries $q$ is given and each query is given in the following $q$ lines. $com_i$ denotes a type of query. 0 and 1 represents min($b, e$) and max($b, e$) respectively.

Output

For each query, print the minimum element or the maximum element in a line.

Example

Input

7
8 3 7 1 9 1 4
3
0 0 3
0 1 5
1 0 7


Output

3
1
9

Solution (in CPP):

Solution:


/**
 *      purpose : 
 *      author  : kyomukyomupurin
 *      created : 
**/

// input/output
#include <iostream>
#include <fstream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstring>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
// variadic template
template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}
template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}
// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;
const int MOD = 1000000007;

struct SegmentTree{
  private:
    int n;
    std::vector<int> dat;
  public:
    SegmentTree(std::vector<int> v) {
      int SIZE = v.size();
      n = 1;
      while (n < SIZE) n <<= 1;
      dat.resize(2 * n - 1, INF);
      for (int i = 0; i < SIZE; ++i) dat[i + n - 1] = v[i];
      for (int i = n - 2; i >= 0; --i) dat[i] = std::min(dat[2 * i + 1], dat[2 * i + 2]);
    }

    void update(int pos, int val){
      pos += n - 1;
      dat[pos] = val;
      while (pos > 0){
        pos = (pos - 1) / 2;
        dat[pos] = std::min(dat[2 * pos + 1], dat[2 * pos + 2]);
      }
    }

    int query(int a, int b, int k = 0, int l = 0, int r = -1){
      if (r < 0) r = n;
      if (r <= a || b <= l) return INF;
      if (a <= l && r <= b) return dat[k];
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return std::min(vl, vr);
    }
};

struct SegmentTree2{
  private:
    int n;
    std::vector<int> dat;
  public:
    SegmentTree2(std::vector<int> v) {
      int SIZE = v.size();
      n = 1;
      while (n < SIZE) n <<= 1;
      dat.resize(2 * n - 1, -INF);
      for (int i = 0; i < SIZE; ++i) dat[i + n - 1] = v[i];
      for (int i = n - 2; i >= 0; --i) dat[i] = std::max(dat[2 * i + 1], dat[2 * i + 2]);
    }

    void update(int pos, int val){
      pos += n - 1;
      dat[pos] = val;
      while (pos > 0){
        pos = (pos - 1) / 2;
        dat[pos] = std::max(dat[2 * pos + 1], dat[2 * pos + 2]);
      }
    }

    int query(int a, int b, int k = 0, int l = 0, int r = -1){
      if (r < 0) r = n;
      if (r <= a || b <= l) return -INF;
      if (a <= l && r <= b) return dat[k];
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return std::max(vl, vr);
    }
};

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  SegmentTree seg(a);
  SegmentTree2 seg2(a);

  int query; cin >> query;

  for (int i = 0; i < query; ++i) {
    int op; cin >> op;
    int l, r; cin >> l >> r;
    if (op == 0) {
      cout << seg.query(l, r) << '\n';
    } else {
      cout << seg2.query(l, r) << '\n';
    }
  }

  return 0;
}