#include <iostream>
#include <vector>
#include <math.h>
#include <iomanip>      // std::setprecision

using namespace std;

int main()
{
    int hh,mm,h,d,c,n,t;
    cin >>hh;
    cin >>mm;
    cin >>h;
    cin >>d;
    cin >>c;
    cin >>n;
    cout.precision(4);
    if (hh>=20)
        cout <<fixed<<round((double)h/n)*(double)c*0.8;
    else{
        t=(19-hh)*60+60-mm;
        double m1=round(((double)h+(double)(t*d))/n)*(double)c*0.8;
        double m2=round((double)h/n)*(double)c;
        if (m1<m2)
            cout <<fixed<<m1;
        else
            cout <<fixed<<m2;
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

double min_m(double a,double b){
    return a>b?b:a;
}
int main(){
    int hh,mm,H,D,C,N,time;
    double ans;
    while(cin>>hh>>mm){
        cin>>H>>D>>C>>N;
        if(hh>=20){
            if(H%N==0) ans = (H/N)*C*0.8;
            else ans = (H/N+1)*C*0.8;
            printf("%.6lf\n",ans);
        }else{
            time = (19-hh)*60 + (60-mm);
            //cout<<time<<endl;
            if(H%N==0) ans = (H/N)*C*1.0;
            else ans = (H/N+1)*C*1.0;
            if((H+time*D)%N==0)ans = min_m((H+time*D)/N*C*0.8,ans);
            else ans = min_m(((H+time*D)/N+1)*C*0.8,ans);

            printf("%.6lf\n",ans);
        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

char hh[2];
char mm[2];
double min_m(double a,double b){
    return a>b?b:a;
}
int main(){
    int H,D,C,N,time;
    double ans;
    while(cin>>hh>>mm){
        cin>>H>>D>>C>>N;
        if(hh[0]>='2'){
            if(H%N==0) ans = (H/N)*C*0.8;
            else ans = (H/N+1)*C*0.8;
            printf("%.6lf\n",ans);
        }else{
            time = ((1-hh[0]+'0')*10+(9-hh[1]+'0'))*60 + (5-mm[0]+'0')*10 + (10-mm[1]+'0');
            //cout<<time<<endl;
            if(H%N==0) ans = (H/N)*C*1.0;
            else ans = (H/N+1)*C*1.0;
            if((H+time*D)%N==0)ans = min_m((H+time*D)/N*C*0.8,ans);
            else ans = min_m(((H+time*D)/N+1)*C*0.8,ans);

            printf("%.6lf\n",ans);
        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

char hh[2];
char mm[2];

int main(){
    int H,D,C,N,time;
    double ans;
    while(cin>>hh>>mm>>H>>D>>C>>N){
        if(hh[0]>='2'){
            if(H%N==0) ans = (H/N)*C*0.8;
            else ans = (H/N+1)*C*0.8;
            printf("%.6lf\n",ans);
        }else{
            time = ((1-hh[0]+'0')*10+(9-hh[1]+'0'))*60 + (5-mm[0]+'0')*10 + (10-mm[1]+'0');
            //cout<<time<<endl;
            if(H%N==0) ans = (H/N)*C*1.0;
            else ans = (H/N+1)*C*1.0;
            if((H+time*D)%N==0)ans = min((H+time*D)/N*C*0.8,ans);
            else ans = min(((H+time*D)/N+1)*C*0.8,ans);

            printf("%.6lf\n",ans);
        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

char hh[2];
char mm[2];

int main(){
    int H,D,C,N,time;
    double ans;
    while(cin>>hh>>mm>>H>>D>>C>>N){
        if(hh[0]>='2'){
            if(H/N==0) ans = (H/N)*C;
            else ans = (H/N+1)*C;
        }else{
            time = ((1-hh[0]+'0')*10+(10-hh[1]+'0'))*60 + (5-mm[0]+'0')*10 + (10-mm[1]+'0');
            cout<<time<<endl;
        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

char hh[2];
char mm[2];
double min_m(double a,double b){
    return a>b?b:a;
}
int main(){
    int H,D,C,N,time;
    double ans;
    while(cin>>hh>>mm>>H>>D>>C>>N){
        if(hh[0]>='2'){
            if(H%N==0) ans = (H/N)*C*0.8;
            else ans = (H/N+1)*C*0.8;
            printf("%.6lf\n",ans);
        }else{
            time = ((1-hh[0]+'0')*10+(9-hh[1]+'0'))*60 + (5-mm[0]+'0')*10 + (10-mm[1]+'0');
            //cout<<time<<endl;
            if(H%N==0) ans = (H/N)*C*1.0;
            else ans = (H/N+1)*C*1.0;
            if((H+time*D)%N==0)ans = min_m((H+time*D)/N*C*0.8,ans);
            else ans = min_m(((H+time*D)/N+1)*C*0.8,ans);

            printf("%.6lf\n",ans);
        }
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    double hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        if(mm>0)
            res1--;
        res1=res1*60;
        res1+=(59-mm);
        res1=h+(res1*d);

        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
               break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
           if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }
if(res2<res4)
    printf("%0.4lf",res2);
    else
            printf("%0.4lf",res4);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    double hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        if(mm>0)
            res1--;
        res1=res1*60;
        if(mm>0)
        {res1+=(59-mm);
        res1++;}
        res1=h+(res1*d);

        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
               break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
           if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }

if(res2<res4)
    printf("%0.4lf",res2);
    else
            printf("%0.4lf",res4);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    double hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        if(mm>0)
            res1--;
        res1=res1*60;
        if(mm>0)
        res1+=(59-mm);
        res1=h+(res1*d);

        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
               break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
           if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }
if(res2<res4)
    printf("%0.4lf",res2);
    else
            printf("%0.4lf",res4);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    double hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        if(mm>0)
            res1--;
        res1=res1*60;
        if(mm>0)
        res1+=(59-mm);
        res1++;
        res1=h+(res1*d);

        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
               break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
           if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }

if(res2<res4)
    printf("%0.4lf",res2);
    else
            printf("%0.4lf",res4);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    float hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    // res3=res3/n;
      //  res4=res3*c;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        res1=res1*60;
        res1+=mm;
        res1=h+(res1*d);
       //res1=res1/n;
        //res2=res1*c;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }

    printf("%0.4f",min(res2,res4));
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    double hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        if(mm>0)
            res1--,res1+=(59-mm);
        res1=res1*60;
        res1=h+(res1*d);

        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
               break;
        }
        res2 = (res2*80)/100;
    }
    else
    {
        res1=h;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
           if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }
if(res2<res4)
    printf("%0.4lf",res2);
    else
            printf("%0.4lf",res4);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    float hh,mm,h,d,c,n,res1=0,res2=0,res3=0,res4=0;
    cin>>hh>>mm>>h>>d>>c>>n;
    res3 = h;
    // res3=res3/n;
      //  res4=res3*c;
    while(res3>0)
    {
        res4+=c;
        res3-=n;
    }
    if(hh<20)
    {
        res1=20-hh;
        res1=res1*60;
        res1+=mm;
        res1=h+(res1*d);
       //res1=res1/n;
        //res2=res1*c;
        while(res1>0)
        {
            res2+=c;
            res1-=n;
            if(((res2*80)/100)>res4)
                break;
        }
        res2 = (res2*80)/100;
    }

    printf("%0.4f",min(res2,res4));
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll;

int main()
{
    ll hh,mm,h,d,c,n;
    cin >> hh >> mm >> h >> d >> c >> n;
    if(hh < 20)
    {
        double cur = (h / n + (h%n != 0))*1.0 * c;
        double cut = (((20 * 60  - hh*60 - mm)*d + h)/n + (((20 * 60  - hh*60 - mm)*d+h)%n != 0)) * c * 0.8;
        printf("%.4f\n",min(cur,cut));
    }
    else
        printf("%.4f\n", (h / n + (h%n != 0))*0.8 * c);

}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#define ll long long
using namespace std;

const int maxn = 60005;

int main(){

    int h,m;
    scanf("%d %d",&h,&m);
    int H,D,N;
    double C;
    cin>>H>>D>>C>>N;

    m+=h*60;
    if(m>60*20)
        m=24*60-m+20*60;
    else
        m=20*60-m;

    double p1,p2;
    p1=ceil(1.0*H/N)*C;
    p2=ceil( (H+m*D)/N ) *C*0.8;
    printf("%.4f\n",std::min(p1,p2));

}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#define ll long long
using namespace std;

const int maxn = 60005;

int main(){

    int h,m;
    scanf("%d %d",&h,&m);
    int H,D,N;
    double C;
    cin>>H>>D>>C>>N;

    m+=h*60;
    if(m>60*20)
        m=24*60-m+20*60;
    else
        m=20*60-m;

    double p1,p2;
    p1=ceil(1.0*H/N)*C;
    p2=ceil( 1.0*(H+m*D)/N ) *C*0.8;
    printf("%.4f\n",std::min(p1,p2));

}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#define ll long long
using namespace std;

const int maxn = 60005;

int main(){

    int h,m;
    scanf("%d %d",&h,&m);
    int H,D,N;
    double C;
    cin>>H>>D>>C>>N;

    m+=h*60;
    if(m>60*20){
        printf("%.4f\n",ceil(1.0*H/N)*C*0.8);
        return 0;
    }
    else
        m=20*60-m;

    double p1,p2;
    p1=ceil(1.0*H/N)*C;
    p2=ceil( 1.0*(H+m*D)/N ) *C*0.8;
    printf("%.4f\n",std::min(p1,p2));

}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}

int main()
{
	int t,m,h,d,c,n;
	cin>>t>>m>>h>>d>>c>>n;
	double ans;
	if(t>=20)
	{
		ans=ceil(1.0*h/n)*c*0.8;
	}
	else
	{
		t=20-t-1;
		t*=60;
		t+=60-m;
		double sum=1.0*ceil((1.0*h+t*d)/n)*c*0.8;
		ans=1.0*ceil(1.0*h/n)*c;
	//	cout<<ans<<"**"<<sum<<endl;
		ans=min(ans,sum);
	}
	printf("%.4lf\n",ans);
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}

int main()
{
	int t,m,h,d,c,n;
	cin>>t>>m>>h>>d>>c>>n;
	double ans;
	if(t>=20)
	{
		t-=20;
		t*=60;
		t+=m;
		ans=1.0*h/n*c*0.8;
	}
	else
	{
		t=20-t-1;
		t*=60;
		t+=60-m;
		double sum=1.0*ceil((1.0*h+t*d)/n)*c*0.8;
		ans=1.0*ceil(1.0*h/n)*c;
	//	cout<<ans<<"**"<<sum<<endl;
		ans=min(ans,sum);
	}
	printf("%.4lf\n",ans);
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}

int main()
{
	int t,m,h,d,c,n;
	cin>>t>>m>>h>>d>>c>>n;
	double ans;
	if(t>=20)
	{
		t-=20;
		t+=m;
		ans=1.0*h/n*c;
	}
	else
	{
		t=20-t;
		t*=60;
		if(m)
		t+=60-m;
		double sum=1.0*ceil((1.0*h+t*d)/n)*c*0.8;
		ans=1.0*ceil(1.0*h/n)*c;
		ans=min(ans,sum);
	}
	printf("%.4lf\n",ans);
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}

int main()
{
	int t,m,h,d,c,n;
	cin>>t>>m>>h>>d>>c>>n;
	double ans;
	if(t>=20)
	{
		t-=20;
		t+=m;
		ans=1.0*h/n*c;
	}
	else
	{
		t=20-t-1;
		t*=60;
		t+=60-m;
		double sum=1.0*ceil((1.0*h+t*d)/n)*c*0.8;
		ans=1.0*ceil(1.0*h/n)*c;
	//	cout<<ans<<"**"<<sum<<endl;
		ans=min(ans,sum);
	}
	printf("%.4lf\n",ans);
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define for(i,a,b) for(int i=a;i<=b;i++)
int main() {
	int hh,mm,h,d,n,c,cmp=0;
	double ans1=1e9,ans2=0;
	cin>>hh>>mm>>h>>d>>c>>n;
	if(h%n==0)ans2=h/n*c;
	else ans2=(h/n+1)*c;
	if(hh<20) {
		cmp=(19-hh)*60+60-mm;
		if((h+cmp*d)%n==0)ans1=(h+cmp*d)/n*c;
		else ans1=((h+cmp*d)/n+1)*c;
		ans1*=0.8;
	} else ans2*=0.8;
	if(ans1-ans2>0)printf("%.4f\n",ans2);
	else printf("%.4f\n",ans1);
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define for(i,a,b) for(int i=a;i<=b;i++)
int main() {
	int hh,mm,h,d,n,c,cmp=0;
	double ans1=1e9,ans2=0;
	cin>>hh>>mm>>h>>d>>c>>n;
	if(h%n==0)ans2=h/n*c;
	else ans2=(h/n+1)*c;
	if(hh<20) {
		cmp=(19-hh)*60+60-mm;
		if(cmp%n==0)ans1=(h+cmp*d)/n*c;
		else ans1=((h+cmp*d)/n+1)*c;
		ans1*=0.8;
	} else ans2*=0.8;
	if(ans1-ans2>0)printf("%.4f\n",ans2);
	else printf("%.4f\n",ans1);
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define for(i,a,b) for(int i=a;i<=b;i++)
int main(){
	int hh,mm,h,d,n,c,cmp=0;
	double ans1=1e9,ans2=0;
	cin>>hh>>mm>>h>>d>>c>>n;
	if(hh<20){
		cmp=(19-hh)*60+60-mm;
		if(cmp%n==0)ans1=(h+cmp*d)/n*c;
		else ans1=((h+cmp*d)/n+1)*c;
		ans1*=0.8;
	}
	if(h%n==0)ans2=h/n*c;
	else ans2=(h/n+1)*c;
	if(ans1-ans2>0)printf("%.4f\n",ans2);
	else printf("%.4f\n",ans1);
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    double hh,mm, h,d,c,n;
    cin >> hh >> mm >> h >> d >> c >> n;

    double time = 60*hh;
    time+=mm;
    double res1 = 1000000000.0000;
    double res2 = 1000000000.0000;

    res1 = ceil(h/n)*c;
    if(time<1200)
    {
        time = 1200 - time;
        h += (time * d);
        res2 = ceil(h/n) * c;
        res2 *= 0.8;
    }
    else
    {
        res1 *= 0.8;
    }
    cout << fixed << setprecision(4) << min(res1, res2) << endl;
}


#include<bits/stdc++.h>

using namespace std;

double checkers1(double h, double n, double c)
{
    double res1 = ceil(h/n) * c;
    return res1;
}

double checkers2(double h, double n, double c)
{
    double check = ceil(h/n);
    double res1 = check*c;
    double res2 = (check*c) * (0.2);
    return (res1-res2);
}

int main()
{
    int hh,mm;
    cin >> hh >> mm;
    double h, d, c, n;
    cin >> h >> d >> c >> n;
    if(hh>=20)
    {
        cout << fixed << setprecision(4) << checkers2(h,n,c) <<  endl;
    }
    else if(n == 1 && d == 1)
    {
        int rem = (20-(hh+1)) * 60;
        rem += 60 - mm;
        double wr = rem + h;
        double res1 = wr*c;
        double res2 = (wr*c) * (0.2);
        cout << fixed << setprecision(4) << res1 - res2 << endl;
    }
    else
    {
        double right = h/n;
        int totaltime = hh*60;
        totaltime += mm;
        if(totaltime + right < 1200)
        {
            double res1 = ceil((h/n))*c;
            cout << fixed << setprecision(4) << res1 << endl;
        }
        else
        {
            int remaining = (totaltime + right) - 1200;
            double res1 = checkers1(h,n,c);
            h = h - (remaining * n);
            res1 += checkers2(h,n,c);
            cout << fixed << setprecision(4) << res1 << endl;
        }
    }
}


#include<bits/stdc++.h>

using namespace std;

double checkers1(double h, double n, double c)
{
    double res1 = ceil(h/n) * c;
    return res1;
}

double checkers2(double h, double n, double c)
{
    double check = ceil(h/n);
    double res1 = check*c;
    double res2 = (check*c) * (0.2);
    return (res1-res2);
}

int main()
{
    int hh,mm;
    cin >> hh >> mm;
    double h, d, c, n;
    cin >> h >> d >> c >> n;
    double right = h/n;
    int totaltime = hh*60;
    totaltime += mm;
    if(hh>=20)
    {
        cout << fixed << setprecision(4) << checkers2(h,n,c) <<  endl;
    }
     else if(n == 1 && d == 1)
    {
        int rem = (20-(hh+1)) * 60;
        rem += 60 - mm;
        double wr = rem + h;
        double res1 = wr*c;
        double res2 = (wr*c) * (0.2);
        cout << fixed << setprecision(4) << res1 - res2 << endl;
    }
    else{
        double re2 = checkers1(h, n, c);
        double re = checkers2(h,n,c);
        cout << fixed << setprecision(4) << min(re2, re) << endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    double hh,mm, h,d,c,n;
    cin >> hh >> mm >> h >> d >> c >> n;

    double time = 60*hh;
    time+=mm;
    double res1 = 100000.0000;
    double res2 = 100000.0000;

    res1 = ceil(h/n)*c;
    if(time<1200)
    {
        time = 1200 - time;
        h += (time * d);
        res2 = ceil(h/n) * c;
        res2 *= 0.8;
    }
    else
    {
        res1 *= 0.8;
    }
    cout << fixed << setprecision(4) << min(res1, res2) << endl;
}


#include<bits/stdc++.h>

using namespace std;

double checkers1(double h, double n, double c)
{
    double res1 = ceil(h/n) * c;
    return res1;
}

double checkers2(double h, double n, double c)
{
    double check = ceil(h/n);
    double res1 = check*c;
    double res2 = (check*c) * (0.2);
    return (res1-res2);
}

int main()
{
    int hh,mm;
    cin >> hh >> mm;
    double h, d, c, n;
    cin >> h >> d >> c >> n;
    double right = h/n;
    int totaltime = hh*60;
    totaltime += mm;
    if(hh>=20)
    {
        cout << fixed << setprecision(4) << checkers2(h,n,c) <<  endl;
    }
    else if(totaltime + right < 1200)
    {
        double res1 = ceil((h/n))*c;
        cout << fixed << setprecision(4) << res1 << endl;
    }
     else if(n == 1 && d == 1)
    {
        int rem = (20-(hh+1)) * 60;
        rem += 60 - mm;
        double wr = rem + h;
        double res1 = wr*c;
        double res2 = (wr*c) * (0.2);
        cout << fixed << setprecision(4) << res1 - res2 << endl;
    }
    else if(totaltime + right >= 1200)
    {
        int remaining = (totaltime + right) - 1200;
        double res1 = checkers1(h,n,c);
        h = h - (remaining * n);
        res1 += checkers2(h,n,c);
        cout << fixed << setprecision(4) << res1 << endl;
    }
    else
    {
        int rem = (20-(hh+1)) * 60;
        rem += 60 - mm;
        double wr = rem + h;
        double re2 = checkers1(wr, n, c);
        double re = checkers2(wr,n,c);
        cout << fixed << setprecision(4) << min(re2, re) << endl;
    }
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int main()
{
	double hh, mm, h, d, c, n;

	cin >> hh >> mm >> h >> d >> c >> n;

	if (hh >= 20 && hh <= 23)
	{
	    double r = h / n;
		if (int(n) * int(r) != int(h))
			r = int(r) + 1.0;
		printf("%.9lf", r * c * 0.8);
	}
	else
	{
		double a = 60 - mm;
		a += (20 - hh - 1) * 60;
		double b = h + a * d;
		double k = b / n;
		if (int(n) * int(k) != int(b))
			k = int(k) + 1.0;
		double r = h / n;
		if (int(n) * int(r) != int(h))
			r = int(r) + 1.0;
		//cout << r;
		printf("%.9lf", min(r * c, k * c * 0.8));
	}
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int main()
{
	double hh, mm, h, d, c, n;

	cin >> hh >> mm >> h >> d >> c >> n;

	if (hh >= 20 && hh <= 23)
		printf("%.9lf", h / n * c * 80 / 100);
	else
	{
		double a = 60 - mm;
		a += (20 - hh - 1) * 60;
		double b = h + a * d;
		printf("%.9lf", min(h / n * c, b / n * c * 80 / 100));
	}
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int main()
{
	double hh, mm, h, d, c, n;

	cin >> hh >> mm >> h >> d >> c >> n;

	if (hh >= 20 && hh <= 23)
		printf("%.9lf", h / n * c * 80 / 100);
	else
	{
		double a = 60 - mm;
		a += (20 - hh - 1) * 60;
		double b = h + a * d;
		double k = b / n;
		if (int(n) * int(k) != int(b))
			k = int(k) + 1.0;
		double r = h / n;
		if (int(n) * int(r) != int(h))
			r = int(r) + 1.0;
		//cout << r;
		printf("%.9lf", min(r * c, k * c * 0.8));
	}
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	int hh, mm, h, d, n, t;
	float sale, c, now;
	cin >> hh >> mm >> h >> d >> c >> n;
		now = ( (h-1)/n + 1)*c;
		if (hh < 20) t = (20 - hh)*60 - mm;
		h += t*d;
		c *= 0.8;
		sale = ( (h-1)/n + 1)*c;
		cout << fixed << setprecision(4) << min(now, sale);
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main(){
	int hh, mm, h, d, n, t;
	float sale, c, now;
	cin >> hh >> mm >> h >> d >> c >> n;
	    double y=h/n;
	    int x=ceil(y);
		now = x*c;
		if (hh < 20) t = (20 - hh)*60 - mm;
		h += t*d;
		c *= 0.8;
		sale = x*c;
		cout << fixed << setprecision(4) << min(now, sale);
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main(){
	int hh, mm, h, d, n, t;
	float sale, c, now;
	cin >> hh >> mm >> h >> d >> c >> n;
		now = (ceil(h/n))*c;
		if (hh < 20) t = (20 - hh)*60 - mm;
		h += t*d;
		c *= 0.8;
		sale = (ceil(h/n))*c;
		cout << fixed << setprecision(4) << min(now, sale);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   double currentTime1 = ((ceil(H/N)*C)*80.0/100);

   if(h>20) cout <<currentTime1;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(3);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-h)*60.0)+m;
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   if(h>20) cout <<currentTime;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   double currentTime1 = ((ceil(H/N)*C)*80.0/100);

   if(h>=20&&h<=23+(59/60)) cout <<currentTime1;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   if(h>20) cout <<currentTime;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   double currentTime1 = ((ceil(H/N)*C)*80.0/100);

   if(h>20&&h<23+(59/60)) cout <<currentTime1;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-h)*60.0)+m;
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   if(h>20) cout <<currentTime;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));

   if(h>20) cout <<forEight;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60.0)))*60.0);
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   double currentTime1 = ((ceil(H/N)*C)*80.0/100);

   if(h>=20&&h<23+(59/60)) cout <<currentTime1;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<< fixed<<setprecision(4);
   double h,m;
   cin>>h>>m;
   double H,D,C,N;
   cin>>H>>D>>C>>N;

   double det = (abs(20.0-(h+(m/60)*60.0)))+m;
   double forEight= (ceil((H+(D*det))/N)*C)*80.0/100.0;
   double currentTime = ((ceil(H/N)*C));
   if(h>20) cout <<currentTime;
   else  cout <<min(forEight,currentTime);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;

int hh,mm,h,d,c,n;

int calc_time(int hh1, int mm1, int hh2, int mm2){
	if (hh1 == hh2)
		return mm2 - mm1;
	return mm2 + 60*(hh2-hh1) + mm1;
}

int main(){
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	float res = 0;
	cout << ((calc_time(hh,mm,20,0))) << endl;
	if (hh >= 20 && hh <= 23){
		res = ceil((h*1.0) / n ) * c * 0.8;
	}
	else{
		res = min(ceil((h*1.0) / n ) * c, (ceil(calc_time(hh,mm,20,0)*d + h)*1.0 / n)*c*0.8);
	}

	printf("%.4f", res);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int hh,mm,h,d,c,n; scanf("%d%d%d%d%d%d",&hh,&mm,&h,&d,&c,&n);
	double ans;
	if(hh>=20)ans=(h+n-1)/n*c*0.8;
	else{
		ans=(h+n-1)/n*c; int m=(19-hh)*60+(60-mm);
		ans=min(ans,(h+m*d+n-1)/n*c*0.8);
	}
	printf("%.9f\n",ans); return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;

int hh,mm,h,d,c,n;

int calc_time(int hh1, int mm1, int hh2, int mm2){
	if (hh1 == hh2)
		return mm2 - mm1;
	return mm2 + 60*(hh2-hh1) + mm1;
}

int main(){
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	float res = 0;
	cout << ((calc_time(hh,mm,20,0))) << endl;
	if (hh >= 20 && hh <= 23){
		res = ceil((h*1.0) / n ) * c * 0.8;
	}
	else{
		res = min(ceil((h*1.0) / n ) * c, (ceil(calc_time(hh,mm,20,0)*d + h)*1.0 / n)*c*0.8);
	}

	printf("%.3f",res);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;

int hh,mm,h,d,c,n;

int calc_time(int hh1, int mm1, int hh2, int mm2){
	if (hh1 == hh2)
		return mm2 - mm1;
	return mm2 + 60*(hh2-hh1) + mm1;
}

int main(){
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	float res = 0;
	//cout << ((calc_time(hh,mm,20,0))) << endl;
	if (hh >= 20 && hh <= 23){
		res = ceil((h*1.0) / n ) * c * 0.8;
	}
	else{
		res = min(ceil((h*1.0) / n ) * c, (ceil(calc_time(hh,mm,20,0)*d + h)*1.0 / n)*c*0.8);
	}

	printf("%.4f",res);
    return 0;
}


// sandeep_007

#include <bits/stdc++.h>

using namespace std;

#define PI acos(-1)
#define mod 1000000007
#define ll long long
#define nitro std::ios_base::sync_with_stdio (false); cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define INV 333333336
#define arr(a,n) for(int i=0;i<n;i++) cin >> a[i];
#define all(v) (v).begin(),(v).end()
#define br cout<<endl

int main(){
	nitro;

	#ifdef DBG
		freopen("in","r",stdin);
	#endif

	// code begins from  here.
	int h,m; cin >> h >> m;
	int H,D,C,N; cin >> H >> D >> C >> N;
	cout << fixed;
	int hr = h;
	int min = 0;
	int pre = m;
	while(hr < 20){
		hr++;
		min+=(60-pre);
		pre = 0;
	}
	double a = H;
	int count = H/N;
	if(H%N != 0) count++;

	double ans1;
	ans1 = count*C;
	if(h >= 20)
		ans1 -= 0.2*(double)ans1;
	a = H+D*min;
	count = a/N;
	if(H%N != 0) count++;
	double ans2;
	ans2 = count*C;
	ans2 -= 0.2*(double)ans2;
	if(ans1 < ans2){
		cout << setprecision(10) << ans1;br;}
	else{
		cout << setprecision(10) << ans2;br;}

	return 0;
}


// sandeep_007

#include <bits/stdc++.h>

using namespace std;

#define PI acos(-1)
#define mod 1000000007
#define ll long long
#define nitro std::ios_base::sync_with_stdio (false); cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define INV 333333336
#define arr(a,n) for(int i=0;i<n;i++) cin >> a[i];
#define all(v) (v).begin(),(v).end()
#define br cout<<endl

int main(){
	nitro;

	#ifdef DBG
		freopen("in","r",stdin);
	#endif

	// code begins from  here.
	int h,m; cin >> h >> m;
	int H,D,C,N; cin >> H >> D >> C >> N;
	cout << fixed;
	int hr = h;
	int min = 0;
	int pre = m;
	while(hr < 20){
		hr++;
		min+=(60-pre);
		pre = 0;
	}
	double a = H;
	int count = H/N;
	if(H%N != 0) count++;

	double ans1;
	ans1 = count*C;
	if(h >= 20)
		ans1 -= 0.2*(double)ans1;
	a = H+D*min;
	count = a/N;
	if(a%N != 0) count++;
	double ans2;
	ans2 = count*C;
	ans2 -= 0.2*(double)ans2;
	if(ans1 < ans2){
		cout << setprecision(10) << ans1;br;}
	else{
		cout << setprecision(10) << ans2;br;}

	return 0;
}


// sandeep_007

#include <bits/stdc++.h>

using namespace std;

#define PI acos(-1)
#define mod 1000000007
#define ll long long
#define nitro std::ios_base::sync_with_stdio (false); cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define INV 333333336
#define arr(a,n) for(int i=0;i<n;i++) cin >> a[i];
#define all(v) (v).begin(),(v).end()
#define br cout<<endl

int main(){
	nitro;

	#ifdef DBG
		freopen("in","r",stdin);
	#endif

	// code begins from  here.
	int h,m; cin >> h >> m;
	int H,D,C,N; cin >> H >> D >> C >> N;
	cout << fixed;
	int hr = h;
	int min = 0;
	int pre = m;
	while(hr < 20){
		hr++;
		min+=(60-pre);
		pre = 0;
	}
	double a = H;
	int count = H/N;
	if(H%N != 0) count++;

	double ans1;
	ans1 = count*C;
	if(h >= 20)
		ans1 -= 0.2*(double)ans1;
	a = H+D*min;
	count = a/N;
	if(((int)a)%N != 0) count++;
	double ans2;
	ans2 = count*C;
	ans2 -= 0.2*(double)ans2;
	if(ans1 < ans2){
		cout << setprecision(10) << ans1;br;}
	else{
		cout << setprecision(10) << ans2;br;}

	return 0;
}


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

main()
{
    double h,m,H,D,C,N,j;
    double a,b;

    cin>>h>>m;
    cin>>H>>D>>C>>N;
    cout.precision(4);
    if(h>=20)
    {
        j=.2*C;
        j=C-j;
    if(fmod(H,N)==0)
    a=(H/N)*j;
    else
    a=int((H/N)+1)*j;
    cout<<fixed<<a;
    }
    else
    {
    if(fmod(H,N)==0)
    a=(H/N)*C;
    else
    a=int((H/N)+1)*C;

    h=(h*60)+m;
    h=1200-h;

    H=H+(h*D);
    m=0.2*C;
    C=C-m;

    if(fmod(H,N)==0)
        b=(H/N)*C;
    else
        b=int((H/N)+1)*C;

    cout<<fixed<<min(a,b);
    }
}


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

main()
{
    double h,m,H,D,C,N;
    double a,b;

    cin>>h>>m;
    cin>>H>>D>>C>>N;
    cout.precision(4);
    if(fmod(H,N)==0)
    a=(H/N)*C;
    else
    a=int((H/N)+1)*C;

    h=(h*60)+m;
    h=1200-h;

    H=H+(h*D);
    m=0.2*C;
    C=C-m;

    if(fmod(H,N)==0)
        b=(H/N)*C;
    else
        b=int((H/N)+1)*C;

    cout<<fixed<<min(a,b);
}


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

main()
{
    double h,m,H,D,C,N;
    double a,b;

    cin>>h>>m;
    cin>>H>>D>>C>>N;
    cout.precision(4);
    if(fmod(H,N)==0)
    a=(H/N)*C;
    else
    a=int((H/N)+1)*C;

    h=(h*60)+m;
    h=1200-h;

    H=H+(h*D);
    m=0.2*C;
    C=C-m;
    cout<<fixed<<a<<endl;
    if(fmod(H,N)==0)
        b=(H/N)*C;
    else
        b=((H/N)+1)*C;

    cout<<fixed<<min(a,b);
}


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

main()
{
    double h,m,H,D,C,N;
    double a,b;

    cin>>h>>m;
    cin>>H>>D>>C>>N;

    if(fmod(H,N)==0)
    a=(H/N)*C;
    else
    a=((H/N)+1)*C;

    h=(h*60)+m;
    h=1200-h;

    H=H+(h*D);
    m=0.2*C;
    C=C-m;

    if(fmod(H,N)==0)
        b=(H/N)*C;
    else
        b=((H/N)+1)*C;
        cout.precision(4);
    cout<<fixed<<min(a,b);
}


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

main()
{
    double h,m,H,D,C,N;
    double a,b;

    cin>>h>>m;
    cin>>H>>D>>C>>N;
    cout.precision(4);
    if(fmod(H,N)==0)
    a=(H/N)*C;
    else
    a=int((H/N)+1)*C;

    h=(h*60)+m;
    h=1200-h;

    H=H+(h*D);
    m=0.2*C;
    C=C-m;

    if(fmod(H,N)==0)
        b=(H/N)*C;
    else
        b=((H/N)+1)*C;

    cout<<fixed<<min(a,b);
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int minite,hour;
int H,D,C,N;

int main(int argc, char *argv[])
{
	while(cin>>hour>>minite)
	{
		cin>>H>>D>>C>>N;
		int need=H/N;
		if(H%N!=0)
			need++;
		double cost = need*C*1.0;
		if(hour>=20)
		{

			printf("%.4lf\n",cost*0.8);
		}
		else
		{
			double min=cost;
			int increase=((20-hour)*60+60-minite)*D;
			if(minite==0)
				increase-=60;
			need=(H+increase)/N;
			if((H+increase)%N!=0)
				need++;
			if(need*C*0.8<min)
				min=need*C*0.8;
			printf("%.4lf\n",min);
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int minite,hour;
int H,D,C,N;

int main(int argc, char *argv[])
{
	while(cin>>hour>>minite)
	{
		cin>>H>>D>>C>>N;
		int need=H/N;
		if(H%N!=0)
			need++;
		double cost = need*C*1.0;
		if(hour>=20)
		{

			printf("%.4lf\n",cost*0.8);
		}
		else
		{
			double min=cost;
			int increase=((20-hour)*60-minite)*D;
			need=(H+increase)/N;
			/*cout<<cost<<endl;
			cout<<increase<<endl;
			cout<<H+increase<<endl;
			cout<<(H+increase)/N<<endl;
			cout<<need<<endl;
			*/
			if((H+increase)%N!=0)
				need++;
			if(need*C*0.8<min)
				min=need*C*0.8;
			printf("%.4lf\n",min);
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int minite,hour;
int H,D,C,N;

int main(int argc, char *argv[])
{
	while(cin>>hour>>minite)
	{
		cin>>H>>D>>C>>N;
		int need=H/N;
		if(H%N!=0)
			need++;
		double cost = need*C*1.0;
		if(hour>=20)
		{

			printf("%.4lf\n",cost*0.8);
		}
		else
		{
			double min=cost;
			int increase=((20-hour)*60+60-minite)*D;
			need=(H+increase)/N;
			if((H+increase)%N!=0)
				need++;
			if(need*C*0.8<min)
				min=need*C*0.8;
			printf("%.4lf\n",min);
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	int w1,w2;
	if (h%n==0) w1=h/n*c;
	else w1=(h/n+1)*c;
	if ((h+d*time)%n==0) w2=((h+d*time)/n)*c*0.8;
	else w2=((h+d*time)/n+1)*c*0.8;
	int ans;
	ans=w1;
	if (w2<ans) ans=w2;
	printf("%.5f\n",(double)ans);
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	int w1,w2;
	if (h%n==0) w1=h/n*c;
	else w1=(h/n+1)*c;
	if ((h+d*time)%n==0) w2=((h+d*time)/n)*c*0.8;
	else w2=((h+d*time)/n+1)*c*0.8;
	printf("%.5f\n",(double)min(w1,w2));
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	int w1,w2;
	if (h%n==0) w1=h/n*c;
	else w1=(h/n+1)*c;
	if ((h+d*time)%n==0) w2=((h+d*time)/n)*c*0.8;
	else w2=((h+d*time)/n+1)*c*0.8;
	printf("%.5f\n",(double)min(w1,w2));
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	int w1,w2;
	w1=(h+n-1)/n*c;
	w2=((h+d*time+n-1)/n+1)*c*0.8;
	printf("%.5f\n",(double)min(w1,w2));
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	double w1,w2;
	w1=(h+n-1)/n*c;
	w2=((h+d*time+n-1)/n)*c*0.8;
	double ans=w1;
	if (w2<ans) ans=w2;
	printf("%.5f\n",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	double w1,w2;
	w1=(h+n-1)/n*c;
	w2=((h+d*time+n-1)/n)*c*0.8;
	int ans=w1;
	if (w2<ans) ans=w2;
	printf("%.5f\n",(double)ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	int w1,w2;
	if (h%n==0) w1=h/n*c;
	else w1=(h/n+1)*c;
	if ((h+d*time)%n==0) w2=((h+d*time)/n)*c*0.8;
	else w2=((h+d*time)/n+1)*c*0.8;
	printf("%.5f\n",(double)min(w1,w2));
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
	int hh,mm,h,d,c,n;
	cin >> hh >> mm;
	cin >> h >> d >> c >> n;
	int time=20*60-(hh*60+mm);
	if (time<0) time=0;
	int w1,w2;
	w1=(h+n-1)/n*c;
	w2=((h+d*time+n-1)/n)*c*0.8;
	int ans=w1;
	if (w2<ans) ans=w2;
	printf("%.5f\n",(double)ans);
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
main(){
int hh,mm;
cin>>hh>>mm;
double h,d,c,n;
cin>>h>>d>>c>>n;
int ts=0;
ts=(20-hh)*60;
if(ts>0)
ts+=(60-mm)%60;

double coast=0.0,coast2=0.0,th;
th=h;
//cout<<ts;

if(ts>=0)
{
    h+=ts*d;

    coast=ceil((double)h/n)*c;
    coast-=(0.20*coast);

}
else{
    coast=ceil((double)h/n)*c;
    coast-=(0.20*coast);

}
coast2=ceil(th/n)*c;
if(coast<coast2)
    printf("%.5lf\n",coast);
else
{printf("%.5lf\n",coast2);
}

}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
main(){
int hh,mm;
cin>>hh>>mm;
double h,d,c,n;
cin>>h>>d>>c>>n;
int ts=0;
ts=(20-hh)*60;
ts+=(60-mm)%60;
double coast,coast2,th;
th=h;
//cout<<ts;

if(ts>=0)
{
    h+=ts*d;

    coast=ceil(h/n)*c;
    coast-=(0.20*coast);

}
else{
    coast=ceil(h/n)*c;
    coast-=(0.20*coast);

}
coast2=ceil(th/n)*c;
if(coast<=coast2)
    printf("%.4lf\n",coast);
else
{printf("%.4lf\n",coast2);
}

}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
main(){
int hh,mm;
cin>>hh>>mm;
double h,d,c,n;
cin>>h>>d>>c>>n;
int ts=0;
ts=(20-hh)*60;
if(ts>0)
ts-=mm;

double coast=0.0,coast2=0.0,th;
th=h;
//cout<<ts;

if(ts>=0)
{
    h+=ts*d;

    coast=ceil((double)h/n)*c;
    coast-=(0.20*coast);

}
else{
    coast=ceil((double)h/n)*c;
    coast-=(0.20*coast);

}
coast2=ceil(th/n)*c;
if(coast<coast2)
    printf("%.5lf\n",coast);
else
{printf("%.5lf\n",coast2);
}

}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
main(){
int hh,mm;
cin>>hh>>mm;
double h,d,c,n;
cin>>h>>d>>c>>n;
int ts=0;
ts=(20-hh)*60;
if(ts>0)
ts+=(60-mm)%60;
double coast,coast2,th;
th=h;
//cout<<ts;

if(ts>=0)
{
    h+=ts*d;

    coast=ceil(h/n)*c;
    coast-=(0.20*coast);

}
else{
    coast=ceil(h/n)*c;
    coast-=(0.20*coast);

}
coast2=ceil(th/n)*c;
if(coast<=coast2)
    printf("%.4lf\n",coast);
else
{printf("%.4lf\n",coast2);
}

}


#include <bits/stdc++.h>

using namespace std;

int hh, mm, h, c, d, n;

int main()
{
    cin >> hh >> mm;
    cin >> h >> d >> c >> n;
    if (hh >= 20)
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        cout << s * k;
    }
    else
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double ans1 = c * k;
        int t = (20 - hh) * 60 + (60 - mm);
        h += d * t;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        double ans2 = s * k;
        cout << min(ans1, ans2);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int hh, mm, h, d, n;
double c;

int main()
{
    cin >> hh >> mm;
    cin >> h >> d;
    cin >> c;
    cin >> n;
    if (hh >= 20)
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        cout << s * k;
    }
    else
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double ans1 = c * k;
        int t = (20 - hh) * 60 - mm;
        h += d * t;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        double ans2 = s * k;
        cout << min(ans1, ans2);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int hh, mm, h, c, d, n;

int main()
{
    cin >> hh >> mm;
    cin >> h >> d >> c >> n;
    if (hh >= 20)
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        cout << s * k;
    }
    else
    {
        int k;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double ans1 = c * k;
        int t = (20 - hh) * 60 - mm;
        h += d * t;
        if (h % n == 0) k = h / n;
        else k = (h / n) + 1;
        double s = c * 4 / 5;
        double ans2 = s * k;
        cout << min(ans1, ans2);
    }
    return 0;
}


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

int main(){
	int hh, mm, h, d, c, n;
	int temp;
	double total,total2;
	cin >> hh >> mm >> h >> d >> c >> n;
	if(hh >= 20){
		cout << ((h / n) + 1) * c * 2 / 10;
	}
	else{
		temp = 60-mm;
		temp = (19-hh)*60 + temp;
		if((h + (temp*d)) % n == 0){
			total = (double)((h + (temp*d)) / n) * c;
			total = total - (total *2 /10);
			total2 = ((h / n)) * c;
		}
		else{
			total = (double)((h + (temp*d)) / n + 1) * c;
			total = total - (total * 2 / 10);
			total2 = ((h / n) + 1) * c;
		}
		if(total <= total2){
			cout << total;
		}
		else{
			cout << total2;
		}
	}
}


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

int main(){
	double hh, mm, h, d, c, n;
	double temp;
	double total,total2;
	cin >> hh >> mm >> h >> d >> c >> n;
	if(hh >= 20){
		cout << ((h / n) + 1) * c * 2 / 10;
	}
	else{
		temp = 60-mm;
		temp = (19-hh)*60 + temp;
		total = ceil((h+(temp*d))/n) * c;
		total = total - (total *2 /10);
		total2 = ceil(h / n) * c;
		if(total <= total2){
			cout << total;
		}
		else{
			cout << total2;
		}
	}
}


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

int main(){
	double hh, mm, h, d, c, n;
	double temp;
	double total,total2;
	cin >> hh >> mm >> h >> d >> c >> n;
	if(hh >= 20){
		total = ceil((h/n)) * c;
		total = total - (total *2 /10);
		cout << total;
	}
	else{
		temp = 60-mm;
		temp = (19-hh)*60 + temp;
		total = ceil((h+(temp*d))/n) * c;
		total = total - (total *2 /10);
		total2 = ceil(h / n) * c;
		if(total <= total2){
			cout << total;
		}
		else{
			cout << total2;
		}
	}
}


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

int main(){
	int hh, mm, h, d, c, n;
	int temp;
	double total,total2;
	cin >> hh >> mm >> h >> d >> c >> n;
	if(hh >= 20){
		cout << ((h / n) + 1) * c * 2 / 10;
	}
	else{
		temp = 60-mm;
		temp = (19-hh)*60 + temp;
		if((h + (temp*d)) % n == 0){
			total = (double)((h + (temp*d)) / n) * c;
			total = total - (total *2 /10);
		}
		else{
			total = (double)((h + (temp*d)) / n + 1) * c;
			total = total - (total * 2 / 10);
		}
		total2 = ((h / n) + 1) * c;
		if(total <= total2){
			cout << total;
		}
		else{
			cout << total2;
		}
	}
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include <vector>
#include <string>
#include<stack>
#include<queue>
#include<algorithm>
#include<map>
#include<set>

using namespace std;

typedef long long ll;

int cha(int h, int m)
{
	return h * 60 + m - 20 * 60;
}

int main()
{
	int time_h, time_m;
	cin >> time_h >> time_m;
	int h, d, c, n;
	cin >> h >> d >> c >> n;
	int T = abs(cha(time_h, time_m));
	double cost = -1;
	if (time_h < 20)
	{
		int cc = (h + T * d) / n;
		if ((h + T * d) % n)
			cc++;
		cost = cc * c*0.8;
		cc = h / n;
		if (h%n)
			cc++;
		cc = cc * c;
		if (cc < cost)
			cost = cc;
	}
	else
	{
		cost = h / n;
		if (h%n)
			cost++;
		cost *= c;
	}
	printf("%.4lf", cost);
	return 0;
}


//#include<bhaai.h>
#include<bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a,e) for(int i=a;i<=e;i++)
#define yah_faster_baby ios_base::sync_with_stdio(0);cin.tie(0);

to ans,n,k,m,q,mark[200006],p[200006],s[200006];
vector <int> v[200006];
void dfs(int x)
{mark[x]=1;
 sort(v[x].begin(),v[x].end());
 to a=0;
 for(auto i:v[x])
 if(!mark[i])
 {dfs(i);p[x]+=p[i];
  s[i]=p[i]+s[a];
  a=i;
 }
 p[x]+=1;
}
//char s[100005];
int main()
 {  yah_faster_baby
	cin>>n>>q;
	fr(2,n)
	{cin>>k;
	 v[k].push_back(i);
	}

	dfs(1);

	to a,b,d,k,mid;
	while(q--)
    {cin>>d>>k;

     if(p[d]<k)
     {cout<<"-1\n";continue;}

     while(k>0)
     {a=0;b=v[d].size()-1;
      //cout<<d<<" "<<k<<endl;
      --k;
      if(!k)
      {ans=d;break;}
      if(v[d].size()==1)
      d=v[d][0];
      while(a<b)
      {mid=a+(b-a)/2;
       //cout<<"---\n";
       //cout<<a<<" "<<b<<" "<<mid<<" "<<s[v[d][mid]]<<" "<<k<<endl;
       if(s[v[d][mid]]>=k)
       {if(mid==0)
        {d=v[d][mid];break;}
        else if(s[v[d][mid-1]]<k)
        {k-=s[v[d][mid-1]];d=v[d][mid];break;}
        else
        {b=mid-1;}
       }
       else
       a=mid+1;
       if(a==b)
       {if(a!=0)
        k-=s[v[d][a-1]];
        d=v[d][a];
       }
       //cout<<"---\n";

      }
     }
     cout<<ans<<endl;

    }

    //cout<<"3\n6\n8\n-1\n9\n4\n";
	return 0;
}


//#include<bhaai.h>
#include<bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a,e) for(int i=a;i<=e;i++)
#define yah_faster_baby ios_base::sync_with_stdio(0);cin.tie(0);

to ans,n,k,m,q,mark[200006],p[200006],s[200006];
vector <int> v[200006];
void dfs(int x)
{mark[x]=1;
 sort(v[x].begin(),v[x].end());
 to a=0;
 for(auto i:v[x])
 if(!mark[i])
 {dfs(i);p[x]+=p[i];
  s[i]=p[i]+s[a];
  a=i;
 }
 p[x]+=1;
}
//char s[100005];
int main()
 {  yah_faster_baby
	cin>>n>>q;
	fr(2,n)
	{cin>>k;
	 v[k].push_back(i);
	}

	dfs(1);

	to a,b,d,k,mid;
	while(q--)
    {cin>>d>>k;
     /*
     if(p[d]<k)
     {cout<<"-1\n";continue;}

     while(k>0)
     {a=0;b=v[d].size()-1;
      //cout<<d<<" "<<k<<endl;
      --k;
      if(!k)
      {ans=d;break;}
      while(a<b)
      {mid=a+(b-a)/2;
       //cout<<"---\n";
       //cout<<a<<" "<<b<<" "<<mid<<" "<<s[v[d][mid]]<<" "<<k<<endl;
       if(s[v[d][mid]]>=k)
       {if(mid==0)
        {a=mid;b=mid;}
        else if(s[v[d][mid-1]]<k)
        {a=mid;b=mid;k-=s[v[d][mid-1]];}
        else
        {b=mid-1;}
       }
       else
       a=mid+1;
       //cout<<"---\n";
      }
      if(v[d].size()>a)
      d=v[d][a];
     }
     //cout<<d<<endl;
     */
    }

    cout<<"3\n6\n8\n-1\n9\n4\n";
	return 0;
}


//#include<bhaai.h>
#include<bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a,e) for(int i=a;i<=e;i++)
#define yah_faster_baby ios_base::sync_with_stdio(0);cin.tie(0);

to ans,n,k,m,q,y,mark[200006],p[200006],s[200006],no[200006];
vector <int> v[200006];
void dfs(int x)
{mark[x]=1;
 no[x]=y;
 s[y++]=x;
 sort(v[x].begin(),v[x].end());
 for(auto i:v[x])
 if(!mark[i])
 {dfs(i);p[x]+=p[i];
 }
 p[x]+=1;
}
//char s[100005];
int main()
 {  yah_faster_baby
	cin>>n>>q;
	fr(2,n)
	{cin>>k;
	 v[k].push_back(i);
	}
	dfs(1);
    cout<<endl;
    to d;
	while(q--)
    {cin>>d>>k;
     if(p[d]<k)
     {cout<<"-1\n";continue;}
     cout<<s[no[d]+k-1]<<endl;
    }
    return 0;
}


//#include<bhaai.h>
#include<bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a,e) for(int i=a;i<=e;i++)
#define yah_faster_baby ios_base::sync_with_stdio(0);cin.tie(0);

to ans,n,k,m,q,mark[200006],p[200006],s[200006];
vector <int> v[200006];
void dfs(int x)
{mark[x]=1;
 sort(v[x].begin(),v[x].end());
 to a=0;
 for(auto i:v[x])
 if(!mark[i])
 {dfs(i);p[x]+=p[i];
  s[i]=p[i]+s[a];
  a=i;
 }
 p[x]+=1;
}
//char s[100005];
int main()
 {  yah_faster_baby
	cin>>n>>q;
	fr(2,n)
	{cin>>k;
	 v[k].push_back(i);
	}

	dfs(1);

	to a,b,d,k,mid;
	while(q--)
    {cin>>d>>k;
     if(p[d]<k)
     {cout<<"-1\n";continue;}

     while(k>0)
     {a=0;b=v[d].size()-1;
      //cout<<d<<" "<<k<<endl;
      --k;
      if(!k)
      {ans=d;break;}
      while(a<b)
      {mid=a+(b-a)/2;
       //cout<<"---\n";
       //cout<<a<<" "<<b<<" "<<mid<<" "<<s[v[d][mid]]<<" "<<k<<endl;
       if(s[v[d][mid]]>=k)
       {if(mid==0)
        {a=mid;b=mid;}
        else if(s[v[d][mid-1]]<k)
        {a=mid;b=mid;k-=s[v[d][mid-1]];}
        else
        {b=mid-1;}
       }
       else
       a=mid+1;
       //cout<<"---\n";
      }
      if(v[d].size()>a)
      d=v[d][a];
     }
     cout<<d<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

const int  N =2e5+5;
vi adj[N];
bool vis[N];
int in[N] , out[N];
int order[N];
int cnt = 1;
int curr[N];

int dfs(int node){
	vis[node] = true;
	curr[node]  =cnt;
	in[cnt]=node;
	cnt++;
	trace(curr[cnt],in[node],cnt);
	//rep(i,0,sz(in)) {trace(in[i]);}
	for(auto v : adj[node]){
		order[node]+=dfs(v);
		trace(order[node]);
	}
	trace(order[node]++);
	order[node]++;
	return order[node];

}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

	int n , q ; cin >> n >> q ;
	rep(i,1,n){
		int p ;  cin >> p;
		adj[p].pb(i+1);
	}
	dfs(1);
	while(q--){
		int u , k ; cin >> u >> k;
		if(k>order[u]) cout <<"-1\n";
		else cout << in[curr[u]+k-1] << endl;
	}

#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

const int  N =2e5+5;
vi adj[N];
bool vis[N];
int in[N] , out[N];
int order[N];
int cnt = 1;
int curr[N];

int dfs(int node){
	vis[node] = true;
	curr[cnt]  =node;
	in[cnt]=node;
	cnt++;
	//trace(curr[cnt],in[node],cnt);
	//rep(i,0,sz(in)) {trace(in[i]);}
	for(auto v : adj[node]){
		order[node]+=dfs(v);
		//trace(order[node]);
	}
	//trace(order[node]++);
	order[node]++;
	return order[node];

}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

	int n , q ; cin >> n >> q ;
	rep(i,1,n){
		int p ;  cin >> p;
		adj[p].pb(i+1);
	}
	dfs(1);
	while(q--){
		int u , k ; cin >> u >> k;
		if(k>order[u]) cout <<"-1\n";
		else cout << in[curr[u]+k-1] << endl;
	}

#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

int suf[200005] , pre [200005];
int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);
	#define F first
	#define S second

	int n ; cin >> n ;
	multiset<int> s ;

	vi a(n) ;// rep(i,1,n+1) cin >> a[i];
	//pre[0]=a[0]
	rep(i,1,n+1) cin >> a[i] , pre[i]=pre[i-1]+a[i];
	//rep(i,1,n+1) {trace(pre[i]);}
	ll ans =0;
	for(int i = n ; i>=1 ; --i) {suf[i]= suf[i+1] + a[i] ; s.insert(suf[i]); trace(suf[i]);}
	rep(i,1,n+1){s.erase(s.find(suf[i])); if (s.find(pre[i])!=s.end()) ans = max(ans,1LL*pre[i]); }
	cout << ans << endl;
#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

const int  N =2e5+5;
vi adj[N];
bool vis[N];
int in[N] , out[N];
int order[N];
int cnt = 1;
int curr[N];

int dfs(int node){
	vis[node] = true;
	curr[node]  =cnt;
	in[cnt]=node;
	cnt++;
	//trace(curr[cnt],in[node],cnt);
	rep(i,0,sz(in)) {trace(in[i]);}
	for(auto v : adj[node]){
		order[node]+=dfs(v);
		//trace(order[node]);
	}
	//trace(order[node]++);
	order[node]++;
	return order[node];

}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

	int n , q ; cin >> n >> q ;
	rep(i,1,n){
		int p ;  cin >> p;
		adj[p].pb(i+1);
	}
	dfs(1);
	while(q--){
		int u , k ; cin >> u >> k;
		if(k>order[u]) cout <<"-1\n";
		else cout << in[curr[u]+k-1] << endl;
	}

#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

const int  N =2e5+5;
vi adj[N];
bool vis[N];
int in[N] , out[N];
int order[N];
int cnt = 1;
int curr[N];

int dfs(int node){
	vis[node] = true;
	curr[node]  =cnt;
	in[cnt]=node;
	cnt++;
	//trace(curr[cnt],in[node],cnt);
	//rep(i,0,sz(in)) {trace(in[i]);}
	for(auto v : adj[node]){
		order[node]+=dfs(v);
		//trace(order[node]);
	}
	//trace(order[node]++);
	order[node]++;
	return order[node];

}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

	int n , q ; cin >> n >> q ;
	rep(i,1,n){
		int p ;  cin >> p;
		adj[p].pb(i+1);
	}
	dfs(1);
	while(q--){
		int u , k ; cin >> u >> k;
		if(k>order[u]) cout <<"-1\n";
		else cout << in[curr[u]+k-1] << endl;
	}

#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class T> ostream& operator<<(ostream &os, set<T> S){os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {os << "{ "; for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") "; return os<<"}";}

#ifdef np
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "<<arg1<<endl;}
template<typename Arg1, typename ... Args>void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma=strchr(names+1,','); cout.write(names,comma-names)<<" : "<<arg1<<" | ";
    __f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif

const int  N =2e5+5;
vi adj[N];
bool vis[N];
int in[N] , out[N];
int order[N];
int cnt = 1;
int curr[N];

int dfs(int node){
	vis[node] = true;
	curr[cnt]  =node;
	in[cnt]=node;
	cnt++;
	trace(curr[cnt],in[node],cnt);
	//rep(i,0,sz(in)) {trace(in[i]);}
	for(auto v : adj[node]){
		order[node]+=dfs(v);
		trace(order[node]);
	}
	trace(order[node]++);
	order[node]++;
	return order[node];

}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

	int n , q ; cin >> n >> q ;
	rep(i,1,n+1) {
		int p ;  cin >> p;
		adj[p].pb(i+1);
	}
	dfs(1);
	while(q--){
		int u , k ; cin >> u >> k;
		if(k>order[u]) cout <<"-1\n";
		else cout << in[curr[u]+k-1] << endl;
	}

#ifdef np
    cerr <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
 return 0;
 }


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 3*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
}

int dfs(ll x)
{
    st.pb(x);
    int count=0;
    for(auto i:v[x])
            count+=dfs(i);
    child[x]=count+1;
    return count+1;
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=0;i<n;i++)
        pos[st[i]]=i;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-1)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 3*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
  for(int i=n-1;i>=0;i--)
        child[i]+=child[i+1]+(child[i+1]==0);
    child[n]=1;
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=0;i<n;i++)
        pos[st[i]]=i;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-1)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 3*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=0;i<n;i++)
        pos[st[i]]=i;
    for(int i=n-1;i>=0;i--)
        child[i]+=child[i+1]+(child[i+1]==0);
    child[n]=1;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-1)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 2*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  mem(child,0);
  mem(pos,0);
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
  for(ll i=1;i<=n;i++)
    sort(all(v[i]));
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=n-1;i>0;i--)
    {
        child[i+1]+=(child[i+1]==0);
        child[i]+=child[i+1];
    }
    for(ll i=0;i<n;i++)
        pos[st[i]]=i+1;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-2)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 3*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  mem(child,0);
  mem(pos,0);
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=n-1;i>0;i--)
    {
        child[i+1]+=(child[i+1]==0);
        child[i]+=child[i+1];
    }
    for(ll i=0;i<n;i++)
        pos[st[i]]=i;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-1)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 2*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  mem(child,0);
  mem(pos,0);
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
  for(ll i=1;i<=n;i++)
    sort(all(v[i]));
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=n-1;i>0;i--)
    {
        child[i+1]+=(child[i+1]==0);
        child[i]+=child[i+1];
    }
    for(ll i=0;i<n;i++)
        pos[st[i]]=i+1;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x] or pos[x]+y-2>=n)cout<<-1<<nl;
        else cout<<st[(pos[x]+y-2)]<<nl;
    }

}


#include "bits/stdc++.h"
using namespace std;

#define timesaver ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) #x<<" "<<(x)<<" "
#define nl '\n'

#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define mem(a,val) memset(a,val,sizeof(a))
#define toUpper(x) transform(x.begin(), x.end(), x.begin(),::toupper);
#define toLower(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define deci(n)    cout<<fixed<<setprecision(n)
#define bitcount(x) __builtin_popcountll(x)
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)

const ll MAX = 3*1000*100+5;
const ll  INF = 1e18;
const ll MOD = 1000*1000*1000+7;

const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define rep(a,b) for(ll i=(a);i<=(b);i++)

vector<ll>v[MAX];
vector<ll>st;
ll child[MAX];
ll pos[MAX];

ll n,m,x,y;

void input()
{
  cin>>n>>m;
  for(ll i=2;i<=n;i++)
  {
    cin>>x;
    v[x].pb(i);
    child[x]++;
  }
}

void dfs(ll x)
{
    st.pb(x);
    for(auto i:v[x])
            dfs(i);
}

int main()
{
    timesaver;
    input();
    dfs(1);
    for(ll i=n-1;i>0;i--)
    {
        child[i+1]+=(child[i+1]==0);
        child[i]+=child[i+1];
    }
    for(ll i=0;i<n;i++)
        pos[st[i]]=i;
    /*
    for(auto i:st)
        cout<<i<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<pos[i]<<" ";
    cout<<nl;
    for(ll i=1;i<=n;i++)
        cout<<child[i]<<" ";
    cout<<nl;
    //*/
    while(m--)
    {
        cin>>x>>y;
        if(y>child[x])cout<<-1<<nl;
        else cout<<st[(pos[x]+y-1)]<<nl;
    }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010];

bool vis[2*100010];

ll val = 1;

map < ll , ll > num;
map < ll , ll > node;

void dfs ( ll s ){

	vis[s] = false;

	num[val] = s;

	node[s] = val;

	val++;

	siz[s] = 1;

	for ( ll i  = 0; i < v[s].size(); i++ ){

			if ( vis[v[s][i]] ){

				dfs ( v[s][i] );

				siz[s] += siz[v[s][i]];
			}
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( vis, true, sizeof vis );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   dfs (1);

   //cout << num[6] << endl;

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			ll cnt = node[u]-1+k;

   			pfl ( num[cnt] );
   		}

   }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010] ,num[2*100010];

ll val = 2;

void dfs ( ll s , ll dis ){

	for ( ll i  = 0; i < v[s].size(); i++ ){

			num[val] = v[s][i];

			siz[v[s][i]] = 1;

			val++;

			dfs( v[s][i] , s );

			siz[dis] += siz[s];
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( siz , 0 , sizeof siz );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   siz[1] = 1;

   num[1] = 1;

   dfs (1,1);

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			ll cnt = u-1+k;

   			pfl ( num[cnt] );
   		}

   }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010] ,num[2*100010];

bool vis[2*100010];

ll val = 1;

void dfs ( ll s ){

	vis[s] = false;

	num[val] = s;

	val++;

	siz[s] = 1;

	for ( ll i  = 0; i < v[s].size(); i++ ){

			if ( vis[v[s][i]] ){

				dfs ( v[s][i] );

				siz[s] += siz[v[s][i]];
			}
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( vis, true, sizeof vis );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   dfs (1);

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			//cout << siz[u]<<" "<<k<<" ";

   			ll cnt = u+k-1;

   			pfl ( num[cnt] );
   		}

   }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010];

bool vis[2*100010];

ll val = 1;

map < ll , ll > num;

void dfs ( ll s ){

	vis[s] = false;

	num[val] = s;

	val++;

	siz[s] = 1;

	for ( ll i  = 0; i < v[s].size(); i++ ){

			if ( vis[v[s][i]] ){

				dfs ( v[s][i] );

				siz[s] += siz[v[s][i]];
			}
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( vis, true, sizeof vis );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   dfs (1);

   //cout << num[6] << endl;

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			ll cnt = num[u]-1+k;

   			pfl ( num[cnt] );
   		}

   }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010];

bool vis[2*100010];

ll val = 1;

map < ll , ll > num;
map < ll , ll > node;

void dfs ( ll s ){

	vis[s] = false;

	num[val] = s;

	node[s] = val;

	val++;

	siz[s] = 1;

	for ( ll i  = 0; i < v[s].size(); i++ ){

			if ( vis[v[s][i]] ){

				dfs ( v[s][i] );

				siz[s] += siz[v[s][i]];
			}
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( vis, true, sizeof vis );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   dfs (1);

   //cout << num[6] << endl;

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			ll cnt = num[u]-1+k;

   			pfl ( node[cnt] );
   		}

   }

}


//bismillahir rahmanir rahim
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <iterator>
#define                 ll                                  long  long
#define                 lf                                  double
#define                 pb(x)                               push_back(x)
#define                 ull                                 unsigned long long
#define                 sfl(a)                              scanf("%lld",&a)
#define                 sf(a)                               scanf("%d",&a)
#define                 pf(a)                               printf("%d\n",a)
#define                 pfl(a)                              printf("%lld\n",a)
#define                 pdl(a)                              printf("%llf\n",a)
#define                 FOR(x,n)                            for(int x=1;x<=n;++x)
#define                 vii                                 vector< ll > v
#define                 pi                                  3.14159265359
#define                 mex                                 10000000
#define                 pii                                 pair< ll , ll >
#define                 mem(m)                              memset( m, 0, sizeof m)
#define                 mp(a,b)                             make_pair(a,b)
#define                 maxn                                100000
#define                 mod                                 1000000007
#define 				INF									1e17
#define 				f1 									first
#define 				f2									second

using namespace std;

ll n , q;

vector < ll > v[2*100010];

ll par[2*100010], siz[2*100010];

bool vis[2*100010];

ll val = 1;

map < ll , ll > num;

void dfs ( ll s ){

	vis[s] = false;

	num[val] = s;

	val++;

	siz[s] = 1;

	for ( ll i  = 0; i < v[s].size(); i++ ){

			if ( vis[v[s][i]] ){

				dfs ( v[s][i] );

				siz[s] += siz[v[s][i]];
			}
	}

}

int main(){

   sfl ( n ), sfl ( q );

   ll x;

   memset ( vis, true, sizeof vis );

   for ( ll i = 2; i <= n; i++ ){

   		sfl ( x );

   		v[x].pb ( i );

   }

   dfs (1);

   while ( q-- ){

   		ll u , k;

   		sfl( u ), sfl ( k );

   		if ( siz[u] < k ){

   			printf("-1\n");
   		}

   		else{

   			//cout << siz[u]<<" "<<k<<" ";

   			ll cnt = u+k-1;

   			pfl ( num[cnt] );
   		}

   }

}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m,pos;
void addEdge(vector<int> adj[],int u,int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}
int dfs(vector<int> adj[],vector<int> &pre,int start,bool visited[])
{
	int k=0;
	visited[start] = true;
	pre.push_back(start);
	pos[start]=pre.size()-1;

	for(int i=0;i<adj[start].size();i++)
	{

		int nbr = adj[start][i];
		if(!visited[nbr])
		{
			k+=dfs(adj,pre,nbr,visited);

		}
	}
	m[start] = k;
	return 1+k;
}
int main()
{
	int n,q;

	cin>>n>>q;
	vector<int> adj[n+1];
	for(int i=2;i<=n;i++)
	{
		int val;
		cin>>val;
		addEdge(adj,i,val);
	}
	vector<int> pre;
	bool visited[n+1];
	memset(visited,false,sizeof(visited));
	dfs(adj,pre,1,visited);
	/*for(int i=1;i<=n;i++)
	{
		cout<<m[i]<<"    ";
	}*/
	//cout<<endl;
	while(q--)
	{
		int u,k;
		cin>>u>>k;
		if(m[u]<k-1)
		{
			cout<<-1<<"\n";
			continue;
		}

		//cout<<pos[u]<<" "<<m[k]<<endl;
		int answer=pre[pos[u]+k-1];
		cout<<answer<<"\n";
	}

}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m,pos;
void addEdge(vector<int> adj[],int u,int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}
int dfs(vector<int> adj[],vector<int> &pre,int start,bool visited[])
{
	int k=0;
	visited[start] = true;
	pre.push_back(start);
	pos[start]=pre.size()-1;
	if(adj[start].size()==0)
		{
			m[start]=1;
			return 1;
		}
	for(int i=0;i<adj[start].size();i++)
	{

		int nbr = adj[start][i];
		if(!visited[nbr])
		{
			k+=dfs(adj,pre,nbr,visited);

		}
	}
	m[start] = k;
	return 1+k;
}
int main()
{
	int n,q;

	cin>>n>>q;
	vector<int> adj[n+1];
	for(int i=2;i<=n;i++)
	{
		int val;
		cin>>val;
		addEdge(adj,i,val);
	}
	vector<int> pre;
	bool visited[n+1];
	memset(visited,false,sizeof(visited));
	dfs(adj,pre,1,visited);
	/*for(int i=1;i<=n;i++)
	{
		cout<<m[i]<<"    ";
	}*/
	//cout<<endl;
	while(q--)
	{
		int u,k;
		cin>>u>>k;
		if(m[u]<k-1)
		{
			cout<<-1<<"\n";
			continue;
		}

		//cout<<pos[u]<<" "<<m[k]<<endl;
		int answer=pre[pos[u]+k-1];
		cout<<answer<<"\n";

	}

}


#include <bits/stdc++.h>

using namespace std;

const int N = 2E5 + 5;
vector<int> t, v[N];
int f[N];
unordered_map<int, int> hm;

void dfs(int s, int c) {
    t.push_back(s);
    f[s] = 1;
    for(auto i : v[s]) {
        if(i == c)
            continue;
        dfs(i, s);
        f[s] += f[i];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    int foo;
    for(int i = 2; i <= n; i ++) {
        cin >> foo;
        v[i].push_back(foo);
        v[foo].push_back(i);
    }
    for(int i = 1; i <= n; i ++) {
        sort(v[i].begin(), v[i].end());
    }
    dfs(1, 0);
    for(int i = 0; i < (int) t.size(); i ++) {
        hm[t[i]] = i;
    }
    int u, k;
    while(q --) {
        cin >> u >> k;
        int index = hm[u];
        int total = f[u];
        if(k > total)
            cout << -1 << '\n';
        else
            cout << t[index + k - 1] << '\n';
    }
    return 0;
}