#include using namespace std; void input(string f) { string one = f + ".in"; string two = f + ".out"; freopen(one.c_str(), "r", stdin); freopen(two.c_str(), "w", stdout); } inline unsigned long long calc(unsigned long long x, unsigned long long b, unsigned long long c){ return x*x + b*x + c; } unsigned long long bin_search(unsigned long long num, unsigned long long b, unsigned long long c, unsigned long long s, unsigned long long n, unsigned long long l, unsigned long long r){ unsigned long long mid; while(l != r){ mid = (l + r) / 2; if(calc(mid, b, c) > num){ r = mid; } else{ l = mid + 1; } } return l - s; } void solve(){ unsigned long long b1, c1, s1, b2, c2, s2, n; cin >> b1 >> c1 >> s1 >> b2 >> c2 >> s2 >> n; unsigned long long l = s1, r = s1 + n, mid, l2 = s2, r2 = s2 + n; while(l != r){ mid = (l + r) / 2; unsigned long long ans = bin_search(calc(mid, b1, c1), b2, c2, s2, n, l2, r2); if(ans + mid - s1 + 1 >= n){ r = mid; r2 = ans + s2; } else{ l = mid + 1; l2 = ans + s2; } } mid = l; if(bin_search(calc(mid, b1, c1), b2, c2, s2, n, l2, r2) + mid - s1 + 1 == n){ cout << calc(mid, b1, c1) << "\n"; return ; } l = s2, r = s2 + n, mid; l2 = s1, r2 = s1 + n; while(l != r){ mid = (l + r) / 2; unsigned long long ans = bin_search(calc(mid, b2, c2), b1, c1, s1, n, l2, r2); if(ans + mid - s2 + 1 >= n){ r = mid; r2 = ans + s1; } else{ l = mid + 1; l2 = ans + s1; } } mid = l; cout << calc(mid, b2, c2) << "\n"; return ; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); input("sequences"); unsigned long long q; cin >> q; for(unsigned long long i = 0; i < q; i++){ solve(); } return 0; }