#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long mod = 1e9 + 7;
long long n, m, seed, vmax, a[N];
long long rnd() {
  int ret = seed;
  seed = (seed * 7 + 13) % mod;
  return ret;
}
struct node {
  long long l, r;
  mutable long long val;
  node(long long L, long long R = -1, long long V = 0) : l(L), r(R), val(V){};
  bool operator<(const node& tt) const { return l < tt.l; }
};
set<node> st;
long long qpow(long long a, long long k, long long p) {
  long long res = 1;
  a %= p;
  while (k > 0) {
    if (k & 1) res = (res * a) % p;
    a = (a * a) % p;
    k >>= 1;
  }
  return res;
}
set<node>::iterator split(long long pos) {
  set<node>::iterator it = st.lower_bound(node(pos));
  if (it != st.end() && it->l == pos) {
    return it;
  }
  --it;
  long long l = it->l, r = it->r, val = it->val;
  st.erase(it);
  st.insert(node(l, pos - 1, val));
  return st.insert(node(pos, r, val)).first;
}
void assign(long long l, long long r, long long val) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  st.erase(itl, itr);
  st.insert(node(l, r, val));
}
void add(long long l, long long r, long long val) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  for (; itl != itr; itl++) {
    itl->val += val;
  }
}
long long sum(long long l, long long r, long long x, long long y) {
  long long res = 0;
  set<node>::iterator itr = split(r + 1), itl = split(l);
  for (; itl != itr; itl++) {
    res += ((qpow(itl->val, x, y) % y) * ((itl->r - itl->l + 1) % y)) % y;
    res %= y;
  }
  return res;
}
long long kth(long long l, long long r, long long k) {
  vector<pair<long long, long long> > vec;
  set<node>::iterator itr = split(r + 1), itl = split(l);
  for (; itl != itr; itl++) {
    vec.push_back(pair<long long, long long>(itl->val, itl->r - itl->l + 1));
  }
  sort(vec.begin(), vec.end());
  for (vector<pair<long long, long long> >::iterator it = vec.begin();
       it != vec.end(); it++) {
    k -= it->second;
    if (k <= 0) {
      return it->first;
    }
  }
  return -1;
}
int main() {
  scanf("%lld%lld%lld%lld", &n, &m, &seed, &vmax);
  for (long long i = 1; i <= n; i++) {
    a[i] = (rnd() % vmax) + 1;
    st.insert(node(i, i, a[i]));
  }
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    long long op = (rnd() % 4) + 1, l = (rnd() % n) + 1, r = (rnd() % n) + 1;
    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) {
          printf("%lld\n", kth(l, r, x));
        } else {
          printf("%lld\n", sum(l, r, x, y));
        }
      }
    }
  }
  return 0;
}
