#include #include #include using namespace std; typedef long long Int; struct arr { Int init,a,b,mod; }; arr arrs[300001]; Int n,m; priority_queue< pair > pq; Int q[300001]; Int ans[300001]; int main() { freopen("arrays.in","r",stdin); freopen("arrays.out","w",stdout); Int i; Int themax=0; pair k; scanf("%I64d",&n); for (i=1;i<=n;i++) { scanf("%I64d %I64d %I64d %I64d",&arrs[i].init,&arrs[i].a,&arrs[i].b,&arrs[i].mod); pq.push( make_pair(-arrs[i].init,i) ); } scanf("%I64d",&m); for (i=1;i<=m;i++) { scanf("%I64d",&q[i]); if (q[i]>themax) { themax=q[i]; } } for (i=1;i<=themax;i++) { k=pq.top(); pq.pop(); k.first=-k.first; ans[i]=k.first; pq.push( make_pair( -(k.first+(k.first*arrs[ k.second ].a+arrs[ k.second ].b)%arrs[ k.second ].mod) , k.second ) ); } for (i=1;i<=m;i++) { printf("%I64d\n", ans[ q[i] ] ); } return 0; }