Problem: — Willem...

— What's the matter?

— It seems that there's something wrong with Seniorious...

— I'll have a look...

<image>

Seniorious is made by linking special talismans in particular order.

After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.

Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is an integer ai.

In order to maintain it, Willem needs to perform m operations.

There are four types of operations:

  * 1 l r x: For each i such that l ≤ i ≤ r, assign ai + x to ai.
  * 2 l r x: For each i such that l ≤ i ≤ r, assign x to ai.
  * 3 l r x: Print the x-th smallest number in the index range [l, r], i.e. the element at the x-th position if all the elements ai such that l ≤ i ≤ r are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1 ≤ x ≤ r - l + 1.
  * 4 l r x y: Print the sum of the x-th power of ai such that l ≤ i ≤ r, modulo y, i.e. <image>.

Input

The only line contains four integers n, m, seed, vmax (1 ≤ n, m ≤ 105, 0 ≤ seed < 109 + 7, 1 ≤ vmax ≤ 109).

The initial values and operations are generated using following pseudo code:
    
    
      
    def rnd():  
      
        ret = seed  
        seed = (seed * 7 + 13) mod 1000000007  
        return ret  
      
    for i = 1 to n:  
      
        a[i] = (rnd() mod vmax) + 1  
      
    for i = 1 to m:  
      
        op = (rnd() mod 4) + 1  
        l = (rnd() mod n) + 1  
        r = (rnd() mod n) + 1  
      
        if (l > r):   
             swap(l, r)  
      
        if (op == 3):  
            x = (rnd() mod (r - l + 1)) + 1  
        else:  
            x = (rnd() mod vmax) + 1  
      
        if (op == 4):  
            y = (rnd() mod vmax) + 1  
      
    

Here op is the type of the operation mentioned in the legend.

Output

For each operation of types 3 or 4, output a line containing the answer.

Examples

Input

10 10 7 9


Output

2
1
0
3


Input

10 10 9 9


Output

1
1
3
3

Note

In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}.

