#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b+1]-sm[a-1]<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a==b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b+1]-sm[a-1]<sm[b]-sm[a-1] || sm[b+1]-sm[a-1]<0)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a==b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b]-sm[a-1]<mx || sm[b]-sm[a-1]<0)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

ll mem[300005][11][2], vis[300005][11][2], in[300005];
ll n,m,k;

ll i_am_a_fucking_stupid(ll i, ll md, ll st)
{
    if(i>n) re 0;
    if(vis[i][md][st]) re mem[i][md][st];
    vis[i][md][st]=1;
    ll r1=0,r2=0;
    if(!st)
    {
        r1=i_am_a_fucking_stupid(i+1, md, 0);
        r2=in[i]+i_am_a_fucking_stupid(i+1,i%m,1)-k;
        re mem[i][md][st]=max(r1,r2);
    }
    r1=in[i]+i_am_a_fucking_stupid(i+1,md,1);
    if(i%m==md) r1-=k;
    re mem[i][md][st]=max(0LL,r1);
}

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,cnt=0,g,flg,t;
    cin>>n>>m>>k;
    fr(i,1,n)
        cin>>in[i];
    fr(i,1,m)
    {
        cnt=max(cnt,i_am_a_fucking_stupid(i,i%m,0));
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a>b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b]-sm[a-1]<0 || (b-a+1==2*m && sm[b]-sm[a-1]-k*x<cnt))
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a==b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b]-sm[a-1]<mx)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        x=b-a+1;
        x++;
        x/=m;
        //cout<<a<<" "<<b<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a==b)
            b++;
        else if(sm[b]-sm[a-1]<0)
            a++;
        else if(b<n)
            b++;
        else
            a++;
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a==b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b+1]-sm[a-1]<sm[b]-sm[a-1] || sm[b]-sm[a-1]<0)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b]-sm[a-1]<<" "<<sm[b]-sm[a-1]-k*x<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a>b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b]-sm[a-1]<0 || (b-a+1>=2*m && sm[b]-sm[a-1]-k*x<cnt))
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    ll int i,j,x,y,n,m,cnt=0,k,g,flg,t;
    cin>>n>>m>>k;
    ll in[n+1],sm[n+1];
    sm[0]=0;
    fr(i,1,n)
    {
        cin>>in[i];
        sm[i]=in[i]+sm[i-1];
    }
    ll a,b,mx=0;
    a=b=1;
    while(a<=n && b<=n)
    {
        y=b-a+1;
        x=y/m;
        if(y%m) x++;

        //cout<<a<<" "<<b<<" "<<sm[b+1]-sm[a-1]<<" "<<x<<endl;
        cnt=max(cnt, sm[b]-sm[a-1]-k*x);
        if(a>b)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else if(sm[b+1]-sm[a-1]<0)
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
        else if(b<n)
        {
            mx=sm[b]-sm[a-1];
            b++;
        }
        else
        {
            mx=sm[b]-sm[a-1];
            a++;
        }
    }
    cout<<cnt;
}


#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 5e5;
int dp[N][15][3],a[N];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n+1)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,(cnt+1)%m,1) ;
        if(cnt==0)
        {
            ans-=k ;
        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,1),call(i+1,0,0)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
        return 0;
    }


#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 5e5;
int dp[N][15][3],a[N];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n+1)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,(cnt+1),1) ;
        if(cnt%m==0)
        {
            ans-=k ;
        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,1),call(i+1,0,0)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
        return 0;
    }


#include <bits/stdc++.h>
#define int long long
using namespace std ;

int32_t main()
{
    int n,m,k ;
    cin>>n>>m>>k ;
    int a[n+1] ;
    for(int i=1;i<=n;i++)cin>>a[i] ;
    int ans=0 ;
    for(int i=1;i<=m;i++)
    {
        int now =0 , c=0 ;
        for(int j=i;j<=n;j++)
        {

            if(c%m==0)
            {   now=max(now,0LL) ;

                now-=k ;

            }
            now +=a[j] ;    c++ ;
            ans=max(now,ans) ;
        }
    }
    cout<<ans<<endl ;
    return 0 ;
}


#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 5e5;
int dp[N][15][3],a[N];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,cnt+1,1) ;
        if(cnt%m==0)
        {
            ans-=k ;
        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,1),call(i+1,0,0)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=0; i<n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
        return 0;
    }


#include <bits/stdc++.h>
#define int long long
using namespace std ;
int dp[4*100000][15][3],a[100000*4];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,cnt+1,1) ;
        if(cnt%m==0)
        {
            ans-=k ;

        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,0),call(i+1,0,1)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=0; i<n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
    }


#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 5e5;
int dp[N][15][3],a[N];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,(cnt+1)%m,1) ;
        if(cnt==0)
        {
            ans-=k ;
        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,1),call(i+1,0,0)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=0; i<n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
        return 0;
    }


#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 5e5;
int dp[N][15][3],a[N];
int n,m,k;

int call(int i,int cnt,int f)
{
    if(i>=n+1)
        return 0 ;
    if(dp[i][cnt][f]!=-1)
        return dp[i][cnt][f] ;
    int ans=0 ;
    if(f)
    {
        ans=a[i] + call(i+1,(cnt+1)%m,1) ;
        if(cnt==0)
        {
            ans-=k ;
        }
         ans=max(ans,0LL) ;
    }
    else {
        ans=max(call(i+1,0,1),call(i+1,0,0)) ;
    }
    dp[i][cnt][f] = ans ;
    return dp[i][cnt][f]  ;
}
    int32_t  main()
    {
        cin>>n>>m>>k ;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i] ;
        }
        memset(dp,-1,sizeof dp) ;
        cout<<call(0,0,0) <<endl ;
        return 0;
    }
.


