#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; const int INF = 1e9; const ll inf = 1LL<<60; void solve() { vector v(4); for (int i=0; i<4; i++) cin >> v[i]; ll k; cin >> k; set> s; deque> d; vector val = {v[0], 0, 0, 0}; s.insert(val); d.push_back(val); int cur = 0; ll pre = 0, loop = 0; while (1) { if (val[cur] + val[(cur+1)%4] <= v[(cur+1)%4]) { val[(cur+1)%4] += val[cur]; val[cur] = 0; } else { val[cur] -= v[(cur+1)%4] - val[(cur+1)%4]; val[(cur+1)%4] = v[(cur+1)%4]; } if (s.count(val)) { while (d.back() != val) { loop++; d.pop_back(); } loop++; d.pop_back(); pre = d.size(); break; } else { s.insert(val); d.push_back(val); } cur++; cur %= 4; } k++; if (k > pre) k = pre + ((k - pre)%loop == 0 ? loop : (k-pre)%loop); val = {v[0], 0, 0, 0}; for (int i=0; i> t; /*while (t--)*/ solve(); }