The operations are:

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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T &m, const T &v) {
  if (v > m) return m = v, true;
  return false;
}
template <typename T>
bool chmin(T &m, const T &v) {
  if (v < m) return m = v, true;
  return false;
}
template <typename T>
bool chinc(T &v1, T &v2) {
  if (v1 > v2) return swap(v1, v2), true;
  return false;
}
template <typename T>
bool chdec(T &v1, T &v2) {
  if (v1 < v2) return swap(v1, v2), true;
  return false;
}
namespace fio {
static const char eof = '\xff';
struct Cg {
  inline char operator()() {
    char c = (char)getchar();
    if (c == eof) valid = false;
    return c;
  };
  bool valid = true;
};
struct Cp {
  inline void operator()(int x) { putchar(x); }
};
struct Ce {
  inline void operator()(int x) { putc(x, stderr); }
};
inline bool IS(char x) {
  return x == ' ' || x == '\r' || x == '\n' || x == '\t';
}
template <class T>
struct Fr {
  T P;
  inline Fr &operator,(int &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(long &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(long long &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(unsigned &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(unsigned long &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(unsigned long long &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(float &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (c == '.') {
      double u = 0.1;
      for (c = P(); c >= '0' && c <= '9'; c = P(), u *= 0.1) x += u * (c - '0');
    };
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(double &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (c == '.') {
      double u = 0.1;
      for (c = P(); c >= '0' && c <= '9'; c = P(), u *= 0.1) x += u * (c - '0');
    };
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(long double &x) {
    x = 0;
    int c = P();
    while ((c < '0' || c > '9') && c != '-') c = P();
    bool h = false;
    if (c == '-') h = true, c = P();
    for (; c >= '0' && c <= '9'; c = P()) x = x * 10 + c - '0';
    if (c == '.') {
      double u = 0.1;
      for (c = P(); c >= '0' && c <= '9'; c = P(), u *= 0.1) x += u * (c - '0');
    };
    if (h) x = -x;
    return *this;
  }
  inline Fr &operator,(char &x) {
    for (x = P(); IS(x);) x = P();
    return *this;
  }
  inline Fr &operator,(char *s) {
    for (*s = P(); IS(*s) && P.valid;) *s = P();
    for (; !IS(*s) && P.valid;) *++s = P();
    *s = 0;
    return *this;
  }
  inline Fr &operator,(string &s) {
    s.clear();
    char c = P();
    for (; IS(c) && P.valid;) c = P();
    for (; !IS(c) && P.valid;) s.push_back(c), c = P();
    return *this;
  }
  inline operator bool() { return P.valid; }
};
template <class T>
struct Fw {
  T P;
  char B[32];
  inline Fw &operator,(int x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator,(long x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator,(long long x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator,(unsigned x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator,(unsigned long x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator,(unsigned long long x) {
    if (x) {
      if (x < 0) P('-'), x = -x;
      int p = 0;
      while (x) B[p++] = x % 10 + '0', x /= 10;
      while (p--) P(B[p]);
    } else
      P('0');
    return *this;
  }
  inline Fw &operator()(float g, int d) {
    if (d) {
      double t = 0.5;
      for (int i = d; i--;) t *= 0.1;
      if (g < 0) P('-'), g = -g;
      g += t;
      *this, (long long)(g);
      P('.');
      g -= floor(g);
      while (d--) {
        g *= 10;
        int i = (int)floor(g);
        P(i + '0');
        g -= i;
      }
    } else
      *this, (g > 0 ? (long long)(g + 0.5) : (long long)(g - 0.5));
    return *this;
  }
  inline Fw &operator()(double g, int d) {
    if (d) {
      double t = 0.5;
      for (int i = d; i--;) t *= 0.1;
      if (g < 0) P('-'), g = -g;
      g += t;
      *this, (long long)(g);
      P('.');
      g -= floor(g);
      while (d--) {
        g *= 10;
        int i = (int)floor(g);
        P(i + '0');
        g -= i;
      }
    } else
      *this, (g > 0 ? (long long)(g + 0.5) : (long long)(g - 0.5));
    return *this;
  }
  inline Fw &operator()(long double g, int d) {
    if (d) {
      double t = 0.5;
      for (int i = d; i--;) t *= 0.1;
      if (g < 0) P('-'), g = -g;
      g += t;
      *this, (long long)(g);
      P('.');
      g -= floor(g);
      while (d--) {
        g *= 10;
        int i = (int)floor(g);
        P(i + '0');
        g -= i;
      }
    } else
      *this, (g > 0 ? (long long)(g + 0.5) : (long long)(g - 0.5));
    return *this;
  }
  inline Fw &operator,(char x) {
    P(x);
    return *this;
  }
  typedef basic_ostream<char, char_traits<char>> &OS;
  typedef basic_istream<char, char_traits<char>> &IS;
  inline Fw &operator,(OS &(*func)(OS)) {
    if (func == static_cast<OS &(*)(OS)>(endl<char, char_traits<char>>))
      P('\n');
    return *this;
  }
  inline Fw &operator,(IS &(*func)(IS)) {
    if (func == static_cast<IS &(*)(IS)>(ws<char, char_traits<char>>)) P(' ');
    return *this;
  }
  inline Fw &operator,(const char *s) {
    for (; *s; ++s) P(*s);
    return *this;
  }
  inline Fw &operator,(const string &s) {
    (*this), s.c_str();
    return *this;
  }
  template <class C>
  inline Fw &all(const C &v, char sep = ' ', char nl = '\n') {
    auto it = v.begin();
    for (int i = 0; i < (int)v.size(); ++i, ++it) {
      if (i && sep) P(sep);
      (*this), *it;
    }
    if (nl) P(nl);
    return *this;
  }
  template <class U>
  inline Fw &print(U x) {
    (*this), x;
    P('\n');
    return *this;
  }
  template <class U, class... Us>
  inline Fw &print(U x, Us... args) {
    (*this), x;
    P(' ');
    print(args...);
    return *this;
  }
};
}  // namespace fio
fio::Fr<fio::Cg> fin;
fio::Fw<fio::Cp> fout;
fio::Fw<fio::Ce> ferr;
template <typename Int>
Int gcd(Int a, Int b) {
  while (a != 0) {
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
  return b;
}
template <typename Int, typename RInt = Int>
Int lcm(Int a, Int b) {
  return a / gcd(a, b) * static_cast<RInt>(b);
}
template <typename Int, typename XInt>
Int extended_gcd(Int a, Int b, XInt &x, XInt &y) {
  static_assert(std::numeric_limits<XInt>::is_signed, "x, y must be signed");
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  Int g = extended_gcd(b, a % b, y, x);
  y -= (a / b) * x;
  return g;
}
template <typename Int>
Int mul_mod(Int a, Int b, Int m) {
  static_assert(2 * std::numeric_limits<Int>::digits <=
                    std::numeric_limits<int64_t>::digits,
                "multiplication will overflow!");
  int64_t p = a;
  p = (p * b) % m;
  return (Int)p;
}
template <>
int64_t mul_mod<int64_t>(int64_t a, int64_t b, int64_t m) {
  __int128_t p = __int128_t(a) * b;
  return (int64_t)(p % m);
}
template <typename Int, typename PInt>
Int pow_mod(Int a, PInt p, Int m) {
  Int r;
  a %= m;
  for (r = 1; p; p >>= 1) {
    if (p & 1) r = mul_mod(r, a, m);
    a = mul_mod(a, a, m);
  }
  return r;
}
template <typename Int, typename XInt = typename std::make_signed<Int>::type>
Int inv_mod(Int a, Int m) {
  XInt x, y;
  extended_gcd(a, m, x, y);
  if (x < 0) return x + m;
  return x;
}
template <typename Int>
Int addx_mod(Int u, Int v, Int m) {
  Int r = u + v;
  if (r >= m) r -= m;
  return r;
}
template <typename Int>
Int subx_mod(Int u, Int v, Int m) {
  Int r = u - v;
  if (r < 0) r += m;
  return r;
}
template <typename Int>
Int div_mod(Int u, Int v, Int m) {
  Int g = gcd(v, m);
  assert(u % g == 0);
  Int mm = m / g;
  return mul_mod(u / g, inv_mod(v / g, mm), mm);
}
template <typename Int>
Int div_pos_r(Int a, Int b, Int &r) {
  Int q = a / b;
  r = a % b;
  if (r < 0) {
    if (b < 0)
      q += 1, r -= b;
    else
      q -= 1, r += b;
  }
  return q;
}
static const int maxn = 100005;
int n, m, seed, vmax;
int64_t a[maxn];
struct node_t {
  int len;
  int64_t v;
};
map<int, node_t> mp;
int rnd() {
  int ret = seed;
  seed = (1LL * seed * 7 + 13) % 1000000007;
  return ret;
}
void add(int l, int r, int x) {
  auto it = mp.lower_bound(l);
  if (it == mp.end() || it->first > l) --it;
  assert(it->first <= l);
  int curx = it->first;
  while (curx <= r) {
    it = mp.lower_bound(curx);
    curx += it->second.len;
    if (it->first >= l && it->first + it->second.len - 1 <= r) {
      it->second.v += x;
      ++it;
    } else {
      int start = it->first;
      node_t cur = it->second;
      it = mp.erase(it);
      if (l > start) {
        mp[start] = node_t{l - start, cur.v};
      }
      if (r < start + cur.len - 1) {
        mp[r + 1] = node_t{start + cur.len - r - 1, cur.v};
      }
      int b = max(l, start), e = min(r, start + cur.len - 1);
      if (e >= b) mp[b] = node_t{e - b + 1, cur.v + x};
    }
  }
}
void assign(int l, int r, int x) {
  auto it = mp.lower_bound(l);
  if (it == mp.end() || it->first > l) --it;
  int curx = it->first;
  while (curx <= r) {
    it = mp.lower_bound(curx);
    curx += it->second.len;
    if (it->first >= l && it->first + it->second.len - 1 <= r) {
      it = mp.erase(it);
    } else {
      int start = it->first;
      node_t cur = it->second;
      it = mp.erase(it);
      if (l > start) {
        mp[start] = node_t{l - start, cur.v};
      }
      if (r < start + cur.len - 1) {
        mp[r + 1] = node_t{start + cur.len - r - 1, cur.v};
      }
    }
  }
  mp[l] = node_t{r - l + 1, x};
}
int64_t kth(int l, int r, int x) {
  auto it = mp.lower_bound(l);
  if (it == mp.end() || it->first > l) --it;
  vector<pair<int64_t, int>> vec;
  while (it != mp.end() && it->first <= r) {
    if (it->first >= l && it->first + it->second.len - 1 <= r) {
      vec.push_back(make_pair(it->second.v, it->second.len));
      ++it;
    } else {
      int start = it->first;
      node_t cur = it->second;
      int b = max(l, start), e = min(r, start + cur.len - 1);
      vec.push_back(make_pair(cur.v, e - b + 1));
      ++it;
    }
  }
  int tot = 0;
  for (auto kv : vec) {
    tot += kv.second;
  }
  assert(tot == r - l + 1);
  sort(begin(vec), end(vec));
  for (auto kv : vec) {
    if (x < kv.second) return kv.first;
    x -= kv.second;
  }
  throw 0;
}
int64_t psum(int l, int r, int x, int y) {
  auto it = mp.lower_bound(l);
  if (it == mp.end() || it->first > l) --it;
  vector<pair<int64_t, int>> vec;
  while (it != mp.end() && it->first <= r) {
    if (it->first >= l && it->first + it->second.len - 1 <= r) {
      vec.push_back(make_pair(it->second.v, it->second.len));
      ++it;
    } else {
      int start = it->first;
      node_t cur = it->second;
      int b = max(l, start), e = min(r, start + cur.len - 1);
      vec.push_back(make_pair(cur.v, e - b + 1));
      ++it;
    }
  }
  int tot = 0;
  for (auto kv : vec) {
    tot += kv.second;
  }
  assert(tot == r - l + 1);
  int ans = 0;
  for (auto kv : vec) {
    ans += 1LL * pow_mod(int(kv.first % y), x, y) * kv.second % y;
    ans %= y;
  }
  return ans;
}
void check() {
  int cur = 1;
  auto it = mp.begin();
  while (it != mp.end()) {
    assert(it->first == cur);
    cur += it->second.len;
    assert(it->second.len > 0);
    ++it;
  }
  assert(cur == n + 1);
}
int main() {
  fin, n, m, seed, vmax;
  for (int i = 1, iend = n; i <= iend; i++) {
    a[i] = (rnd() % vmax) + 1;
    mp[i] = node_t{1, 1LL * a[i]};
  }
  for (int i = 1, iend = m; i <= iend; i++) {
    int op = (rnd() % 4) + 1;
    int l = (rnd() % n) + 1;
    int r = (rnd() % n) + 1;
    int x, y = 0;
    if (l > r) swap(l, r);
    if (op == 3)
      x = (rnd() % (r - l + 1)) + 1;
    else
      x = (rnd() % vmax) + 1;
    if (op == 4) y = (rnd() % vmax) + 1;
    if (op == 1) {
      add(l, r, x);
    } else if (op == 2) {
      assign(l, r, x);
    } else if (op == 3) {
      int64_t ans = kth(l, r, x - 1);
      fout, ans, endl;
    } else {
      assert(op == 4);
      int64_t ans = psum(l, r, x, y);
      fout, ans, endl;
    }
  }
  return 0;
}