#include<iostream>
#include<algorithm>
using namespace std;
int pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db) cout << endl;
    if (db)
    {
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    int minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max(maxsum, pre[i] - minv + ( i-mini % m == 0?k:0 ));
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    cout << max(0, maxsum - k) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
int pre[300005];
int   a[300005];
int n, m, k;

void precalc(int r){
    // r = 0.. m-1
    //cout << "Precalc: r = " << r << "\n";
    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
     //   cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
}

int findmax(){
    int minv=0, maxsum=0;
    for (int i=0; i<n+1; i++){
        maxsum = max(maxsum, pre[i] - minv);
        minv = min(minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        //cout << "FINDMAX: " << f << endl;
    }

    cout << max(0, maxsum - k) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
int pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    int minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max(maxsum, pre[i] - minv + ( i-mini + 1 % m == 0?k:0 ));
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    cout << max(0, maxsum - k) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
long long pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    long long minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max((long long) maxsum, pre[i] - minv);
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int findmax2(int r){
    long long minv=pre[r], maxsum=0;
    for (int i=r; i<n+1; i+=m){
        maxsum = max((long long)maxsum, pre[i] - minv);
        minv = min((long long) minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    long long maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        long long f = findmax();
        maxsum = max(maxsum, f);s
        if (db) cout << "FINDMAX: " << f << endl;
    }

    maxsum -= k;
    for (int r=0; r<m; r++){
            maxsum = max(maxsum, findmax2(r));
    }
    cout << max(0, maxsum) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
long long pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

long long findmax(){
    long long minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max((long long) maxsum, pre[i] - minv);
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

long long findmax2(int r){
    long long minv=pre[r], maxsum=0;
    for (int i=r; i<n+1; i+=m){
        maxsum = max((long long)maxsum, pre[i] - minv);
        minv = min((long long) minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    long long maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        long long f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    maxsum -= k;
    for (int r=0; r<m; r++){
            maxsum = max(maxsum, findmax2(r));
    }
    cout << max(0ll, maxsum) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
int pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    int minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max(maxsum, pre[i] - minv);
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int findmax2(int r){
    int minv=pre[r], maxsum=0;
    for (int i=r; i<n+1; i+=m){
        maxsum = max(maxsum, pre[i] - minv);
        minv = min(minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    maxsum -= k;
    for (int r=0; r<m; r++){
            maxsum = max(maxsum, findmax2(r));
    }
    cout << max(0, maxsum) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
long long pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    int minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max(maxsum, pre[i] - minv);
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int findmax2(int r){
    int minv=pre[r], maxsum=0;
    for (int i=r; i<n+1; i+=m){
        maxsum = max(maxsum, pre[i] - minv);
        minv = min(minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    maxsum -= k;
    for (int r=0; r<m; r++){
            maxsum = max(maxsum, findmax2(r));
    }
    cout << max(0, maxsum) << endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
long long pre[300005];
int   a[300005];
int n, m, k;
#define db false

void precalc(int r){
    // r = 0.. m-1
    if (db) cout << "Precalc: r = " << r << "\n";

    pre[0] = 0;
    for (int i=0, x; i<n; i++){
        x = a[i];
        if (i%m == r) x-=k;
        if (db) cout << x << ' ';
        pre[i+1] = x + pre[i];
    }
    if (db)
    {
        cout << endl;
        for (int i=0; i<n+1; i++)
            cout << pre[i] << ' ';
        cout << endl;

    }
}

int findmax(){
    int minv=0, maxsum=0, mini=0;
    for (int i=0; i<n+1; i++){
        maxsum = max((long long) maxsum, pre[i] - minv);
        if (pre[i] < minv)
            minv = pre[i], mini=i;
    }
    return maxsum;
}

int findmax2(int r){
    int minv=pre[r], maxsum=0;
    for (int i=r; i<n+1; i+=m){
        maxsum = max((long long)maxsum, pre[i] - minv);
        minv = min((long long) minv, pre[i]);
    }
    return maxsum;
}

int main(){
    cin >> n >> m >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];

    int maxsum = 0;
    for (int r=0; r<m; r++){
        precalc(r);
        int f = findmax();
        maxsum = max(maxsum, f);
        if (db) cout << "FINDMAX: " << f << endl;
    }

    maxsum -= k;
    for (int r=0; r<m; r++){
            maxsum = max(maxsum, findmax2(r));
    }
    cout << max(0, maxsum) << endl;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll func(ll m,ll r,ll l){
    double temp = (double)(r-l+1)/m;
    ll temp1 = (r-l+1)/m;
    if((double)temp1==temp){
        return temp1;
    }

    else{

        return temp1+1;

    }

}
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    dp[0]=a[0];
    vector <ll> l(n);
    vector <ll> r(n);
    l[0]=0;
    r[0]=0;
    ll prev = 0;
    for(ll i=1;i<n;i++){
        if(a[i]+dp[i-1]>a[i]){
            dp[i]=a[i]+dp[i-1];
            r[i]=i;
            l[i]=prev;
        }
        else{
            r[i]=i;
            l[i]=i;
            dp[i]=a[i];
            prev=i;
        }
    }
    ll maxi = dp[0];
    for(ll i=1;i<n;i++){
        maxi = max(maxi,dp[i]-k*func(m,r[i],l[i]));
    }
    if(maxi>0)
        cout<<maxi<<endl;
    else{
        cout<<0<<endl;
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
#define f first
#define s second
#define M 1e7
typedef long long int ll;
using namespace std;
//vector <vector <vector <ll>>> dp;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n);
    dp[0]=a[0]-k;
    ll ans = 0;
    for(ll i=1;i<n;i++){
        ll sum = 0;
        ll maxi = 0;
        for(ll j=i;i-j<m && j>=0;j--){
            sum=sum+a[j];
            maxi=max(maxi,sum);
        }
        dp[i]=max(dp[i],maxi-k);
        if(i>=m){
            dp[i]=max(dp[i],dp[i-m]+sum-k);
        }
        ans=max(ans,dp[i]);
    }
//    for(ll i=0;i<n;i++){
//        cout<<dp[i]<<" ";
//    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll func(ll m,ll r,ll l){
    double temp = (double)(r-l+1)/m;
    ll temp1 = (r-l+1)/m;
    if((double)temp1==temp){
        return temp1;
    }

    else{

        return temp1+1;

    }

}
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    dp[0]=a[0];
    vector <ll> l(n);
    vector <ll> r(n);
    l[0]=0;
    r[0]=0;
    ll prev = 0;
    for(ll i=1;i<n;i++){
        if(a[i]+dp[i-1]>a[i]){
            dp[i]=a[i]+dp[i-1];
            r[i]=i;
            l[i]=prev;
        }
        else{
            r[i]=i;
            l[i]=i;
            dp[i]=a[i];
            prev=i;
        }
    }
    ll maxi = INT_MIN;
    for(ll i=0;i<n;i++){
        for(ll j=1;j<=m;j++)
            maxi = max(maxi,dp[i]-k*func(j,r[i],l[i]));
    }
    if(maxi>0)
        cout<<maxi<<endl;
    else{
        cout<<0<<endl;
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    ll ans = INT_MIN;
    if(m>1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
            ll sum = a[i];
            ll maxi = a[i]-k;
            for(ll j=i-1;j>i-m && j>=0;j--){
                dp[i]=max(dp[i],dp[j]+sum-k);
    //            cout<<i<<" "<<j<<" "<<dp[i]<<endl;
                sum+=a[j];
                maxi = max(maxi,sum-k);
            }
    //        cout<<"maxi"<<" "<<maxi<<endl;
            dp[i]=max(dp[i],maxi);
            if(dp[i]<0){
                dp[i]=0;
            }
            ans = max(ans,dp[i]);
        }
    }
    else if(m==1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
        }
        ans = dp[0];
        ll curr = dp[0];
        for(ll i=1;i<n;i++){
            curr = max(dp[i],curr+dp[i]);
            ans = max(ans,curr);
        }

    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    ll ans = INT_MIN;
    for(ll i=0;i<n;i++){
        dp[i]=a[i]-k;
        ll sum = a[i];
        ll maxi = a[i];
        for(ll j=i-1;j>i-m && j>=0;j--){
            dp[i]=max(dp[i],dp[j]+sum-k);
            sum+=a[j];
            dp[i]=max(dp[i],sum-k);
        }
        ans = max(ans,dp[i]);
    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    ll ans = INT_MIN;
    if(m>1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
            ll sum = a[i];
            ll maxi = a[i]-k;
            for(ll j=i-1;j>i-m && j>=0;j--){
                dp[i]=max(dp[i],dp[j]+sum-k);
    //            cout<<i<<" "<<j<<" "<<dp[i]<<endl;
                sum+=a[j];
                maxi = max(maxi,sum-k);
            }
    //        cout<<"maxi"<<" "<<maxi<<endl;
            dp[i]=max(dp[i],maxi);
            ans = max(ans,dp[i]);
        }
    }
    else if(m==1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
        }
        ans = dp[0];
        ll curr = dp[0];
        for(ll i=1;i<n;i++){
            curr = max(dp[i],curr+dp[i]);
            ans = max(ans,curr);
        }

    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
#define f first
#define s second
#define M 1e7
typedef long long int ll;
using namespace std;
//vector <vector <vector <ll>>> dp;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n);
    dp[0]=a[0]-k;
    ll ans = a[0]-k;
    for(ll i=1;i<n;i++){
        ll sum = 0;
        ll maxi = 0;
        for(ll j=i;i-j<m && j>=0;j--){
            sum=sum+a[j];
            maxi=max(maxi,sum);
        }
        dp[i]=max(dp[i],maxi-k);
        if(i>=m){
            dp[i]=max(dp[i],dp[i-m]+sum-k);
        }
        ans=max(ans,dp[i]);
    }
//    for(ll i=0;i<n;i++){
//        cout<<dp[i]<<" ";
//    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    vector <ll> dp(n,INT_MIN);
    ll ans = INT_MIN;
    for(ll i=0;i<n;i++){
        dp[i]=a[i]-k;
        ll sum = a[i];
        ll maxi = a[i]-k;
        for(ll j=i-1;j>i-m && j>=0;j--){
            dp[i]=max(dp[i],dp[j]+sum-k);
            sum+=a[j];
            maxi = max(maxi,sum-k);
        }
        dp[i]=max(dp[i],maxi);
        ans = max(ans,dp[i]);
    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector <ll> a(n);
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }

    vector <ll> dp(n,INT_MIN);
    ll ans = INT_MIN;
    if(m>1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
            ll sum = a[i];
            ll maxi = a[i]-k;
            for(ll j=i-1;j>i-m && j>=0;j--){
                dp[i]=max(dp[i],dp[j]+sum-k);
    //            cout<<i<<" "<<j<<" "<<dp[i]<<endl;
                sum+=a[j];
                maxi = max(maxi,sum-k);
            }
    //        cout<<"maxi"<<" "<<maxi<<endl;
            dp[i]=max(dp[i],maxi);
            ans = max(ans,dp[i]);
        }
    }
    else if(m==1){
        for(ll i=0;i<n;i++){
            dp[i]=a[i]-k;
        }
        ans = dp[0];
        ll curr = dp[0];
        for(ll i=1;i<n;i++){
            curr = max(dp[i],curr+dp[i]);
            ans = max(ans,curr);
        }

    }
    if(ans>0)
        cout<<ans<<endl;
    else
        cout<<0<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=1<<18;
const ll INF=1LL<<55;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M,K;cin>>N>>M>>K;
    vector<ll> A(N+1);
    for(int i=1;i<=N;i++){
        //cin>>A[i];
        scanf("%lld",&A[i]);
        A[i]+=A[i-1];
    }

    ll dp[N+1][M];

    for(int i=0;i<N+1;i++){
        for(int j=0;j<M;j++){
            if(j==0) dp[i][j]=0;
            else dp[i][j]=-INF;
        }
    }

    for(int i=1;i<=N;i++){
        for(int j=0;j<M;j++){
            for(int k=1;k<=M;k++){
                if(i-k<0) continue;
                if(j-k>0) dp[i][j]=max(dp[i][j],dp[i-k][(j-k)]+A[i]-A[i-k]);
                else dp[i][j]=max(dp[i][j],dp[i-k][(j+M-k)%M]+A[i]-A[i-k]-K);
            }
        }
    }

    ll ans=0;

    for(int i=1;i<=N;i++){
        for(int j=0;j<M;j++){
            ans=max(ans,dp[i][j]);
        }
    }

    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100001,INF=1<<30;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M;
    ll K;cin>>N>>M>>K;
    vector<ll> a(N),S(N+1,0);
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=1;i<=N;i++){
        S[i]=S[i-1]+a[i-1];
    }

    int left=-1,right=-1;
    ll diffmax=K;
    for(int i=1;i<=M;i++){
        for(int j=0;j+i<=N;j++){
            if(S[j+i]-S[j]>diffmax){
                diffmax=S[j+i]-S[j];
                left=j;
                right=j+i;
            }
        }
    }

    //cout<<left<<" "<<right<<endl;

    if(left==-1) cout<<0<<endl;
    else{
        while(1){
            int diff=(right-left-1)%M+1;
            diffmax=K;
            int LE=left,RI=right;
            for(int i=M-diff+1;i<=2*M-diff;i++){
                int l=max(0,left-i),r=left;
                if(S[r]-S[l]>diffmax){
                    diffmax=S[r]-S[l];
                    LE=l;
                }
                l=right;r=min(N,right+i);
                if(S[r]-S[l]>diffmax){
                    diffmax=S[r]-S[l];
                    RI=r;
                }
            }
            if(diffmax==K){
                cout<<S[right]-S[left]-(right-left+M-1)/M*K<<endl;
                break;
            }else{
                left=LE;
                right=RI;
            }
        }
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100001,INF=1<<30;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M;
    ll K;cin>>N>>M>>K;
    vector<ll> a(N),S(N+1,0);
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=1;i<=N;i++){
        S[i]=S[i-1]+a[i-1];
    }

    int left=-1,right=-1;
    ll diffmax=K;
    for(int i=1;i<=M;i++){
        for(int j=0;j+i<=N;j++){
            if(S[j+i]-S[j]>diffmax){
                diffmax=S[j+i]-S[j];
                left=j;
                right=j+i;
            }
        }
    }

    //cout<<left<<" "<<right<<endl;

    if(left==-1) cout<<0<<endl;
    else{
        while(1){
            int L=left,R=right;
            ll ans=diffmax-K;
            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=left;l++){
                    if(S[r]-S[l]-(r-l+M-1)/M*K>ans){
                        ans=S[r]-S[l]-(r-l+M-1)/M*K;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-(r-left+M-1)/M*K>ans){
                    ans=S[r]-S[left]-(r-left+M-1)/M*K;
                    right=r;
                }
            }

            cout<<ans<<endl;
            break;
        }
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=1<<18;
const ll INF=1LL<<55;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M,K;cin>>N>>M>>K;
    vector<ll> A(N+1);
    for(int i=1;i<=N;i++){
        cin>>A[i];
        A[i]+=A[i-1];
    }

    ll dp[N+1][M];

    for(int i=0;i<N+1;i++){
        for(int j=0;j<M;j++){
            if(j==0) dp[i][j]=0;
            else dp[i][j]=-INF;
        }
    }

    for(int i=1;i<=N;i++){
        for(int j=0;j<M;j++){
            for(int k=1;k<=M;k++){
                if(i-k<0) continue;
                if(j-k>0) dp[i][j]=max(dp[i][j],dp[i-k][(j-k)]+A[i]-A[i-k]);
                else dp[i][j]=max(dp[i][j],dp[i-k][(j+M-k)%M]+A[i]-A[i-k]-K);
            }
        }
    }

    ll ans=0;

    for(int i=1;i<=N;i++){
        for(int j=0;j<M;j++){
            ans=max(ans,dp[i][j]);
        }
    }

    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100001,INF=1<<30;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M;
    ll K;cin>>N>>M>>K;
    vector<ll> a(N),S(N+1,0);
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=1;i<=N;i++){
        S[i]=S[i-1]+a[i-1];
    }

    int left=-1,right=-1;
    ll diffmax=K;
    for(int i=1;i<=M;i++){
        for(int j=0;j+i<=N;j++){
            if(S[j+i]-S[j]>diffmax){
                diffmax=S[j+i]-S[j];
                left=j;
                right=j+i;
            }
        }
    }

    //cout<<left<<" "<<right<<endl;

    if(left==-1) cout<<0<<endl;
    else{
        while(1){
            int L=left,R=right;
            ll ans=diffmax-K;
            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=L;l++){
                    if(S[r]-S[l]-(r-l+M-1)/M*K>ans){
                        ans=S[r]-S[l]-(r-l+M-1)/M*K;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-(r-left+M-1)/M*K>ans){
                    ans=S[r]-S[left]-(r-left+M-1)/M*K;
                    right=r;
                }
            }

            L=left;R=right;

            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=L;l++){
                    if(S[r]-S[l]-(r-l+M-1)/M*K>ans){
                        ans=S[r]-S[l]-(r-l+M-1)/M*K;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-(r-left+M-1)/M*K>ans){
                    ans=S[r]-S[left]-(r-left+M-1)/M*K;
                    right=r;
                }
            }

            cout<<ans<<endl;
            break;
        }
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100001,INF=1<<30;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M;
    ll K;cin>>N>>M>>K;
    vector<ll> a(N),S(N+1,0);
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=1;i<=N;i++){
        S[i]=S[i-1]+a[i-1];
    }

    int left=-1,right=-1;
    ll diffmax=K;
    for(int i=1;i<=M;i++){
        for(int j=0;j+i<=N;j++){
            if(S[j+i]-S[j]>diffmax){
                diffmax=S[j+i]-S[j];
                left=j;
                right=j+i;
            }
        }
    }

    //cout<<left<<" "<<right<<endl;

    if(left==-1) cout<<0<<endl;
    else{
        while(1){
            int L=left,R=right;
            ll ans=diffmax-K;
            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=L;l++){
                    if(S[r]-S[l]-(r-l+M-1)/M*K>ans){
                        ans=S[r]-S[l]-(r-l+M-1)/M*K;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-(r-left+M-1)/M*K>ans){
                    ans=S[r]-S[left]-(r-left+M-1)/M*K;
                    right=r;
                }
            }

            cout<<ans<<endl;
            break;
        }
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100001,INF=1<<30;

int main(){

    //std::ifstream in("text.txt");
    //std::cin.rdbuf(in.rdbuf());

    int N,M;
    ll K;cin>>N>>M>>K;
    vector<ll> a(N),S(N+1,0);
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=1;i<=N;i++){
        S[i]=S[i-1]+a[i-1];
    }

    int left=-1,right=-1;
    ll diffmax=0;
    for(int i=1;i<=M;i++){
        for(int j=0;j+i<=N;j++){
            if(S[j+i]-S[j]-K>diffmax){
                diffmax=S[j+i]-S[j]-K;
                left=j;
                right=j+i;
            }
        }
    }

    //cout<<left<<" "<<right<<endl;

    if(left==-1) cout<<0<<endl;
    else{
        while(1){
            int L=left,R=right;
            ll ans=diffmax;
            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=L;l++){
                    if(S[r]-S[l]-K*(r-l+M-1)/M>ans){
                        ans=S[r]-S[l]-K*(r-l+M-1)/M;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-K*(r-left+M-1)/M>ans){
                    ans=S[r]-S[left]-K*(r-left+M-1)/M;
                    right=r;
                }
            }

            L=left;R=right;

            for(int r=R;r<=min(N,R+M-1);r++){
                for(int l=0;l<=L;l++){
                    if(S[r]-S[l]-K*(r-l+M-1)/M>ans){
                        ans=S[r]-S[l]-K*(r-l+M-1)/M;
                        left=l;
                        right=r;
                    }
                }
            }

            for(int r=min(N,R);r<=N;r++){
                if(S[r]-S[left]-K*(r-left+M-1)/M>ans){
                    ans=S[r]-S[left]-K*(r-left+M-1)/M;
                    right=r;
                }
            }

            cout<<ans<<endl;
            break;
        }
    }

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
using ULL = unsigned long long;
//using LL = __int64;
//using ULL = unsigned __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 1e100;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_998;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline T get_abs(T x) {if(x < 0) return -x; return x;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
    return res;
}

template< int mod > class Mint
{
public:
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() const {return Mint(fp(x, -1, mod));}
    Mint pow(int k) const {return Mint(fp(x, k, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}
    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline bool is_end() {while(ptr < tail && is_blank()) ptr++; return ptr >= tail;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open) throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    inline void read(int &x) {read_i(x);}
    inline void read(unsigned int &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //<utility>
    }
}reader;

const int maxn = (int) 1e5 + 5;
const int maxm = (int) 2e5 + 5;

using mint = Mint<MOD_1e9>;

struct Node
{
    mint x;
    int lch, rch;
}T[maxn * 350];
int tcnt;

int rt[maxn];

void upd(int &o, mint x, int p, int l, int r, int id)
{
    int oo = ++tcnt;
    T[oo] = T[o];
    o = oo;

    if(l == r)
    {
        T[o].x *= x;
        return ;
    }
    int mid = MID(l, r);
    if(p <= mid) upd(T[o].lch, x, p, lson);
    else upd(T[o].rch, x, p, rson);
    T[o].x = T[T[o].lch].x * T[T[o].rch].x;
}

mint que(int o, int L, int R, int l, int r, int id)
{
    if(L <= l && r <= R) return T[o].x;
    int mid = MID(l, r);
    if(R <= mid) return que(T[o].lch, L, R, lson);
    if(mid < L) return que(T[o].rch, L, R, rson);
    return que(T[o].lch, L, R, lson) * que(T[o].rch, L, R, rson);
}

int n;
int a[maxn];

int primes[maxm], low_prime[maxm];
mint po[20][maxm], inv[20][maxm];
struct item{int a, b, c;};
stack<item> stk[maxm];

void work()
{
    reader >>n;
    int mx = 0;
    for(int i = 1; i <= n; i++)
    {
        reader >>a[i];
        get_max(mx, a[i]);
    }

    {
        int m = 0;
        for(int i = 2; i <= mx; i++)
        {
            if(low_prime[i] == 0)
            {
                primes[m++] = i;
                low_prime[i] = i;

                po[0][i] = 1;
                for(int j = 1; j < 20; j++) po[j][i] = po[j - 1][i] * i;
                inv[19][i] = po[19][i].inv();
                for(int j = 18; ~j; j--) inv[j][i] = inv[j + 1][i] * i;
            }

            for(int j = 0; j < m; j++)
            {
                if(i * primes[j] > mx) break;
                low_prime[i * primes[j]] = primes[j];
                if(i % primes[j] == 0) break;
            }
        }
    }

    tcnt = 0;
    T[0].x = 1;
    T[0].lch = T[0].rch = 0;

    rt[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = a[i];
        rt[i] = rt[i - 1];

        while(x != 1)
        {
            int p = low_prime[x];
            int cnt = 0;
            while(x % p == 0)
            {
                x /= p;
                cnt++;
            }

            while(!stk[p].empty() && stk[p].top().a <= cnt)
            {
                int c = stk[p].top().a, j = stk[p].top().b, r = stk[p].top().c;
                stk[p].pop();
//                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], inv[r][p], j, 1, n, 1);
            }

            if(!stk[p].empty())
            {
                int c = stk[p].top().a, j = stk[p].top().b, r = stk[p].top().c;
                stk[p].pop();
////                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
//                upd(rt[i], inv[r][p], j, 1, n, 1);
////                upd(rt[i], mint(p).pow(c - cnt), j, 1, n, 1);
//                upd(rt[i], po[c - cnt][p], j, 1, n, 1);
                int t = r - (c - cnt);
                if(t > 0) upd(rt[i], inv[t][p], j, 1, n, 1);

                stk[p].emplace(item{c, j, c - cnt});
            }

//            upd(rt[i], mint(p).pow(cnt), i, 1, n, 1);
            upd(rt[i], po[cnt][p], i, 1, n, 1);
            stk[p].emplace(item{cnt, i, cnt});
        }
    }

    int q;
    reader >>q;
    int last = 0;
    while(q--)
    {
        int l, r;
        reader >>l >>r;

        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        if(l > r) swap(l, r);

        last = que(rt[r], l, r, 1, n, 1).x;
        printf("%d\n", last);
    }
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    reader.open_alter(1 << 24);

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
using ULL = unsigned long long;
//using LL = __int64;
//using ULL = unsigned __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 1e100;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_998;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline T get_abs(T x) {if(x < 0) return -x; return x;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
    return res;
}

template< int mod > class Mint
{
public:
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() const {return Mint(fp(x, -1, mod));}
    Mint pow(int k) const {return Mint(fp(x, k, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}
    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline bool is_end() {while(ptr < tail && is_blank()) ptr++; return ptr >= tail;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open) throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    inline void read(int &x) {read_i(x);}
    inline void read(unsigned int &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //<utility>
    }
}reader;

const int maxn = (int) 1e5 + 5;
const int maxm = (int) 2e5 + 5;

using mint = Mint<MOD_1e9>;

struct Node
{
    mint x;
    int lch, rch;
}T[maxn * 200];
int tcnt;

int rt[maxn];

void upd(int &o, mint x, int p, int l, int r, int id)
{
    int oo = ++tcnt;
    T[oo] = T[o];
    o = oo;

    if(l == r)
    {
        T[o].x *= x;
        return ;
    }
    int mid = MID(l, r);
    if(p <= mid) upd(T[o].lch, x, p, lson);
    else upd(T[o].rch, x, p, rson);
    T[o].x = T[T[o].lch].x * T[T[o].rch].x;
}

mint que(int o, int L, int R, int l, int r, int id)
{
    if(L <= l && r <= R) return T[o].x;
    int mid = MID(l, r);
    if(R <= mid) return que(T[o].lch, L, R, lson);
    if(mid < L) return que(T[o].rch, L, R, rson);
    return que(T[o].lch, L, R, lson) * que(T[o].rch, L, R, rson);
}

int n;
int a[maxn];
int primes[maxm], low_prime[maxm];
mint po[20][maxm], inv[20][maxm];
stack<tuple<int, int, int>> stk[maxn];

void work()
{
    reader >>n;
    for(int i = 1; i <= n; i++) reader >>a[i];

    int mx = 0;
    for(int i = 1; i <= n; i++) get_max(mx, a[i]);

    {
        for(int i = 0; i <= mx; i++) low_prime[i] = 0;

        int m = 0;
        for(int i = 2; i <= mx; i++)
        {
            if(low_prime[i] == 0)
            {
                primes[m++] = i;
                low_prime[i] = i;

                po[0][i] = 1;
                for(int j = 1; j < 20; j++) po[j][i] = po[j - 1][i] * i;
                inv[19][i] = po[19][i].inv();
                for(int j = 18; ~j; j--) inv[j][i] = inv[j + 1][i] * i;
            }

            for(int j = 0; j < m; j++)
            {
                if(i * primes[j] > mx) break;
                low_prime[i * primes[j]] = primes[j];
                if(i % primes[j] == 0) break;
            }
        }
    }

    for(int i = 0; i <= mx; i++)
        while(!stk[i].empty())
            stk[i].pop();
    tcnt = 0;
    T[0].x = 1;
    T[0].lch = T[0].rch = 0;

    rt[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = a[i];
        rt[i] = rt[i - 1];

        while(x != 1)
        {
            int p = low_prime[x];
            int cnt = 0;
            while(x % p == 0)
            {
                x /= p;
                cnt++;
            }

            while(!stk[p].empty() && get<0>(stk[p].top()) <= cnt)
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
//                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], inv[r][p], j, 1, n, 1);
            }

            if(!stk[p].empty())
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
//                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], inv[r][p], j, 1, n, 1);
//                upd(rt[i], mint(p).pow(c - cnt), j, 1, n, 1);
                upd(rt[i], po[c - cnt][p], j, 1, n, 1);
                stk[p].emplace(tuple<int, int, int> {c, j, c - cnt});
            }

//            upd(rt[i], mint(p).pow(cnt), i, 1, n, 1);
            upd(rt[i], po[cnt][p], i, 1, n, 1);
            stk[p].emplace(tuple<int, int, int> {cnt, i, cnt});
        }
    }

    int q;
    reader >>q;
    int last = 0;
    while(q--)
    {
        int l, r;
        reader >>l >>r;

        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        if(l > r) swap(l, r);

        last = que(rt[r], l, r, 1, n, 1).x;
        printf("%d\n", last);
    }
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    reader.open_alter(1 << 24);

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
using ULL = unsigned long long;
//using LL = __int64;
//using ULL = unsigned __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 1e100;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_998;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline T get_abs(T x) {if(x < 0) return -x; return x;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
    return res;
}

template< int mod > class Mint
{
public:
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() const {return Mint(fp(x, -1, mod));}
    Mint pow(int k) const {return Mint(fp(x, k, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}
    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline bool is_end() {while(ptr < tail && is_blank()) ptr++; return ptr >= tail;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open) throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    inline void read(int &x) {read_i(x);}
    inline void read(unsigned int &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //<utility>
    }
}reader;

const int maxn = (int) 1e5 + 5;
const int maxm = (int) 2e5 + 5;

using mint = Mint<MOD_1e9>;

struct Node
{
    mint x;
    int lch, rch;
}T[maxn * 200];
int tcnt;

int rt[maxn];

void upd(int &o, mint x, int p, int l, int r, int id)
{
    int oo = ++tcnt;
    T[oo] = T[o];
    o = oo;

    if(l == r)
    {
        T[o].x *= x;
        return ;
    }
    int mid = MID(l, r);
    if(p <= mid) upd(T[o].lch, x, p, lson);
    else upd(T[o].rch, x, p, rson);
    T[o].x = T[T[o].lch].x * T[T[o].rch].x;
}

mint que(int o, int L, int R, int l, int r, int id)
{
    if(L <= l && r <= R) return T[o].x;
    int mid = MID(l, r);
    if(R <= mid) return que(T[o].lch, L, R, lson);
    if(mid < L) return que(T[o].rch, L, R, rson);
    return que(T[o].lch, L, R, lson) * que(T[o].rch, L, R, rson);
}

int n;
int a[maxn];
int primes[maxm], low_prime[maxm];
stack<tuple<int, int, int>> stk[maxn];

void work()
{
    reader >>n;
    for(int i = 1; i <= n; i++) reader >>a[i];

    int mx = 0;
    for(int i = 1; i <= n; i++) get_max(mx, a[i]);

    {
        for(int i = 0; i <= mx; i++) low_prime[i] = 0;

        int m = 0;
        for(int i = 2; i <= mx; i++)
        {
            if(low_prime[i] == 0)
            {
                primes[m++] = i;
                low_prime[i] = i;
            }

            for(int j = 0; j < m; j++)
            {
                if(i * primes[j] > mx) break;
                low_prime[i * primes[j]] = primes[j];
                if(i % primes[j] == 0) break;
            }
        }
    }

    for(int i = 0; i <= mx; i++)
        while(!stk[i].empty())
            stk[i].pop();
    tcnt = 0;
    T[0].x = 1;
    T[0].lch = T[0].rch = 0;

    rt[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = a[i];
        rt[i] = rt[i - 1];

        while(x != 1)
        {
            int p = low_prime[x];
            int cnt = 0;
            while(x % p == 0)
            {
                x /= p;
                cnt++;
            }

            while(!stk[p].empty() && get<0>(stk[p].top()) <= cnt)
            {
                auto [c, j, r] = stk[p].top();
                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
            }

            if(!stk[p].empty())
            {
                auto [c, j, r] = stk[p].top();
                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], mint(p).pow(c - cnt), j, 1, n, 1);
            }

            upd(rt[i], mint(p).pow(cnt), i, 1, n, 1);
        }
    }

    int q;
    reader >>q;
    int last = 0;
    while(q--)
    {
        int l, r;
        reader >>l >>r;

        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        if(l > r) swap(l, r);

        last = que(rt[r], l, r, 1, n, 1).x;
        printf("%d\n", last);
    }
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    reader.open_alter(1 << 24);

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
using ULL = unsigned long long;
//using LL = __int64;
//using ULL = unsigned __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 1e100;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_998;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline T get_abs(T x) {if(x < 0) return -x; return x;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
    return res;
}

template< int mod > class Mint
{
public:
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() const {return Mint(fp(x, -1, mod));}
    Mint pow(int k) const {return Mint(fp(x, k, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}
    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline bool is_end() {while(ptr < tail && is_blank()) ptr++; return ptr >= tail;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open) throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    inline void read(int &x) {read_i(x);}
    inline void read(unsigned int &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //<utility>
    }
}reader;

const int maxn = (int) 1e5 + 5;
const int maxm = (int) 2e5 + 5;

using mint = Mint<MOD_1e9>;

struct Node
{
    mint x;
    int lch, rch;
}T[maxn * 200];
int tcnt;

int rt[maxn];

void upd(int &o, mint x, int p, int l, int r, int id)
{
    int oo = ++tcnt;
    T[oo] = T[o];
    o = oo;

    if(l == r)
    {
        T[o].x *= x;
        return ;
    }
    int mid = MID(l, r);
    if(p <= mid) upd(T[o].lch, x, p, lson);
    else upd(T[o].rch, x, p, rson);
    T[o].x = T[T[o].lch].x * T[T[o].rch].x;
}

mint que(int o, int L, int R, int l, int r, int id)
{
    if(L <= l && r <= R) return T[o].x;
    int mid = MID(l, r);
    if(R <= mid) return que(T[o].lch, L, R, lson);
    if(mid < L) return que(T[o].rch, L, R, rson);
    return que(T[o].lch, L, R, lson) * que(T[o].rch, L, R, rson);
}

int n;
int a[maxn];
int primes[maxm], low_prime[maxm];
stack<tuple<int, int, int>> stk[maxn];

void work()
{
    reader >>n;
    for(int i = 1; i <= n; i++) reader >>a[i];

    int mx = 0;
    for(int i = 1; i <= n; i++) get_max(mx, a[i]);

    {
        for(int i = 0; i <= mx; i++) low_prime[i] = 0;

        int m = 0;
        for(int i = 2; i <= mx; i++)
        {
            if(low_prime[i] == 0)
            {
                primes[m++] = i;
                low_prime[i] = i;
            }

            for(int j = 0; j < m; j++)
            {
                if(i * primes[j] > mx) break;
                low_prime[i * primes[j]] = primes[j];
                if(i % primes[j] == 0) break;
            }
        }
    }

    for(int i = 0; i <= mx; i++)
        while(!stk[i].empty())
            stk[i].pop();
    tcnt = 0;
    T[0].x = 1;
    T[0].lch = T[0].rch = 0;

    rt[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = a[i];
        rt[i] = rt[i - 1];

        while(x != 1)
        {
            int p = low_prime[x];
            int cnt = 0;
            while(x % p == 0)
            {
                x /= p;
                cnt++;
            }

            while(!stk[p].empty() && get<0>(stk[p].top()) <= cnt)
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
            }

            if(!stk[p].empty())
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], mint(p).pow(c - cnt), j, 1, n, 1);
                stk[p].emplace(tuple<int, int, int> {c, j, c - cnt});
            }

            upd(rt[i], mint(p).pow(cnt), i, 1, n, 1);
            stk[p].emplace(tuple<int, int, int> {cnt, i, cnt});
        }
    }

    int q;
    reader >>q;
    int last = 0;
    while(q--)
    {
        int l, r;
        reader >>l >>r;

        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        if(l > r) swap(l, r);

        last = que(rt[r], l, r, 1, n, 1).x;
        printf("%d\n", last);
    }
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    reader.open_alter(1 << 24);

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
using ULL = unsigned long long;
//using LL = __int64;
//using ULL = unsigned __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 1e100;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_998;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline T get_abs(T x) {if(x < 0) return -x; return x;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
    return res;
}

template< int mod > class Mint
{
public:
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() const {return Mint(fp(x, -1, mod));}
    Mint pow(int k) const {return Mint(fp(x, k, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}
    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline bool is_end() {while(ptr < tail && is_blank()) ptr++; return ptr >= tail;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open) throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    inline void read(int &x) {read_i(x);}
    inline void read(unsigned int &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //<utility>
    }
}reader;

const int maxn = (int) 1e5 + 5;
const int maxm = (int) 2e5 + 5;

using mint = Mint<MOD_1e9>;

struct Node
{
    mint x;
    int lch, rch;
}T[maxn * 400];
int tcnt;

int rt[maxn];

void upd(int &o, mint x, int p, int l, int r, int id)
{
    int oo = ++tcnt;
    T[oo] = T[o];
    o = oo;

    if(l == r)
    {
        T[o].x *= x;
        return ;
    }
    int mid = MID(l, r);
    if(p <= mid) upd(T[o].lch, x, p, lson);
    else upd(T[o].rch, x, p, rson);
    T[o].x = T[T[o].lch].x * T[T[o].rch].x;
}

mint que(int o, int L, int R, int l, int r, int id)
{
    if(L <= l && r <= R) return T[o].x;
    int mid = MID(l, r);
    if(R <= mid) return que(T[o].lch, L, R, lson);
    if(mid < L) return que(T[o].rch, L, R, rson);
    return que(T[o].lch, L, R, lson) * que(T[o].rch, L, R, rson);
}

int n;
int a[maxn];
int primes[maxm], low_prime[maxm];
mint po[20][maxm], inv[20][maxm];
stack<tuple<int, int, int>> stk[maxn];

void work()
{
    reader >>n;
    for(int i = 1; i <= n; i++) reader >>a[i];

    int mx = 0;
    for(int i = 1; i <= n; i++) get_max(mx, a[i]);

    {
        for(int i = 0; i <= mx; i++) low_prime[i] = 0;

        int m = 0;
        for(int i = 2; i <= mx; i++)
        {
            if(low_prime[i] == 0)
            {
                primes[m++] = i;
                low_prime[i] = i;

                po[0][i] = 1;
                for(int j = 1; j < 20; j++) po[j][i] = po[j - 1][i] * i;
                inv[19][i] = po[19][i].inv();
                for(int j = 18; ~j; j--) inv[j][i] = inv[j + 1][i] * i;
            }

            for(int j = 0; j < m; j++)
            {
                if(i * primes[j] > mx) break;
                low_prime[i * primes[j]] = primes[j];
                if(i % primes[j] == 0) break;
            }
        }
    }

    for(int i = 0; i <= mx; i++)
        while(!stk[i].empty())
            stk[i].pop();
    tcnt = 0;
    T[0].x = 1;
    T[0].lch = T[0].rch = 0;

    rt[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = a[i];
        rt[i] = rt[i - 1];

        while(x != 1)
        {
            int p = low_prime[x];
            int cnt = 0;
            while(x % p == 0)
            {
                x /= p;
                cnt++;
            }

            while(!stk[p].empty() && get<0>(stk[p].top()) <= cnt)
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
//                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], inv[r][p], j, 1, n, 1);
            }

            if(!stk[p].empty())
            {
                auto [c, j, r] = stk[p].top();
                stk[p].pop();
//                upd(rt[i], mint(p).pow(r).inv(), j, 1, n, 1);
                upd(rt[i], inv[r][p], j, 1, n, 1);
//                upd(rt[i], mint(p).pow(c - cnt), j, 1, n, 1);
                upd(rt[i], po[c - cnt][p], j, 1, n, 1);
                stk[p].emplace(tuple<int, int, int> {c, j, c - cnt});
            }

//            upd(rt[i], mint(p).pow(cnt), i, 1, n, 1);
            upd(rt[i], po[cnt][p], i, 1, n, 1);
            stk[p].emplace(tuple<int, int, int> {cnt, i, cnt});
        }
    }

    int q;
    reader >>q;
    int last = 0;
    while(q--)
    {
        int l, r;
        reader >>l >>r;

        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        if(l > r) swap(l, r);

        last = que(rt[r], l, r, 1, n, 1).x;
        printf("%d\n", last);
    }
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    reader.open_alter(1 << 24);

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}