#pragma GCC optimize("no-stack-protector,Ofast")
#include<bits/stdc++.h>
using namespace std;

unsigned int seed=880301;
int rnd(){
    return seed=(seed*880301)^7122;
}

struct node{
    node *l,*r;
    int sz,val,pri;
    int lz(){return l?l->sz:0;}
    int rz(){return r?r->sz:0;}
    void psz(){sz=lz()+rz()+1;}
    node(int v=-1):l(0),r(0),sz(1),val(v),pri(rnd()){}
} *root,pool[1000006];

pair<int,int> op[1000006];
bitset<1000006> it;
int npt=1;

node *merge(node *a,node *b){
    if(!(a&&b))return a?:b;
    if(a->pri>b->pri){
        a->r=merge(a->r,b);
        a->psz();
        return a;
    }
    else{
        b->l=merge(a,b->l);
        b->psz();
        return b;
    }
}
void split(node *now,int sz,node *&a,node *&b){
    if(!now){a=b=0; return;}
    if(now->lz()>=sz){
        b=now;
        split(now->l,sz,a,b->l);
        b->psz();
    }
    else{
        a=now;
        split(now->r,sz-1-now->lz(),a->r,b);
        a->psz();
    }
}
void gl(node *now){
    if(!now)return;
    gl(now->l);
    if(now->val!=-1)printf("%d ",now->val);
    else{
        while(it[npt])++npt;
        printf("%d ",npt);
        it[npt]=1;
    }
    gl(now->r);
}

int inp(){
    char c; int x=0;
    while((c=getchar()) && (c<'0' || c>'9'));
    while((x=c-'0',c=getchar()) && c>='0' && c<='9');
    return x;
}

int main(){
    // ios_base::sync_with_stdio(0); cin.tie(0);
    int n=inp(),m=inp();
    for(int i=1;i<=m;++i){
        op[i].first=inp();
        op[i].second=inp();
        // it[op[i].first]=1;
    }
    for(int i=1;i<=n;++i)root=merge(root,&pool[i]);

    node *a,*b,*c,*d;
    for(int i=m;i>=1;--i){
        split(root,1,a,b);
        if(a->val==-1){
            if(it[op[i].first])exit((cout<<-1<<endl,0));
            a->val=op[i].first;
            it[op[i].first]=1;
        }
        else if(a->val!=op[i].first)exit((cout<<-1<<endl,0));
        split(b,op[i].second-1,c,d);
        root=merge(c,merge(a,d));
    }
    gl(root);
}


#include <iostream>
using namespace std;

int n, m, x, y;

void init(int *itChecked, int *itDistinc, int *posIT, int lef, int rig, int k){
    itChecked[k] = 0;
    itDistinc[k] = 0;
    if (lef == rig){
        posIT[lef] = k;
        return;
    }
    init(itChecked, itDistinc, posIT, lef, (lef + rig) >> 1, k << 1);
    init(itChecked, itDistinc, posIT, ((lef + rig) >> 1) + 1, rig, (k << 1) + 1);
}

void update(int *it, int posIT, int val){
    while (posIT){
        it[posIT] += val;
        posIT >>= 1;
    }
}

int find(int *itChecked, int lef, int rig, int k, int vt){
    if (lef == rig)
        return lef;

    int mid = (rig + lef) >> 1;
    if (mid + itChecked[(k << 1) + 1] < vt)
        return find(itChecked, mid + 1, rig, (k << 1) + 1, vt);
    else
        return find(itChecked, lef, mid, k << 1, vt - itChecked[(k << 1) + 1]);
}

int get(int *it, int lef, int rig, int u, int v, int k){
    if (u > v)
        return 0;
    if (lef == u && rig == v)
        return it[k];

    int mid = (lef + rig) >> 1;
    if (v <= mid)
        return get(it, lef, mid, u, v, k << 1);
    else
    if (u > mid)
        return get(it, mid + 1, rig, u, v, (k << 1) + 1);
    else
        return get(it, lef, mid, u, mid, k << 1) + get(it, mid + 1, rig, mid + 1, v, (k << 1) + 1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin >> n >> m;
    int *itChecked = new int[5 * n];
    int *itDistinc = new int[5 * n];
    int *posIT = new int[n + 1];
    int *posElement = new int[n + 1];
    bool *exist = new bool[n + 1];
    int *res = new int[n + 1];
    for (int i = 1; i <= n; i++){
        res[i] = 0;
        exist[i] = false;
        posElement[i] = 0;
    }

    init(itChecked, itDistinc, posIT, 1, n, 1);
    for (int i = 1; i <= m; i++){
        cin >> x >> y;
        exist[x] = true;
        update(itDistinc, posIT[i], 1);
        if (posElement[x]){
            update(itDistinc, posIT[posElement[x]], -1);
            if (get(itDistinc, 1, n, posElement[x] + 1, i - 1, 1) == y - 1){
                posElement[x] = i;
                continue;
            }
            else{
                cout << -1;
                return 0;
            }
        }

        int vt = find(itChecked, 1, n, 1, y);
        if (vt + get(itChecked, 1, n, vt + 1, n, 1) != y || res[vt]){
            cout << -1;
            return 0;
        }

        res[vt] = x;
        update(itChecked, posIT[vt], 1);
        posElement[x] = i;
    }
    int j = 1;
    for (int i = 1; i <= n; i++){
        if (res[i] == 0){
            while (exist[j++]);
            res[i] = j - 1;
        }
        cout << res[i] << " ";
    }

    delete[] itChecked;
    delete[] itDistinc;
    delete[] posIT;
    delete[] posElement;
    delete[] exist;
    delete[] res;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int n,k,ans=-1;
int b[200007];
vector <int> rose[4];
string s;
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	cin>>s;
	if(k==1){
		cout<<-1;
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(s[i-1]=='W') rose[1].pb(b[i]);
		else if(s[i-1]=='O') rose[2].pb(b[i]);
		else rose[3].pb(b[i]);
	}
	for(int i=1;i<=3;i++) sort(rose[i].begin(),rose[i].end(),greater<int>());
	if((int)rose[1].size()!=0 && (int)rose[2].size()!=0 && (int)rose[1].size()+(int)rose[2].size()>=k){
		int cur=0;
		cur+=rose[1][0];
		cur+=rose[2][0];
		vector <int> save;
		save.clear();
		for(int i=1;i<(int)rose[1].size();i++) save.pb(rose[1][i]);
		for(int i=1;i<(int)rose[2].size();i++) save.pb(rose[2][i]);
		sort(save.begin(),save.end(),greater<int>());
		for(int i=0;i<k-2;i++) cur+=save[i];
		ans=max(ans,cur);
	}
	if((int)rose[2].size()!=0 && (int)rose[3].size()!=0 && (int)rose[2].size()+(int)rose[3].size()>=k){
		int cur=0;
		cur+=rose[2][0];
		cur+=rose[3][0];
		vector <int> save;
		save.clear();
		for(int i=1;i<(int)rose[2].size();i++) save.pb(rose[2][i]);
		for(int i=1;i<(int)rose[3].size();i++) save.pb(rose[3][i]);
		sort(save.begin(),save.end(),greater<int>());
		for(int i=0;i<k-2;i++) cur+=save[i];
		ans=max(ans,cur);
	}
	cout<<ans;
}


#include<iostream>
#include<queue>
using namespace std;
int n,k;
vector<int>r,o,w;
int d[200001];
int solve(vector<int>a,vector<int>b){
	if(a.empty() || a.size()+b.size()<k) return 0;
	int maxa=0,maxb=0;
	int posa,posb;
	for(int i=0; i<a.size() ;i++){
		if(a[i]>maxa){
			maxa=a[i];
			posa=i;
		}
	}
	for(int i=0; i<b.size() ;i++){
		if(b[i]>maxb){
			maxb=b[i];
			posb=i;
		}
	}
	int ans=maxa+maxb;
	priority_queue<int>pq;
	for(int i=0; i<a.size() ;i++) if(i!=posa) pq.push(a[i]);
	for(int i=0; i<b.size() ;i++) if(i!=posb) pq.push(b[i]);
	for(int i=0; i<k-2; i++){
		ans+=pq.top();
		pq.pop();
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for(int i=1; i<=n ;i++){
		cin >> d[i];
	}
	char c;
	for(int i=1; i<=n ;i++){
		cin >> c;
		if(c=='R') r.push_back(d[i]);
		else if(c=='O') o.push_back(d[i]);
		else w.push_back(d[i]);
	}
	if(k==1 || o.empty()){
		cout << "-1\n";
		return 0;
	}
	int ans=max(solve(r,o),solve(w,o));
	if(ans==0) cout << "-1\n";
	else cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_st=(a);i>=i##_st;--i)
#define foe(i,a) for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define Es(x,i) for(Edge *i=G[x];i;i=i->nxt)
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef double db;
typedef long long ll;
typedef vector<int> Vi;
typedef pair<int,int> pii;
const int inf=~0u>>1,mo=1e9+7;
inline int rd(){
    int x,c,f=1;while(!isdigit(c=getchar()))f=c!='-';x=c-'0';
    while(isdigit(c=getchar()))x=x*10+c-'0';return f?x:-x;
}
const int N=200011;
ll a[N],b[N],c[N];
int t1,t2,t3;
char s[N];int f[N];
int cmp(ll i,ll j){return i>j;}
int main(){
	int n=rd(),k=rd();
	rep(i,1,n)f[i]=rd();
	scanf("%s",s+1);
	rep(i,1,n)if(s[i]=='O')a[++t1]=f[i];else if(s[i]=='R')b[++t2]=f[i];
	else c[++t3]=f[i];
	sort(a+1,a+t1+1,cmp),sort(b+1,b+t2+1,cmp),sort(c+1,c+t3+1,cmp);
	For(i,2,N)a[i]+=a[i-1];For(i,2,N)b[i]+=b[i-1];For(i,2,N)c[i]+=c[i-1];
	if(!t1||!(t2+t3)||K<2){puts("-1");return 0;}
	ll ans=0;
	For(i,1,k){
		ans=max(ans,a[i]+max(b[k-i],c[k-i]));
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int n,k,ans=-1;
int b[200007];
vector <int> rose[4];
string s;
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	cin>>s;
	if(k==1){
		cout<<-1;
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(s[i-1]=='W') rose[1].pb(b[i]);
		else if(s[i-1]=='O') rose[2].pb(b[i]);
		else rose[3].pb(b[i]);
	}
	for(int i=1;i<=3;i++) sort(rose[i].begin(),rose[i].end(),greater<int>());
	if((int)rose[1].size()!=0 && (int)rose[2].size()!=0 && (int)rose[1].size()+(int)rose[2].size()>=k){
		int cur=0;
		cur+=rose[1][0];
		cur+=rose[2][0];
		vector <int> save;
		save.clear();
		for(int i=1;i<(int)rose[1].size();i++) save.pb(rose[1][i]);
		for(int i=1;i<(int)rose[2].size();i++) save.pb(rose[2][i]);
		sort(save.begin(),save.end(),greater<int>());
		for(int i=0;i<k-2;i++) cur+=save[i];
		ans=max(ans,cur);
	}
	if((int)rose[2].size()!=0 && (int)rose[3].size()!=0 && (int)rose[2].size()+(int)rose[3].size()>=k){
		int cur=0;
		cur+=rose[2][0];
		cur+=rose[3][0];
		save.clear();
		vector <int> save;
		for(int i=1;i<(int)rose[2].size();i++) save.pb(rose[2][i]);
		for(int i=1;i<(int)rose[3].size();i++) save.pb(rose[3][i]);
		sort(save.begin(),save.end(),greater<int>());
		for(int i=0;i<k-2;i++) cur+=save[i];
		ans=max(ans,cur);
	}
	cout<<ans;
}


#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define FAST_IO ios::sync_with_stdio(false)
#define sci(d) scanf("%d", &d)
#define scs(s) scanf("%s", &s)

#define SINF 20000000
#define INF 2000000000
#define LLINF 900000000000000000LL
#define X first
#define Y second
#define MAX 1001
#define MOD 1000000007LL
#define endl "\n"
#define ALL(a) (a).begin(), (a).end()
#define pr(n) operator[](n);

typedef long long ll;
typedef pair<int, int> pi;

int Q, N, M, K, L;
ll FAN[100001];
int p[100001];
ll v[100001];

struct comp {
	bool operator() (const int& a, const int& b) const {
		if (v[a] != v[b]) return v[a] < v[b];
		return a < b;
	}
};

set<int, comp> children[100001];
int deg[100001];
multiset<ll> mulset;

ll value_for_self(int a) {
	return FAN[a] - (1 + deg[a]) * value_for_other(a);
}
ll value_for_other(int a) {
	return FAN[a] / (2 + deg[a]);
}

int main() {
	FAST_IO;
	cin >> N >> M;
	for (int i = 0; i < N; i++) cin >> FAN[i];
	for (int i = 0; i < N; i++) {
		cin >> p[i]; p[i]--;
		children[p[i]].insert(i);
		deg[p[i]]++;
	}
	for (int i = 0; i < N; i++) {
		v[i] += value_for_self(i);
		for (int child : children[i])
			v[i] += value_for_other(child);
	}
	for (int i = 0; i < N; i++) {
		children[p[i]].erase(i);
		children[p[i]].insert(i);
	}
	for (int n = 0; n < N; n++) {
		ll pv = value_for_other(n);
		if (!deg[n]) continue;
		mulset.insert(v[*children[n].begin()] + pv);
		mulset.insert(v[*children[n].rbegin()] + pv);
	}
	for (int q = 0; q < M; q++) {
		int a, b, c;
		cin >> a;
		if (a == 1) {
			cin >> b >> c;
			b--; c--;

			set<int> go = { b, c, p[b], p[c], p[p[b]] };
			set<int> go2 = go;
			for (int n : go) go2.insert(p[n]);

			for (int n : go2) {
				if (!deg[n]) continue;
				ll pv = value_for_other(n);
				mulset.erase(v[*children[n].begin()] + pv);
				mulset.erase(v[*children[n].rbegin()] + pv);
			}

			for (int n : go)
				children[p[n]].erase(n);

			for (int n : go) {
				v[n] -= value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] -= value_for_other(m);
			}
			deg[p[b]]--;
			p[b] = c;
			deg[p[b]]++;
			for (int n : go) {
				v[n] += value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] += value_for_other(m);
			}

			for (int n : go)
				children[p[n]].insert(n);

			for (int n : go2) {
				if (!deg[n]) continue;
				ll pv = value_for_other(n);
				mulset.insert(v[*children[n].begin()] + pv);
				mulset.insert(v[*children[n].rbegin()] + pv);
			}
		}
		if (a == 2) {
			cin >> b;
			b--;
			cout << v[b] + value_for_other(p[b]) << endl;
		}
		if (a == 3) {
			cout << *mulset.begin() << " " << *mulset.rbegin() << endl;
		}
	}
	cin >> N;
}


#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define FAST_IO ios::sync_with_stdio(false)
#define sci(d) scanf("%d", &d)
#define scs(s) scanf("%s", &s)

#define SINF 20000000
#define INF 2000000000
#define LLINF 900000000000000000LL
#define X first
#define Y second
#define MAX 1001
#define MOD 1000000007LL
#define endl "\n"
#define ALL(a) (a).begin(), (a).end()
#define pr(n) operator[](n);

typedef long long ll;
typedef pair<int, int> pi;

int Q, N, M, K, L;
ll FAN[100001];
int p[100001];
ll v[100001];

struct comp {
	bool operator() (const int& a, const int& b) const {
		if (v[a] != v[b]) return v[a] < v[b];
		return a < b;
	}
};

set<int, comp> children[100001];
int deg[100001];
multiset<ll> mulset;

ll value_for_self(int a) {
	return FAN[a] - (1 + deg[a]) * value_for_other(a);
}
ll value_for_other(int a) {
	return FAN[a] / (2 + deg[a]);
}

int main() {
	FAST_IO;
	cin >> N >> M;
	for (int i = 0; i < N; i++) cin >> FAN[i];
	for (int i = 0; i < N; i++) {
		cin >> p[i]; p[i]--;
		children[p[i]].insert(i);
		deg[p[i]]++;
	}
	for (int i = 0; i < N; i++) {
		v[i] += value_for_self(i);
		for (int child : children[i])
			v[i] += value_for_other(child);
	}
	for (int i = 0; i < N; i++) {
		children[p[i]].erase(i);
		children[p[i]].insert(i);
	}
	for (int n = 0; n < N; n++) {
		int pv = value_for_other(n);
		if (!deg[n]) continue;
		mulset.insert(v[*children[n].begin()] + pv);
		mulset.insert(v[*children[n].rbegin()] + pv);
	}
	for (int q = 0; q < M; q++) {
		int a, b, c;
		cin >> a;
		if (a == 1) {
			cin >> b >> c;
			b--; c--;

			set<int> go = { b, c, p[b], p[c], p[p[b]] };
			set<int> go2 = go;
			for (int n : go) go2.insert(p[n]);

			for (int n : go2) {
				if (!deg[n]) continue;
				ll pv = value_for_other(n);
				mulset.erase(v[*children[n].begin()] + pv);
				mulset.erase(v[*children[n].rbegin()] + pv);
			}

			for (int n : go)
				children[p[n]].erase(n);

			for (int n : go) {
				v[n] -= value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] -= value_for_other(m);
			}
			deg[p[b]]--;
			p[b] = c;
			deg[p[b]]++;
			for (int n : go) {
				v[n] += value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] += value_for_other(m);
			}

			for (int n : go)
				children[p[n]].insert(n);

			for (int n : go2) {
				if (!deg[n]) continue;
				ll pv = value_for_other(n);
				mulset.insert(v[*children[n].begin()] + pv);
				mulset.insert(v[*children[n].rbegin()] + pv);
			}
		}
		if (a == 2) {
			cin >> b;
			b--;
			cout << v[b] + value_for_other(p[b]) << endl;
		}
		if (a == 3) {
			cout << *mulset.begin() << " " << *mulset.rbegin() << endl;
		}
	}
	cin >> N;
}


#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define FAST_IO ios::sync_with_stdio(false)
#define sci(d) scanf("%d", &d)
#define scs(s) scanf("%s", &s)

#define SINF 20000000
#define INF 2000000000
#define LLINF 900000000000000000LL
#define X first
#define Y second
#define MAX 1001
#define MOD 1000000007LL
#define endl "\n"
#define ALL(a) (a).begin(), (a).end()
#define pr(n) operator[](n);

typedef long long ll;
typedef pair<int, int> pi;

int Q, N, M, K, L;
ll FAN[100001];
int p[100001];
ll v[100001];

struct comp {
	bool operator() (const int& a, const int& b) const {
		if (v[a] != v[b]) return v[a] < v[b];
		return a < b;
	}
};

set<int, comp> children[100001];
int deg[100001];
multiset<ll> mulset;

ll value_for_other(int a) {
	return FAN[a] / (2 + deg[a]);
}
ll value_for_self(int a) {
	return FAN[a] - (1 + deg[a]) * value_for_other(a);
}

int main() {
	FAST_IO;
	cin >> N >> M;
	for (int i = 0; i < N; i++) cin >> FAN[i];
	for (int i = 0; i < N; i++) {
		cin >> p[i]; p[i]--;
		children[p[i]].insert(i);
		deg[p[i]]++;
	}
	for (int i = 0; i < N; i++) {
		v[i] += value_for_self(i);
		for (int child : children[i])
			v[i] += value_for_other(child);
	}
	for (int i = 0; i < N; i++) {
		children[p[i]].erase(i);
		children[p[i]].insert(i);
	}
	for (int i = 0; i < N; i++) {
		if (children[i].empty()) continue;
		mulset.insert(v[*children[i].begin()] + value_for_other(i));
		mulset.insert(v[*children[i].rbegin()] + value_for_other(i));
	}
	for (int q = 0; q < M; q++) {
		int a, b, c;
		cin >> a;
		if (a == 1) {
			cin >> b >> c;
			b--; c--;

			set<int> go = { b, c, p[b], p[c], p[p[b]] };
			set<int> go2 = go;
			for (int n : go) go2.insert(p[n]);

			for (int n : go2) {
				if (children[i].empty()) continue;
				mulset.erase(v[*children[i].begin()] + value_for_other(i));
				mulset.erase(v[*children[i].rbegin()] + value_for_other(i));
			}

			for (int n : go)
				children[p[n]].erase(n);

			for (int n : go) {
				v[n] -= value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] -= value_for_other(m);
			}
			deg[p[b]]--;
			p[b] = c;
			deg[p[b]]++;
			for (int n : go) {
				v[n] += value_for_self(n);
				for (int m : go)
					if (p[m] == n)
						v[n] += value_for_other(m);
			}

			for (int n : go)
				children[p[n]].insert(n);

			for (int n : go2) {
				if (!deg[n]) continue;
				ll pv = value_for_other(n);
				mulset.insert(v[*children[n].begin()] + pv);
				mulset.insert(v[*children[n].rbegin()] + pv);
			}
		}
		if (a == 2) {
			cin >> b;
			b--;
			cout << v[b] + value_for_other(p[b]) << endl;
		}
		if (a == 3) {
			cout << *mulset.begin() << " " << *mulset.rbegin() << endl;
		}
	}
	cin >> N;
}


//  Created by Sengxian on 5/10/16.
//  Copyright (c) 2016年 Sengxian. ALL rights reserved.
//  Codeforces 673F 思维 + 堆
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
using namespace std;

inline int ReadInt() {
	static int n, ch;
	n = 0, ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar();
	return n;
}

typedef long long ll;
inline ll ReadLL() {
	static ll n;
	static int ch;
	n = 0, ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) n = (n << 1) + (n << 3) + ch - '0', ch = getchar();
	return n;
}

const int maxn = 100000 + 3;
const ll INF = 1LL << 60;
struct big_set {
	priority_queue<ll> q1, q2;
	set<ll> s;
	inline bool empty() {
		return !s.size();
		while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) q1.pop(), q2.pop();
		return q1.empty();
	}
	inline void insert(ll x) {q1.push(x); s.insert(x);}
	inline void erase(ll x) {
		q2.push(x);
		s.erase(x);
	}
	inline ll top() {
		assert(s.size());
		return *s.rbegin();
		while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) q1.pop(), q2.pop();
		assert(q1.empty() == false);
		if (q1.empty()) return -INF;
		return q1.top();
	}
	void print() {
		for (set<ll>::iterator it = s.begin(); it != s.end(); ++it)
			cout << *it << endl;
		cout << endl;
	}
}optb, bs[maxn];
struct small_set {
	priority_queue<ll, vector<ll>, greater<ll> > q1, q2;
	set<ll> s;
	inline bool empty() {
		return !s.empty();
		while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) q1.pop(), q2.pop();
		return q1.empty();
	}
	inline void insert(ll x) {q1.push(x);s.insert(x);}
	inline void erase(ll x) {q2.push(x);s.erase(x);}
	inline ll top() {
		return *s.begin();
		while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) q1.pop(), q2.pop();
		assert(q1.empty() == false);
		if (q1.empty()) return INF;
		return q1.top();
	}
}opts, ss[maxn];

int n, q, fa[maxn], s[maxn];
ll t[maxn], val[maxn];

inline ll cal(int u) {
	return t[u] / (s[u] + 2);
}

bool in[maxn];

//将 a 从父亲的堆里面删除
void del(int a) {
	int b = fa[a];
	bs[b].erase(val[a]);
	ss[b].erase(val[a]);
}

//将 a 加入父亲的堆里面
void add(int a) {
	int b = fa[a];
	bs[b].insert(val[a]);
	ss[b].insert(val[a]);
}

void eraseOpt(int a) {
	a = fa[a];
	ll v = cal(a);
	if (in[a] && !bs[a].empty() && !ss[a].empty()) {
		optb.erase(bs[a].top() + v);
		opts.erase(ss[a].top() + v);
		in[a] = false;
	}
}

void insertOpt(int a) {
	a = fa[a];
	ll v = cal(a);
	if (!in[a] && !bs[a].empty() && !ss[a].empty()) {
		optb.insert(bs[a].top() + v);
		opts.insert(ss[a].top() + v);
		in[a] = true;
	}
}

void modify(int a, int c) {
	int b = fa[a];
	eraseOpt(a), eraseOpt(b), eraseOpt(c), eraseOpt(fa[b]), eraseOpt(fa[c]);
	del(a), del(b), del(c), del(fa[b]), del(fa[c]);

	//更新答案
	fa[a] = c;
	ll va_other = cal(a);
	//对 b 的修改
	ll vb_other = cal(b);
	val[b] -= va_other;
	val[b] -= t[b] - (s[b] + 1) * vb_other;
	val[fa[b]] -= vb_other;
	s[b]--;
	vb_other = t[b] / (s[b] + 2);
	val[b] += t[b] - (s[b] + 1) * vb_other;
	val[fa[b]] += vb_other;
	//对 c 的修改
	ll vc_other = cal(c);
	val[c] += va_other;
	val[c] -= t[c] - (s[c] + 1) * vc_other;
	val[fa[c]] -= vc_other;
	s[c]++;
	vc_other = t[c] / (s[c] + 2);
	val[c] += t[c] - (s[c] + 1) * vc_other;
	val[fa[c]] += vc_other;

	//eraseOpt(a, vc_back), eraseOpt(b, vb_back), eraseOpt(c, vc_back), eraseOpt(fa[b], vfa_b_back), eraseOpt(fa[c], vfa_c_back);
	add(a), add(b), add(c), add(fa[b]), add(fa[c]);
	insertOpt(a), insertOpt(b), insertOpt(c), insertOpt(fa[b]), insertOpt(fa[c]);
}

int main() {
	#ifndef ONLINE_JUDGE
		freopen("test.in", "r", stdin);
	#endif
	n = ReadInt(), q = ReadInt();
	for (int i = 0; i < n; ++i) t[i] = ReadLL();
	for (int i = 0; i < n; ++i) s[fa[i] = ReadInt() - 1]++;
	for (int i = 0; i < n; ++i) {
		ll v = cal(i);
		val[fa[i]] += v, val[i] += t[i] - (s[i] + 1) * v;
	}
	for (int u = 0; u < n; ++u) {
		bs[fa[u]].insert(val[u]);
		ss[fa[u]].insert(val[u]);
	}
	memset(in, 0, sizeof in);
	for (int i = 0; i < n; ++i)
		if (s[i]) {
			optb.insert(bs[i].top() + cal(i)),
			opts.insert(ss[i].top() + cal(i));
			in[i] = true;
		}
	while (q--) {
		int type = ReadInt();
		if (type == 1) {
			int u = ReadInt() - 1, v = ReadInt() - 1;
			modify(u, v);
			optb.s.clear();
			opts.s.clear();
			for (int i = 0; i < n; ++i) if (s[i]) {
				optb.insert(bs[i].top() + cal(i));
				opts.insert(ss[i].top() + cal(i));
				in[i] = true;
			}
		} else if (type == 2) {
			int u = ReadInt() - 1;
			printf("%I64d\n", val[u] + cal(fa[u]));
		} else if (type == 3) {
			printf("%I64d %I64d\n", opts.top(), optb.top());
		} else assert(false);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#if defined(ILIKEGENTOO)
void E(){}template<class A,class...B>void E(A _,B...$){cerr<<' '<<_;E($...);}
#define E($...) E(#$,'=',$,'\n')
#else
#define E($...) ;
#endif
#define all(x) begin(x), end(x)
struct ${$(){ios_base::sync_with_stdio(false);cin.tie(nullptr);}}$;

typedef __int128_t INT;
INT lcm(INT x, INT y) {
	return x / __gcd(x, y) * y;
}

int main() {
	int n;
	cin >> n;
	vector<int> f(n);
	for (int i = 0; i < n; ++i) {
		cin >> f[i];
		--f[i];
	}
	uint64_t ans = 1;
	for (int i = 0; i < n; ++i) {
		bool ok = false;
		vector<int> g(2);
		g[1] = f[i];
		for (int i = 2;; ++i) {
			g.emplace_back(f[g[i - 1]]);
			if ((i & 1) == 0 && g[i] == g[i / 2]) {
				ans = lcm(ans, i / 2);
				ok = true;
				break;
			}
		}
		assert(ok);
	}
	assert(ans <= numeric_limits<uint64_t>::max());
	cout << uint64_t(ans) << '\n';
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define INF (1 << 30)

using namespace std;

const int N = 222;

int p[N], a[N];
int n;
int curst;
int v[N], g[N];

void run(int x, int len = 1)
{
	if (p[x] != 0)
	{
		v[curst] = len - p[x];
		g[curst] = p[x];
		return;
	}
	else
	{
		p[x] = len;
	}
	run(a[x], len + 1);
}

int main()
{
	//freopen("input.txt", "r", stdin);

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	for (int i = 1; i <= n; i++)
	{
		fill(p, p + N, 0);
		curst = i;
		run(a[i]);
	}

	int64_t res = v[1];
	int mx = 0;
	for (int i = 1; i <= n; i++)
	{
		mx = max(mx, g[i] - 1);
		res = res * v[i] / __gcd(res, v[i]);
	}

	res = (mx / res + 1) * res;

	cout << res;

	return 0;
}


#include <bits/stdc++.h>

#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define for1(i, n) for(int i = 1; i <= (int)(n); ++i)
#define fore(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ford1(i, n) for(int i = (int)(n); i>=1; --i)
#define fored(i, a, b) for(int i = (int)(b); i >= (int)(a); --i)
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<ld> vld;

template<class T> bool uax(T&a, const T& b) { if( a < b ) { a = b; return true; } return false; }
template<class T> bool uin(T&a, const T& b) { if( a > b ) { a = b; return true; } return false; }

const int inf = 1e9 + 100500;
const int MAXN = 100500;

#ifdef HOME
#define dbg(v) do {cerr << v << '\n';}while(0)
#else
#define dbg(v) do {;}while(0)
#endif

typedef __int64_t bint;

string tostr(bint x) {
  string r;
  while (x) {
    r += (char)('0' + (x%10));
    x /= 10;
  }
  if (r == "") r = "0";
  reverse(all(r));
  return r;
}

void solve() {
  int n;
  cin >> n;
  vi a(n);
  forn (i, n) {
    cin >> a[i];
    --a[i];
  }
  vi cyc(n, n+1);
  forn(i, n) {
    int x = i;
    forn(t, n + 2) {
      x = a[x];
    }
    cyc[x] = 0;
  }
  forn(t, n+2) {
    forn(i, n)
      uin(cyc[i], cyc[a[i]] + 1);
  }
  bint lans = (bint)(*max_element(all(cyc)));
  bint lca = 1;
  forn(i, n) if (cyc[i] == 0) {
    int il = 0, x = i;
    do {
      x = a[x];
      ++il;
    } while(x != i);
    bint l = il;
    lca /= __gcd(lca, l);
    lca *= l;
  }
  bint ans = lca;
  while (ans < lans)
    ans += lca;
  cout << tostr(ans) << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed; cout.precision(15);
  cerr << fixed; cerr.precision(15);
#ifdef HOME
  freopen("input.txt", "r", stdin);
#endif
  solve();
  return 0;
}


#include <bits/stdc++.h>

#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define for1(i, n) for(int i = 1; i <= (int)(n); ++i)
#define fore(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ford1(i, n) for(int i = (int)(n); i>=1; --i)
#define fored(i, a, b) for(int i = (int)(b); i >= (int)(a); --i)
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<ld> vld;

template<class T> bool uax(T&a, const T& b) { if( a < b ) { a = b; return true; } return false; }
template<class T> bool uin(T&a, const T& b) { if( a > b ) { a = b; return true; } return false; }

const int inf = 1e9 + 100500;
const int MAXN = 100500;

#ifdef HOME
#define dbg(v) do {cerr << v << '\n';}while(0)
#else
#define dbg(v) do {;}while(0)
#endif

typedef __int128_t bint;

string tostr(bint x) {
  string r;
  while (x) {
    r += (char)('0' + (x%10));
    x /= 10;
  }
  if (r == "") r = "0";
  reverse(all(r));
  return r;
}

void solve() {
  int n;
  cin >> n;
  vi a(n);
  forn (i, n) {
    cin >> a[i];
    --a[i];
  }
  vi cyc(n, n+1);
  forn(i, n) {
    int x = i;
    forn(t, n + 2) {
      x = a[x];
    }
    cyc[x] = 0;
  }
  forn(t, n+2) {
    forn(i, n)
      uin(cyc[i], cyc[a[i]] + 1);
  }
  bint lans = (bint)(*max_element(all(cyc)));
  bint lca = 1;
  forn(i, n) if (cyc[i] == 0) {
    int il = 0, x = i;
    do {
      x = a[x];
      ++il;
    } while(x != i);
    bint l = il;
    lca /= __gcd(lca, l);
    lca *= l;
  }
  bint ans = lca;
  while (ans < lans)
    ans += lca;
  cout << tostr(ans) << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed; cout.precision(15);
  cerr << fixed; cerr.precision(15);
#ifdef HOME
  freopen("input.txt", "r", stdin);
#endif
  solve();
  return 0;
}


#include <cstdio>
#include <algorithm>
#define inf 2147483647
using namespace std;
typedef long long ll;
ll c0,c1,n;
ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
ll lls[31];
ll comb(ll a,ll b){
    ll s=1,x,g;
    for(ll i=0; ++i<=b; lls[i]=i);
    for(ll i=-1; ++i<b; ){
    	x=a+b-i;
    	for(ll j=0; ++j<=b; ){
    		g=gcd(lls[j],x);
    		x/=g;
    		lls[j]/=g;
		}
		if(x>inf/s)
			return inf;
		s*=x;
	}
    return s;
}
bool check(ll x){
	ll tmp=x/c0+x/c1+1;
	if(tmp>n)
    	return 1;
    for(ll i=0; ++i<=26; ){
		for(ll j=i-1; (++j)*c0+i*c1<=x; )
			if((tmp+=comb(j,i))>n)
				return 1;
		for(ll j=i; (++j)*c1+i*c0<=x; )
			if((tmp+=comb(j,i))>n)
				return 1;
	}
	return 0;
}
void work(ll x,ll &tmp,ll &ans){
    tmp=x/c0+x/c1+1;
    ans=((x/c0)*(x/c0+1)*c0+(x/c1)*(x/c1+1)*c1)>>1;
    ll c;
    for(ll i=1;i<=26;++i){
        for(ll j=i;j*c0+i*c1<=x;++j){
            c=C(j,i);
            tmp+=c;
            ans+=c*(j*c0+i*c1);
        }
        for(ll j=i+1;j*c1+i*c0<=x;++j){
            c=C(j,i);
            tmp+=c;
            ans+=c*(j*c1+i*c0);
        }
    }
}
//void work(ll x,ll &tmp,ll &ans){
//	tmp=x/c0+x/c1+1;
//	ans=((x/c0)*(x/c0+1)*c0+(x/c1)*(x/c1+1)*c1)>>1;
//	ll c;
//	for(int i=0; ++i<=26; ){
//		for(ll j=i-1; (++j)*c0+i*c1<=x; ans+=c*(j*c0+i*c1))
//			tmp+=(c=comb(j,i));
//		for(ll j=i; (++j)*c1+i*c0<=x; ans+=c*(j*c0+i*c1))
//			tmp+=(c=comb(j,i));
//	}
//}
int main(){
	scanf("%lld%lld%lld",&n,&c0,&c1);
	--n,c0>c1&&(swap(c0,c1),1);
	if(!c0){
		printf("%lld\n",n*c1);
		return 0;
	}
	ll l=0,r=c1*26,mid;
    while(l<=r){
        mid=(l+r)>>1;
        if(check(mid))r=mid-1;
        else l=mid+1;
    }
    ll c,s;
    work(l-1,c,s);
    s+=(n-c)*l+n*(c0+c1);
    printf("%lld\n",s);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main ()
{
    int n, m, r, a, b, k;
    cin >> n >> a >> b;
    if(min(a, b) == 0)
	{
        long long res = max(a, b) * (n - 1);
		cout << res << "\n";
		return 0;
	}
    map < long long, long long > mp;
    mp[0] = 1;
    n--;
    while (n){
        long long cost = mp.begin() -> first;
        long long  cnt = mp.begin() -> second;
        if (cnt <= n)
            mp.erase(mp.begin());
        else
            mp[cost] -= n;
        int erased = min (n, cnt);
        mp[a+cost]+= erased;
        mp[b+cost]+= erased;
        n -= erased;
    }
    long long ans = 0;
    for(auto it : mp)
        ans += it.first * it.second;
    cout << ans ;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define sz(x) ((long long int)(x).size())
#define pb push_back
#define itn int
#define mp make_pair
#define pii pair<ll, ll>

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n, c0, c1;
    cin >> n >> c0 >> c1;
    if (c1 == 0 || c2 == 0)
        return !(cout << (c1 + c2) * (n - 1));
    map <ll, ll> f;
    f[0] = 1;
    --n;
    while (n > 0) {
        pii c = *(f.begin());
        if (c.second <= n) {
            f.erase(f.begin());
            f[c.first + c0] += c.second;
            f[c.first + c1] += c.second;
        }
        else {
            f[c.first] -= n;
            f[c.first + c0] += n;
            f[c.first + c1] += n;
        }
        n -= c.second;
    }
    ll ans = 0;
    for (pii x : f) {
        ans += x.first * x.second;
    }
    cout << ans;
    return 0;
}


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

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)

#ifdef ROOM_311
__attribute__((destructor)) void fini_main()
{
	fprintf(stderr, "Execution time = %0.0lf ms\n", clock() * 1000.0 / CLOCKS_PER_SEC);
}
#endif

#define MY_RAND 1
#if MY_RAND
uint64_t rnd_data = 0xDEADBEEFDULL;
inline void my_srand(int seed) { rnd_data = ((uint64_t)seed << 16) | 0x330E; }
inline int my_rand() { rnd_data = rnd_data * 0x5DEECE66DULL + 0xB; return (rnd_data >> 17) & 0x7FFFFFFF; }
#define rand my_rand
#define srand my_srand
#endif

template <class _T> inline _T sqr(const _T &x) { return x * x; }
template <class _T> inline string tostr(const _T &a) { ostringstream os(""); os << a; return os.str(); }

// Types
typedef long double ld;
typedef long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef set < int > SI;
typedef vector < int > VI;
typedef map < string, int > MSI;

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;

int n, c0, c1;
int q[102400000];
u64 b[(102400000 >> 6) + 1];

int main()
{
#ifdef ROOM_311
	freopen("input.txt", "rt", stdin);
#endif
	cout << setiosflags(ios::fixed) << setprecision(10);

	scanf("%d%d%d", &n, &c0, &c1);
	if (c0 > c1) swap(c0, c1);
	int cc = c0 + c1;
	int m = c1 + 1;
	m = (m + 63) & ~63;
	i64 ans = 0;
	q[0] = 1;
	int k = 0;
	i64 kk = 0;
	for (n--; n; )
	{
		while (!q[k])
		{
			if (!(k & 63) && !b[k >> 6]) k += 63;
			if (++k == m) k = 0, kk += m;
		}
		int add = min(n, q[k]);
		if (!(q[k] -= add)) b[k >> 6] &= ~(1ULL << k);
		ans += (k + kk + cc) * (i64)add;
		int k1 = k + c0;
		int k2 = k + c1;
		if (k1 >= m) k1 -= m;
		if (k2 >= m) k2 -= m;
		q[k1] += add;
		q[k2] += add;
		b[k1 >> 6] |= 1ULL << k1;
		b[k2 >> 6] |= 1ULL << k2;
		n -= add;
	}
	printf("%lld\n", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[100],n,b,c;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	b=a[n-1];
	sort(a,a+n);
	c=a[n-1];
	b=b^c
	cout<<b;
	return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int main(){
	int n,Max=-1e9+7,a;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a;
		if (a!=11&&Max!=11) Max=max(a,Max);
		else Max=11;
	}
	cout<<Max^a;
	return 0;
}


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

int main() {
    int n , a[100];
    cin >> n;
    for (int i = 0 ; i < n ; ++ i) {
        cin >> a[i];
    }
    cout << *max_element(a , a + n) ^ a[n - 1] << endl;
}


#include<cstdio>
#include<cmath>
using namespace std;
int n,x=-1,y;
int main(){
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&y);
        x=max(x,y);
    }
    int ans=x^y;
    printf("%d\n",ans);
    return 0;
}


#include<cstdio>
#include<cmath>
int T,p[300],cnt;
double a,b;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		a=b=cnt=0;
		for(int i=0;i<250;i++)
		{
			scanf("%d",&p[i]);
			a+=pow(p[i],2)/250.0;
		}
		for(int i=0;i<250;i++) cnt+=(abs(p[i])<2.0*sqrt(a));
		if(cnt<250) printf("poisson\n");
		else printf("uniform\n");
	}
	return 0;
}

//代码根本就没有送过去嘛


#include<cstdio>
#include<cmath>
int T,p[300],cnt;
double a,b;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		a=b=cnt=0;
		for(int i=0;i<250;i++)
		{
			scanf("%d",&p[i]);
			a+=pow(p[i],2)/250.0;
		}
		for(int i=0;i<250;i++) cnt+=(abs(p[i])<2.0*sqrt(a));
		if(cnt<250) printf("poisson\n");
		else printf("uniform\n");
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(const auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll,ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int bue[MAX_N];
double sm[MAX_N], LOG[MAX_N];

double solve1(int k, int p){
    return k * LOG[p] - p - sm[k];
}

double solve2(int p){
    return -LOG[2*p+1];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,MAX_N){
        LOG[i] = log(i);
        if(i) sm[i] = sm[i-1] + LOG[i];
    }
    rep(i,n){
        // double mean = 0;
        rep(j,250){
            cin >> bue[j];
        }
        // mean /= 250.0;
        // double hoge = 0;
        // rep(j,250){
        //     hoge += (bue[j] - mean) * (bue[j] - mean);
        // }
        // hoge /= 250.0;
        bool ans = false;
        double res = -1e300;
        rep(p,1,1001){
            double ch1 = 0, ch2 = 0;
            bool dame = false;
            rep(j,250){
                if(bue[j] > 2 * p) dame = true;
                ch1 += solve1(bue[j], p), ch2 += solve2(p);
            }
            if(dame) ch2 = -1e301;
            if(ch1 > res) res = ch1, ans = false;
            if(ch2 > res) res = ch2, ans = true;
        }
        if(ans){
            cout << "uniform\n";
        }else{
            cout << "poisson\n";
        }
        // if(abs(hoge - mean) < abs(hoge - mean * mean / 3.0)){
        //     int ans = -1;
        //     double res = -1e300;
        //     srep(p,10,1001){
        //         double ch = 0;
        //         rep(j,250){
        //             ch += solve1(bue[j], p);
        //         }
        //         if(ch > res) res = ch, ans = p;
        //     }
        //     cout<< ans << "\n";
        // }else{
        //     const int cand = (*max_element(bue, bue + 250) + 1) / 2;
        //     cout << cand << "\n";
            // if(cand < 50){
            // }else{
            //     const int ans = round(mean);
            //     cout << ans << "\n";
            // }
            // double res = -1e300;
            // srep(p,10,1001){
            //     double ch = 0;
            //     rep(j,250){
            //         ch += solve2(p);
            //     }
            //     if(ch > res) ch = res, ans = p;
            // }
        // }
    }
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 3005;

int V, oc[2*MX], mn;
ld prob0[1001][MX], prob1[1001][MX];

void gen(int mean) {
    ld a = 1;
    int divi = mean;
    F0R(i,MX) {
        prob0[mean][i] = a*exp(-divi);
        a *= mean; a /= i+1;
        while (divi && a/exp(1) >= 1) divi --, a /= exp(1);
    }
    F0R(i,2*mean+1) prob1[mean][i] = (ld)1/(2*mean+1);
}

ld getOc(int i, int mean) {
    // mean+x = i
    return oc[i-mean+MX];
}

ld dif(ld a, ld b) {
    return abs(a-b)/(a+0.0001);
}

pair<ld,ld> tri(int mean) {
    if (mean < mn) return {MOD,MOD};

    ld x = 0, y = 0;
    F0R(i,MX) {
        ld d = (ld)getOc(i,mean)/250;
        x += dif(prob0[mean][i],d);
        y += dif(prob1[mean][i],d);
    }

    return {x,y};
}

void solve() {
    F0R(i,2*MX) oc[i] = 0;
    int mn = 0;
    F0R(i,250) {
        int k = (rand() % 1501)-750;
        int k; cin >> k;
        // if (k < MX && k > -MX) oc[k+MX] ++;
        mn = min(mn,k);
    }

    ld x = MOD, y = MOD;

    FOR(mean,1,1001) {
        pair<ld,ld> p = tri(mean);
        x = min(x,p.f), y = min(y,p.s);
        // cout << p.s << "\n";
    }

    // cout << x << " " << y << "\n";
    if (x < y) cout << "poisson";
    else cout << "uniform";
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    FOR(i,1,1001) gen(i);
    cin >> V;
    F0R(i,V) solve();
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
* if you have no idea just guess the appropriate well-known algo instead of doing nothing :/
*/


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mk(a,b) make_pair(a, b);
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
typedef long long ll;
typedef double db;
const db eps = 1e-12;
const int inf = 0x3f3f3f3f;
const int N = 3e5 + 5;
const int mod = 998244353;
ll n, f[N][3], q[3];
vector<ll> g[N];
void dfs(ll u, ll fa){
    f[u][0] = 1;
    for(auto v:g[u]){
        if(v == fa) continue;
        dfs(v, u);
        q[0] = (f[u][0] * (f[v][0] + f[v][2])) % mod;
        q[1] = (f[u][0]*f[v][2] + f[u][1] * (f[v][0]+f[v][2]*2)) % mod;
        q[2] = (f[u][0]*(f[v][0]+f[v][1]) + f[u][1]*(f[v][0]+f[v][1]) + f[u][2]*(f[u][0]+f[v][2]*2)) % mod;
        swap(q,f[u]);
    }
}

int main(){
    ll t, k;
    scanf("%lld", &n);
    rep(i,2,n){
        scanf("%lld%lld", &t,&k);
        g[t].pb(k); g[k].pb(t);
    }
    dfs(1,0);
    printf("%lld\n", (f[1][0]+f[1][2]) % mod);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector <vector <long long> > G;
vector <long long> stopnie;
vector <vector <long long> > stany;
long long modulo = 998244353;
/*
stan 1 - jesli jest z synkiem LUB izolowany ( i kazde poddrzewo jest ok)
stan 2 - laczymy z tata, nie z jakims synkiem
stan 3 - laczy sie z ktoryms z synkow
*/
long long SzypkiePot(long long pod, long long pot, long long mod){
    if (pot == 0){return 1;}
    long long wynik = SzypkiePot(pod, pot/2, mod)%mod;
    if (pot % 2 == 0){ return (wynik*wynik)%mod;}
    else{  return (pod*((wynik*wynik)%mod))%mod; }
}
long long Odw(long long x, long long mod){ return SzypkiePot(x, mod-2, mod)%mod;}

void Licz(long long v, long long tata){
    cerr << "v  "<<v << "\n";
    long long stan2 = 1; //czyli polacze z tata
    for (auto w : G[v]){ // dla kazdego synka chce zeby albo sie izolowal albo laczyl z synem
        if (w == tata) {continue;} // jesli nie jest synem
        stan2 = ((stany[w][0] + stany[w][2])*stan2)%modulo; // domnazam na ile sposobow
        // cerr << "Domnazam "<<stany[w][0] + stany[w][2] << "\n";
         // moj synek moze albo sie laczyc z synem swoim albo byc (izolowany lub z synem)
    }
    long long stan3 = 0; // polacze z synkiem
    for (auto w : G[v]){ // wybieram syna do polaczenia
        if (w == tata){continue;}
        long long odwrotnosc_mod = Odw((stany[w][0] + stany[w][2]), modulo);
        // pozostalych laczymy jakos albo z synkami albo opcja (izoluj/synkowie)
        // cerr << "odwrotnosc " << odwrotnosc_mod << "\n";
        if (odwrotnosc_mod < 0){
            odwrotnosc_mod = (odwrotnosc_mod + modulo) % modulo;
        }
        long long sposoby_pozostalych = (stan2 * odwrotnosc_mod)%modulo; // wszystkie sposoby bez tego dolaczonego
        // cerr << "sposoby pozostalych "<<sposoby_pozostalych << "\n";
        long long polacz = (stany[w][1]*sposoby_pozostalych)%modulo; // tego w lacze z tata
        if (polacz < 0){
            polacz = (polacz + modulo)%modulo;
        }
        // cerr << "polacz = "<<polacz << "\n";
        stan3 = (stan3 + polacz)%modulo;
    }
    long long stan1 = 1;
    for (auto w : G[v]){
        if (w == tata){continue;}
        stan1 = (stan1 * stany[w][0])%modulo;
    }
    stan1 = (stan1 + stan3)%modulo;
    // cerr << "stany " << stan1 << " " << stan2 << " " << stan3 << "\n";
    stany[v]={stan1, stan2, stan3};
}
void DFS(long long v, long long tata){
    // cerr << "v "<<v << " tata " << tata << "\n";
    for (auto w : G[v]){
        if (w != tata){
            DFS(w, v);
        }
    }
    Licz(v, tata);
}
int main(){
    long long n;
    cin >> n;
    stopnie.resize(n);
    G.resize(n);
    stany.resize(n);
    for (long long i = 0; i < n-1; i++){
        long long u, v;
        cin >> u >> v;
        u--;
        v--;
        stopnie[u]++;
        stopnie[v]++;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    DFS(0, 0);
    // for (long long i = 0; i < n; i++){
    //     cerr << "v = "<<i+1 << "\n";
    //     cerr << stany[i][0] << " " << stany[i][1] << " " << stany[i][2] << "\n";
    // }
    cout << stany[0][0]%modulo << "\n";

}


//Thank you for supporting FastLine, (WorldWeb or james1990), (KNaga_KZ or KNaga)

#include <bits/stdc++.h>
#define off  exit( 0 )
#define cn   continue
#define rt   return
#define sc   scanf
#define pr   printf
#define pb   push_back
#define pf   push_front
#define p_f  pop_front
#define p_b  pop_back
#define ff   first
#define ss   second
#define vec  vector
#define gcd  __gcd
#define pi   acos( -1 )
#define ll   long long
#define ld   long double
#define pdd  pair < ld, ld >
#define pll  pair < ll, ll >
#define pii  pair < int, int >
#define rep( a, b, c, d )  for( int a = b; a <= c; a += d )
#define rvv( a, b, c, d )  for( int a = b; a >= c; a -= d )
#define en                 printf( "\n" )
#define sqr( a )           a * a
#define all( c )           c.begin(), c.end()
#define srt( c )           sort( all( c ) )
#define rev( c )           reverse( all( c ) )
#define srtr( c )          sort( c.rbegin(), c.rend() )
#define numcounts          __builtin_popcount
#define mxn( x, y, z )     max( x, min( y, z ) )
#define mnx( x, y, z )     min( x, max( y, z ) )
#define rep( i, x, y ) for( int i = (x); i <= (y); i ++ )
#define dep( i, x, y ) for( int i = (x); i >= (y); i -- )
#define TIME() if(QQQQ)printf("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
bool QQQQ;
#define see(x)  if(QQQQ)cout << x << " ";
#define sees(x) if(QQQQ)cout << x << endl;
#define N       105
#define NN      10005
#define M       998244353
#define I       1000000000000000000
const int dx[ ] = { 1, -1, 0, 0 };
const int dy[ ] = { 0, 0, -1, 1 };
using namespace std;

ll ttt = 1;
ll n, ans, a[N], b[N], c[N];
vector < ll > g[N];

void dfs( ll v )
{
    a[v] = c[v] = 1;
    for( ll to: g[v] )
    {
        if( !a[to] )
        {
            dfs( to );
            b[v] = ( ((a[v] * a[to]) % M) + ((b[v] * (b[to] + c[to])) % M) ) % M;
            c[v] = (c[v] * c[to]) % M;
            a[v] = (a[v] * (b[to] + c[to])) % M;
        }
    }
    c[v] = (c[v] + b[v]) % M;
}

void solve()
{
    sc( "%I64d", &n );
    for( int i = 1; i < n; i ++ )
    {
        int x, y;
        sc( "%d%d", &x, &y );
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(1);
    ans = 1;
    for( ll i: g[1] )ans = (ans * c[i]) % M;
    ans = (ans + b[1]) % M;
    pr( "%I64d", ans );
}
int main()
{
    QQQQ = 0;
    //sc( "%I64d", &ttt );
    for( int i = 1; i <= ttt; i ++ )
        solve();
    TIME();
}

/*
1
5 5
1 2 3 4 5
3 4 5 1 2
*/


//#pragma comment(linker, "/STACK:1024000000,1024000000")
/* vim: set fdm=marker */
//{{{
#define __ return
int ATTENTION() {
	/* pls IGNORE this function and go down and HACK me
	 * read this AFTER contest
	 * this function is empty and it will not be called !
	 */

	http://codeforces.com/group/Pmz2A2x7ES
	/* I created a group there.
	 * I hope with the group, people can finish
	 * past contests, which is important for
	 * increasing rating.
	 * I am still getting familiar with this function.
	 */
	 __ 0;
}
// deprecated because mac doesn't support this...
/* #include <bits/stdc++.h> */
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int iinf = 0x7fffffff;
const ll linf = ~(1LL<<63);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<ll, int> mli;
typedef map<ll, ll> mll;
template<typename T>
inline T gcd(T a, T b) {
	if(a < 0) return gcd(-a, b);
	if(b < 0) return gcd( a,-b);
	if(a < b) return gcd(b, a);
	if(b == 0) return a;
	return gcd(b, a%b);
}

ll qpow(ll a, ll n, ll mod) {
	a %= mod;
	ll ans = 1LL;
	while(n) {
		if(n & 1) ans = (ans*a % mod);
		a = (a*a % mod);
		n >>= 1;
	}
	return ans;
}

inline ll rev(ll a, ll p) {
	return qpow(a, p - 2, p);
}

#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define rep(x, st, en) for(int x = st; x < en; x++)
#define rer(x, st, en) for(int x = st; x >= en; x--)
#define pb push_back
#define mp make_pair
#define dbgm(msg, var) cerr<<#msg<<" "<<#var<<" = "<<var<<endl;
#define dbg(var) cerr<<#var<<" = "<<var<<endl;
#define X first
#define Y second
// build prefix sum
template<typename T>
inline void BPS(T* DST, T* SRC, int N, int s = 0) {
	DST[s] = SRC[s];
	rep(i, 1, N) DST[s + i] = DST[s + i - 1] + SRC[s + i];
}
inline int in() { int x; cin>>x; return x; }
#ifdef ALNDBG
namespace alndbg {
	clock_t s, e;
}
#endif

inline void tic() {
#ifdef ALNDBG
	alndbg::s = clock();
#endif
}

inline void toc() {
#ifdef ALNDBG
	alndbg::e = clock();
	cerr<<1000.0 * (alndbg::e - alndbg::s) / CLOCKS_PER_SEC<<" ms"<<endl;
#endif
}

//}}}

// -------------------- Spliters ------------------------
// BFS求最短路小心vis可能访问过
const ll mod   = 998244353ll;
const int maxn = 300005;

vector<int> E[maxn];

int n;

ll dp[maxn][3];

// dp[i][0] = sum(max(dp[sub[i]][0/1]))
// dp[i][1]

void addedge(int u, int v) {
  E[u].pb(v);
  E[v].pb(u);
}

void dfs(int u, int fa=-1) {
  dp[u][0] = 1LL;
  dp[u][1] = 1LL;
  dp[u][2] = 0;
  for(auto v : E[u]) {
    if(v == fa) continue;
    dfs(v, u);
    dp[u][1] = (dp[u][1] * ( (dp[v][0] + dp[v][2]) % mod ) % mod;
    dp[u][0] = (dp[u][0] * dp[v][0]) % mod;
  }
  for(auto v : E[u]) {
    if(v == fa) continue;
    dp[u][2] = (dp[u][2] + dp[v][1] * dp[u][1] % mod * rev(dp[v][0] + dp[v][2], mod) % mod) % mod;
  }
  dp[u][0] = (dp[u][0] + dp[u][2]) % mod;
}

void solve() {
  dfs(1);
  ll ans = dp[1][0];
  cout<<ans<<endl;
}

int main() {
  cin>>n;
  rep(i,0,n-1) {
    int u, v;
    cin>>u>>v;
    addedge(u, v);
  }
  solve();
	return 0;
}
// --USE C++11
// *-USE O2


#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse4,sse4.2,popcnt,abm,mmx,avx")
#endif
#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define rep(i, l, r) for (int i = l; i < r; ++i)
typedef long long Long;
typedef double Double;

template <class U, class V>
istream& operator>>(istream& is, pair<U, V>& p) {
  is >> p.first >> p.second;
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& x : v) {
    is >> x;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto& x : v) {
    os << x << " ";
  }
  return os;
}

const Double EPS = 1e-10;

enum Relation { LESS_THAN, EQUAL, GREATER_THAN };

bool areEqual(Double x, Double y, Double eps = EPS) {
  auto diff = abs(x - y);
  x = abs(x), y = abs(y);
  if (diff <= eps) return true;
  if (min(x, y) <= eps) return false;
  return diff <= eps * max(x, y);
}

bool isZero(Double x, Double eps = EPS) { return abs(x) <= eps; }
bool isZero(Long x) { return x == 0; }

int compareDoubles(Double x, Double y, Double eps = EPS) {
  if (areEqual(x, y, eps)) return Relation::EQUAL;
  if (x < y) return Relation::LESS_THAN;
  return Relation::GREATER_THAN;
}

template <typename T = Double>
struct Point {
  typedef Point P;
  const static P Invalid;
  const static P Origin;

  T x = 0, y = 0;
  Point(T x, T y) : x(x), y(y) {}
  Point() {}
  pair<T, T> to_pair() const { return make_pair(x, y); }
  Point operator+(const Point& p) const { return Point{x + p.x, y + p.y}; }
  Point operator-(const Point& p) const { return Point{x - p.x, y - p.y}; }
  Point operator*(T c) const { return Point(x * c, y * c); }
  Point operator/(T c) const { return Point(x / c, y / c); }
  bool operator<(const Point& p) const {
    return (*this) != p && to_pair() < p.to_pair();
  }
  bool operator>(const Point& p) const { return (*this) != p && !(*this < p); }
  bool operator==(const Point& p) const {
    return isZero(this->x - p.x) && isZero(this->y - p.y);
  }
  bool operator!=(const Point& p) const { return !(*this == p); }
  T cross(const P& p) const { return x * p.y - y * p.x; }
  T cross(const P& a, const P& b) const { return (a - *this).cross(b - *this); }
  T dot(const P& p) const { return x * p.x + y * p.y; }
  P midPoint(const P& p) const { return ((*this) + p) / 2; }
  P getVector(const P& p) const { return p - (*this); }
  T dist2(const P& p) const { return getVector(p).dist2(); }
  T dist2() const { return (*this).dot(*this); }
  Double dist(const P& p) const { return sqrt(dist2(p)); }
  Double dist() const { return sqrt(dist2()); }
  P rotateCCW90() const { return P(-y, x); }
  P rotateCCW90Around(const P& p) const {
    return p + p.getVector(*this).rotateCCW90();
  }
  // Scales the plane by 1/cos(45 degrees)
  P rotateCCW45WithScale() const { return P(x - y, x + y); }
  P rotateCCW(Double angle) const {
    return Point(x * cos(angle) - y * sin(angle),
                 x * sin(angle) + y * cos(angle));
  }

  // Project point onto line through a and b (assuming a != b).
  P projectOnLine(const P& a, const P& b) const {
    P ab = a.getVector(b);
    P ac = a.getVector(*this);
    return a + ab * ac.dot(ab) / a.dist2(b);
  }

  // Project point c onto line segment through a and b (assuming a != b).
  P projectOnSegment(const P& a, const P& b) const {
    P& c = *this;
    P ab = a.getVector(b);
    P ac = a.getVector(c);

    Double r = dot(ac, ab), d = a.dist2(b);
    if (r < 0) return a;
    if (r > d) return b;

    return a + ab * r / d;
  }

  P reflectAroundLine(const P& a, const P& b) const {
    return projectOnLine(a, b) * 2 - (*this);
  }

  friend istream& operator>>(istream& is, P& p) {
    string x, y;
    is >> x >> y;
    p.x = stod(x), p.y = stod(y);
    return is;
  }

  friend ostream& operator<<(ostream& os, const P& p) {
    return os << p.x << "," << p.y;
  }
};

template <typename T>
const Point<T> Point<T>::Invalid = Point<T>(numeric_limits<T>::max(),
                                            numeric_limits<T>::max());
template <typename T>
const Point<T> Point<T>::Origin = Point<T>(0, 0);

typedef Point<Double> Vector;
typedef Point<Double> P;
template <class P>
bool onSegment(P s, P e, P p) {
  return p.cross(s, e) == 0 && (s - p).dot(e - p) <= 0;
}
template <class P>
bool inPolygon(const vector<P>& p, const P& a, bool strict = true) {
  int cnt = 0, n = sz(p);
  rep(i, 0, n) {
    P q = p[(i + 1) % n];
    if (onSegment(p[i], q, a)) return !strict;
    // or: if (segDist(p[i], q, a) <= eps) return !strict;
    cnt ^= ((a.y < p[i].y) - (a.y < q.y)) * a.cross(p[i], q) > 0;
  }
  return cnt;
}

template <class T>
T polygonArea2(vector<Point<T>>& v) {
  T a = v.back().cross(v[0]);
  rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]);
  return a;
}

using Polygon = vector<P>;

struct Circle {
  Double r;
  P center;
};

Double segDist(const P& s, const P& e, const P& p) {
  if (s == e) return (p - s).dist();
  auto d = (e - s).dist2(), t = min(d, max(Double(.0), (p - s).dot(e - s)));
  return ((p - s) * d - (e - s) * t).dist() / d;
}

Double pointPolygonDistance(const P& point, const Polygon& polygon) {
  if (inPolygon(polygon, point)) return 0;
  Double res = 1e15;
  for (int i = 0; i < sz(polygon); ++i) {
    int j = (i + 1) % sz(polygon);
    res = min(res, segDist(polygon[i], polygon[j], point));
  }
  return res;
}

bool circleIntersectsPolygon(const Circle& circle, const Polygon& polygon) {
  return compareDoubles(pointPolygonDistance(circle.center, polygon),
                        circle.r) != Relation::GREATER_THAN;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int n;
  cin >> n >> n >> n;
  vector<Polygon> polys(n);
  vector<Double> areas;
  areas.reserve(n);
  for (auto& poly : polys) {
    int v;
    cin >> v;
    poly.resize(v);
    cin >> poly;
    areas.emplace_back(polygonArea2(poly));
  }
  cout << fixed << setprecision(4);
  int q;
  cin >> q;
  while (q--) {
    Circle circle;
    cin >> circle.center >> circle.r;
    vector<int> res;
    Double sum_areas = 0;
    for (int i = 0; i < n; ++i) {
      if (circleIntersectsPolygon(circle, polys[i])) {
        sum_areas += areas[i];
        res.emplace_back(i);
      }
    }
    cout << sum_areas / 2 << " " << sz(res) << " " << res << endl;
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#define MULTITEST

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(4e18);
const int MOD = 998244353;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int N = 20 * 1000 + 13;

int n;
pt a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d%d", &a[i].x, &a[i].y);
	return true;
}

void solve() {
	map<int, int> xs, ys;
	forn(i, n) xs[a[i].x] ^= 1, ys[a[i].y] ^= 1;
	int mn = 0;
	for (auto it : xs) mn += it.y;
	for (auto it : ys) mn += it.y;

	forn(mask, 1 << n){
		map<int, int> xs2[2], ys2[2];
		forn(i, n){
			int c = (mask >> i) & 1;
			xs2[c][a[i].x] += 1;
			ys2[c][a[i].y] += 1;
		}
		int cur = 0;
		for (auto it : xs) cur += abs(xs2[0][it.x] - xs2[1][it.x]);
		for (auto it : ys) cur += abs(ys2[0][it.x] - ys2[1][it.x]);
		if (cur == mn){
			cerr << n << endl;
			forn(i, n) cerr << a[i].x << " " << a[i].y << " " << int((mask >> i) & 1) << endl;
			assert(mask == 0);
			return;
		}
	}
	//cerr << mn << endl;
	assert(false);
}

bool gen(){
	n = 9;
	forn(i, n) a[i] = mp(rnd() % 7, rnd() % 7);
	return true;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();
#else
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while (gen()){
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse4,sse4.2,popcnt,abm,mmx,avx")
#endif
#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define rep(i, l, r) for (int i = l; i < r; ++i)
typedef long long Long;
typedef double Double;

template <class U, class V>
istream& operator>>(istream& is, pair<U, V>& p) {
  is >> p.first >> p.second;
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& x : v) {
    is >> x;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto& x : v) {
    os << x << " ";
  }
  return os;
}

const Double EPS = 1e-10;

enum Relation { LESS_THAN, EQUAL, GREATER_THAN };

bool areEqual(Double x, Double y, Double eps = EPS) {
  auto diff = abs(x - y);
  x = abs(x), y = abs(y);
  if (diff <= eps) return true;
  if (min(x, y) <= eps) return false;
  return diff <= eps * max(x, y);
}

bool isZero(Double x, Double eps = EPS) { return abs(x) <= eps; }
bool isZero(Long x) { return x == 0; }

int compareDoubles(Double x, Double y, Double eps = EPS) {
  if (areEqual(x, y, eps)) return Relation::EQUAL;
  if (x < y) return Relation::LESS_THAN;
  return Relation::GREATER_THAN;
}

template <typename T = Double>
struct Point {
  typedef Point P;
  const static P Invalid;
  const static P Origin;

  T x = 0, y = 0;
  Point(T x, T y) : x(x), y(y) {}
  Point() {}
  pair<T, T> to_pair() const { return make_pair(x, y); }
  Point operator+(const Point& p) const { return Point{x + p.x, y + p.y}; }
  Point operator-(const Point& p) const { return Point{x - p.x, y - p.y}; }
  Point operator*(T c) const { return Point(x * c, y * c); }
  Point operator/(T c) const { return Point(x / c, y / c); }
  bool operator<(const Point& p) const {
    return (*this) != p && to_pair() < p.to_pair();
  }
  bool operator>(const Point& p) const { return (*this) != p && !(*this < p); }
  bool operator==(const Point& p) const {
    return isZero(this->x - p.x) && isZero(this->y - p.y);
  }
  bool operator!=(const Point& p) const { return !(*this == p); }
  T cross(const P& p) const { return x * p.y - y * p.x; }
  T cross(const P& a, const P& b) const { return (a - *this).cross(b - *this); }
  T dot(const P& p) const { return x * p.x + y * p.y; }
  P midPoint(const P& p) const { return ((*this) + p) / 2; }
  P getVector(const P& p) const { return p - (*this); }
  T dist2(const P& p) const { return getVector(p).dist2(); }
  T dist2() const { return (*this).dot(*this); }
  Double dist(const P& p) const { return sqrt(dist2(p)); }
  Double dist() const { return sqrt(dist2()); }
  P rotateCCW90() const { return P(-y, x); }
  P rotateCCW90Around(const P& p) const {
    return p + p.getVector(*this).rotateCCW90();
  }
  // Scales the plane by 1/cos(45 degrees)
  P rotateCCW45WithScale() const { return P(x - y, x + y); }
  P rotateCCW(Double angle) const {
    return Point(x * cos(angle) - y * sin(angle),
                 x * sin(angle) + y * cos(angle));
  }

  // Project point onto line through a and b (assuming a != b).
  P projectOnLine(const P& a, const P& b) const {
    P ab = a.getVector(b);
    P ac = a.getVector(*this);
    return a + ab * ac.dot(ab) / a.dist2(b);
  }

  // Project point c onto line segment through a and b (assuming a != b).
  P projectOnSegment(const P& a, const P& b) const {
    P& c = *this;
    P ab = a.getVector(b);
    P ac = a.getVector(c);

    Double r = dot(ac, ab), d = a.dist2(b);
    if (r < 0) return a;
    if (r > d) return b;

    return a + ab * r / d;
  }

  P reflectAroundLine(const P& a, const P& b) const {
    return projectOnLine(a, b) * 2 - (*this);
  }

  friend istream& operator>>(istream& is, P& p) { return is >> p.x >> p.y; }

  friend ostream& operator<<(ostream& os, const P& p) {
    return os << p.x << "," << p.y;
  }
};

template <typename T>
const Point<T> Point<T>::Invalid = Point<T>(numeric_limits<T>::max(),
                                            numeric_limits<T>::max());
template <typename T>
const Point<T> Point<T>::Origin = Point<T>(0, 0);

typedef Point<Double> Vector;
typedef Point<Double> P;

template <class P>
bool inPolygon(const vector<P>& p, const P& a) {
  int cnt = 0, n = sz(p);
  rep(i, 0, n - 1) {
    P q = p[i + 1];
    // if (onSegment(p[i], q, a)) return !strict;
    // or: if (segDist(p[i], q, a) <= eps) return !strict;
    cnt ^= ((a.y < p[i].y) - (a.y < q.y)) * a.cross(p[i], q) > 0;
  }
  return cnt;
}

template <class T>
T polygonArea2(vector<Point<T>>& v) {
  T a = v.back().cross(v[0]);
  rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]);
  return a;
}

using Polygon = vector<P>;

struct Circle {
  Double r;
  P center;
};

Double segDist(const P& s, const P& e, const P& p) {
  if (s == e) return (p - s).dist();
  auto d = (e - s).dist2(), t = min(d, max(Double(.0), (p - s).dot(e - s)));
  return ((p - s) * d - (e - s) * t).dist() / d;
}

Double pointPolygonDistance(const P& point, const Polygon& polygon) {
  if (inPolygon(polygon, point)) return 0;
  Double res = 1e15;
  for (int i = 0; i + 1 < sz(polygon); ++i) {
    res = min(res, segDist(polygon[i], polygon[i + 1], point));
  }
  return res;
}

bool circleIntersectsPolygon(const Circle& circle, const Polygon& polygon) {
  return compareDoubles(pointPolygonDistance(circle.center, polygon),
                        circle.r) != Relation::GREATER_THAN;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int n;
  Double tmp;

  cin >> tmp >> tmp >> n;
  vector<Polygon> polys(n);
  vector<Double> areas;
  areas.reserve(n);
  for (auto& poly : polys) {
    int v;
    cin >> v;
    poly.resize(v);
    cin >> poly;
    areas.emplace_back(polygonArea2(poly));
    poly.emplace_back(poly[0]);

    for (int i = 0; i < v; ++i) {
      assert(poly[i] != poly[i + 1]);
    }
  }
  cout << fixed << setprecision(4);
  int q;
  cin >> q;
  while (q--) {
    Circle circle;
    cin >> circle.center >> circle.r;
    vector<int> res;
    Double sum_areas = 0;
    for (int i = 0; i < n; ++i) {
      if (circleIntersectsPolygon(circle, polys[i])) {
        sum_areas += areas[i];
        res.emplace_back(i);
      }
    }
    cout << sum_areas / 2 << " " << sz(res) << " " << res << endl;
  }

  return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops","omit-frame-pointer","inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC option("arch=native","no-zero-upper") //Enable AVX

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<ll,ll> pii;
typedef pair<ld,ll> pdi;
typedef pair<ld,ld> pdd;
typedef pair<ld,pii> pdp;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<string,string> pss;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;
typedef complex<ld> point;
typedef vector<point> polygon;
typedef pair<point,int> ppi;
#define prec(n) cout.precision(n); cout<<fixed
#define mod (ll)(1e9+7)
#define eps (1e-9)
const ll oo=(ll)(1e9);
#define pi acos(-1)
#define MAXN (ll)(1e6+5)

int szs[MAXN];
polygon ps[MAXN];
vector<ld> ds[MAXN];
ld as[MAXN];

point read()
{
    ld x,y;
    cin >> x >> y;
    return point(x,y);
}

#define NEXT(i) (((i) + 1) % n)

struct line { point p, q; };
using segment = line;

double dot(point a, point b) { return real(conj(a) * b); }

ld cross(point a, point b) { return imag(conj(a) * b); }

int ccw(point a, point b, point c)
{
	b -= a; c -= a;
	if (cross(b, c) > 0) return +1; // counter clockwise
	if (cross(b, c) < 0) return -1; // clockwise
	if (dot(b, c) < 0) return +2; // c--a--b on line
	if (dot(b, b) < dot(c, c)) return -2; // a--b--c on line
	return 0;
}

bool intersectSS(const segment &s, const segment &t)
{
	return ccw(s.p, s.q, t.p) * ccw(s.p, s.q, t.q) <= 0
			&& ccw(t.p, t.q, s.p) * ccw(t.p, t.q, s.q) <= 0;
}

ld area2(const polygon &P)
{
	ld A = 0;
	for (int i = 0, n = P.size(); i < n; ++i)
		A += cross(P[i], P[NEXT(i)]);
	return A/2;
}

ld min_dis(ld ab,ld ac,ld bc)
{
    ld cos_abc=(ac*ac-ab*ab-bc*bc)/(-2.0*ab*bc);
    ld cos_acb=(ab*ab-ac*ac-bc*bc)/(-2.0*ac*bc);
    if(min(cos_abc,cos_acb)<=eps)
        return min(ab,ac);

    return sqrt(1-cos_abc*cos_abc)*ab;
}

ld give_me_random_ang()
{
    ld ang=rand()%10000;
    ang/=10000;
    ang*=2*pi;
    return ang;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    prec(6);

    srand(time(0));

    ld w,h;
    int n;
    cin >> w >> h >> n;
    for(int i=0;i<n;i++)
    {
        cin >> szs[i];
        for(int j=0;j<szs[i];j++)
        {
            point p=read();
            ps[i].push_back(p);
        }
        for(int j=0;j<szs[i];j++)
            ds[i].push_back(abs(ps[i][j]-ps[i][(j+1)%szs[i]]));
        as[i]=area2(ps[i]);
    }

    int q;
    cin >> q;
    while(q--)
    {
        ld xx,yy,rr;
        cin >> xx >> yy >> rr;
        point c=point(xx,yy);
        ld r=rr;
        ld a=0;
        vector<int> indexes;
        for(int i=0;i<n;i++)
        {
            vector<ld> ds2(szs[i]);
            bool ok=0;
            int c1=0;
            int c2=0;
            ld a1=give_me_random_ang();
//            ld a2=give_me_random_ang();
            segment l1={c,c+point(oo*cos(a1),oo*sin(a1))};
//            segment l2={c,c+point(oo*cos(a2),oo*sin(a2))};
            for(int j=0;j<szs[i];j++)
            {
                ds2[j]=abs(c-ps[i][j]);
//                c1+=intersectSS(l1,{ps[i][j],ps[i][(j+1)%szs[i]]});
                c2+=intersectSS(l2,{ps[i][j],ps[i][(j+1)%szs[i]]});
            }

            ok=(ok|((c1&1)||(c2&1)));

            for(int j=0;j<szs[i] && !ok;j++)
                ok=(ok|(min_dis(ds2[j],ds2[(j+1)%szs[i]],ds[i][j]))<=r+eps);
            if(ok)
            {
                a+=as[i];
                indexes.push_back(i);
            }
        }

        cout << a << ' ' << indexes.size();
        for(auto y : indexes)
            cout << ' ' << y;
        cout << '\n';
    }

    return 0;
}
/**
10 10 3
4 2 2 3 2 3 3 2 3
3 3.5 2 4.5 2 4.5 3
4 7 8 7.5 8.5 8 8 7.5 9
1
5 2.5 0.5

100 100 1
4 0 0 4 0 4 4 0 4
1
2 2 1
**/


#include <iostream>
#include <cassert>
#include <vector>

#define dvs 100000000LL

using namespace std;

long long to_find, res;
long long mlt[2][2], tmp[2][2], mr[2][2];
long long period[14];

long long mult(const long long &p1, const long long &p2, const long long &md){
	long long a = p1 / dvs, b = p1 % dvs;
	long long c = p2 / dvs, d = p2 % dvs;
	long long tmp = (((dvs * dvs % md) * a  % md) * c % md + (dvs * d % md) * a % md + (dvs * b % md) * c % md + b * d % md) % md;
	return tmp;
}

long long number(const long long &pos, const long long &p){
	mlt = {{1, 1}, {1, 0}};
	mr = {{1, 0}, {0, 1}};
	long long cur = pos - 1;
	if(!pos)return 0;
	while(cur){
		if(cur % 2){
			for(int i = 0; i < 2; ++i)
				for(int j = 0; j < 2; ++j){
					tmp[i][j] = 0;
					for(int k = 0; k < 2; ++k)
						tmp[i][j] = (tmp[i][j] + mult(mr[i][k], mlt[k][j], p)) % p;
				}
			for(int i = 0; i < 2; ++i)
				for(int j = 0; j < 2; ++j)
					mr[i][j] = tmp[i][j];
			--cur;
		}
		cur >>= 1;
		for(int i = 0; i < 2; ++i)
			for(int j = 0; j < 2; ++j){
				tmp[i][j] = 0;
				for(int k = 0; k < 2; ++k)
					tmp[i][j] = (tmp[i][j] + mult(mlt[i][k], mlt[k][j], p)) % p;
			}
		for(int i = 0; i < 2; ++i)
			for(int j = 0; j < 2; ++j)
				mlt[i][j] = tmp[i][j];
	}
	return(mr[0][0]);
}

void init_per(){
	period[1] = 60;
	period[2] = 300;
	period[3] = 1500;
	for(int i = 4; i < 14; ++i)period[i] = period[i - 1] * 10;
}

inline void input_data(){cin >> to_find;}
inline void output_data(){cout << res << "\n";}

int calc(){
	res = -1;
	long long cur = 10000000000000LL;
	//long long cur = 100;
	init_per();
	vector< long long > sm;
	vector< long long > tmp;
	long long cur_n = 10, cur_mod = to_find % cur_n;
	if(!cur_mod)sm.push_back(0);
	if(cur_mod == 1){sm.push_back(1); sm.push_back(2);}
	int mp = 1, pr = 1, itr = 1;
	for(int i = 3; i <= 59; ++i){
		int tmp = (mp + pr) % cur_n;
		if(cur_mod == tmp)sm.push_back(i);
		pr = mp; mp = tmp;
	}
	//cerr << "go\n";
	while(cur_n != cur){
		//cerr << "cur_n = " << cur_n << "\n";
		long long prd = period[itr], next_prd = period[++itr];
		//cerr << prd << " is prd\n" << next_prd << " is next_prd\n";
		cur_n *= 10;
		cur_mod = to_find % cur_n;
		tmp.clear();
		long long now = 0;
		while(now < next_prd){
			for(int i = 0; i < sm.size(); ++i){
				long long nmb = number(sm.at(i) + now, cur_n);
				//cout << "nmb = " << nmb << "\n";
				if(nmb == cur_mod)tmp.push_back(sm.at(i) + now);
			}
			now += prd;
			//cerr << "now = " << now << "\n";
		}
		if(tmp.empty())return 0;
		sm = tmp;
	}
	res = sm.at(0);
	return 0;
}

int main(){
	input_data();
	calc();
	output_data();
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<string.h>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long  LL;

const int MAX_SEG = 9765625 * 5;
const LL M = (LL)1e13;
const int MAX_CIR = 39062500 * 5;

LL A[ 4 ], B[ 4 ], C[ 4 ], D[ 4 ], p;

LL mul( LL a, LL b, LL p) {
	a %= p; b %= p;
	LL ret=0;
	while(b){
		if(b&1) if((ret+=a)>=p) ret-=p;
		if((a<<=1) >= p) a-=p;
		b>>=1;
	}
	return ret;
}

void mul( LL A[ ], LL B[ ], LL C[ ] , LL p){
     C[ 0 ] = (mul(A[ 0 ] , B[ 0 ], p) + mul(A[ 1 ] , B[ 2 ],p)) % p;
     C[ 1 ] = (mul(A[ 0 ] , B[ 1 ],p) + mul(A[ 1 ] , B[ 3 ],p)) % p;
     C[ 2 ] = (mul(A[ 2 ] , B[ 0 ],p) + mul(A[ 3 ] , B[ 2 ],p)) % p;
     C[ 3 ] = (mul(A[ 2 ] , B[ 1 ],p) + mul(A[ 3 ] , B[ 3 ],p)) % p;
     A[ 0 ] = C[ 0 ];
     A[ 1 ] = C[ 1 ];
     A[ 2 ] = C[ 2 ];
     A[ 3 ] = C[ 3 ];
}

LL fibonacci( LL n , LL p ) {
	if(!n) return 0;
	-- n;
    // A^n
    // ini matrix A
    A[ 0 ] = A[ 1 ] = A[ 2 ] = 1;A[ 3 ] = 0;
    // D is ans
    D[ 0 ] = D[ 3 ] = 1;
    D[ 1 ] = D[ 2 ] = 0;
    // pow
    while( n ) {
        if( n & 0x1) mul( D, A, C, p) ;
        mul( A, A, C, p) ;
        n >>= 1;
    }
    return D[0];
}
vector< int > pos ;

void pre(){
	int a, b, c,rem = p % MAX_SEG;
	pos.clear();
	a = 0; if(a == rem ) pos.push_back( 0 );
	b = 1; if(b == rem ) pos.push_back( 1 );
	int i;
	for(i=2;i<=MAX_CIR;++i){
		c=a+b;
		if(c>=MAX_SEG) c-=MAX_SEG;
		if(c == rem) pos.push_back( i );
		a=b;
		b=c;
		if( pos.size() == 4) break ;
	}
	//puts("ok");
}

int main(){
    while(1 == scanf("%I64d", &p)) {
		if(p <= 1) {
		    cout << (int)p << endl;
			continue;
		}
		pre();
		LL ans = (LL) 1e17;
		int i;
		for(i=0;i<pos.size();++i){
			LL now = pos[i];
			if(now <= 1) now += MAX_CIR;
			LL E[4], F[4], G[4];
			fibonacci( MAX_CIR + 1 , M);memcpy(E,D,sizeof D);
			fibonacci( now, M );memcpy(F,D,sizeof D);
			while(now < (LL)(1e13 + 5e12) && now <= ans) {
				if( F[0] == p ) {
					if(now < ans )
						ans = now;
				}
				now += MAX_CIR;
				mul( F, E, G, M );
			}
		}
		if(ans > 1e16) ans = - 1;
cout<<ans<<endl
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define lim 10000000000000LL
using namespace std;
struct matrix
{   long long num[5][5];
    matrix()
    {   memset(num,0,sizeof(num));
    }
};
matrix ft,unit;
long long next[1505];
long long cur[1505];
long long f[1505];
long long n;
long longRE mul(long long a,long long b)
{   long long p=(long double)a*b/lim;
    long long r=a*b-lim*p;
    if (r<0)
        r+=lim;
    return r;
}
matrix operator *(matrix& a,matrix& b)
{   long long i,j,k;
    matrix c;
    for (i=1;i<=2;++i)
        for (j=1;j<=2;++j)
            for (k=1;k<=2;++k)
            {   c.num[i][j]+=mul(a.num[i][k],b.num[k][j]);
                c.num[i][j]%=lim;
            }
    return c;
}
matrix qp(matrix b,long long e)
{   matrix ans,t;
    t=b,ans=unit;
    while (e)
    {   if (e&1)
            ans=ans*t;
        t=t*t;
        e>>=1;
    }
    return ans;
}
long long fib(long long n)
{   matrix f;
    if (n==0)
        return 0;
    if (n==1)
        return 1;
    f=qp(ft,n);
    return f.num[1][2]%lim;
}
long long work()
{   if (n==0)
        return 0;
    if (n==1)
        return 1;
    long long cnt,cnt1,i,j,k,cycle,t;
    cnt=0;
    f[0]=0,f[1]=1;
    for (i=2;i<1500;++i)
        f[i]=(f[i-1]+f[i-2])%lim;
    for (i=0;i<1500;++i)
        if (f[i]%1000==n%1000)
            cur[++cnt]=i;
    cycle=1500;
    for (i=10000;i<=lim;i*=10,cycle*=10)
    {   cnt1=0;
        for (j=0;j<=9;++j)
            for (k=1;k<=cnt;++k)
                if (fib(cycle*j+cur[k])%i==n%i)
                    next[++cnt1]=cycle*j+cur[k];
        cnt=cnt1;
        swap(cur,next);
    }
    if (cnt==0)
        return -1;
    return cur[1];
}
int main()
{   scanf("%I64d",&n);
    unit.num[1][1]=unit.num[2][2]=1;
    ft.num[1][2]=ft.num[2][1]=ft.num[2][2]=1;
    printf("%I64d\n",work());
    return 0;
}


#ifdef APTEST
#include "debug_tools/program.h"
#endif
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const unsigned int maxn = 100, maxm = 5000;
using Real = double;
constexpr Real eps = 1e-9;

struct Edge
{
    unsigned int from, to;
    int c;
} ed[maxm + 1];
unsigned int degree[maxn + 1];
Real equ[maxn + 1][maxn + 1], f[maxn + 1], flw[maxn + 1];

static void buildEquation(const unsigned int n, const unsigned int m)
{
    for (unsigned int i = 0; i < n; ++i)
        equ[i][i] = degree[i];
    for (const Edge* i = ed; i < ed + m; ++i)
    {
        if (i->from != n - 1 && i->from != 0)
            --equ[i->from][i->to];
        if (i->to != n - 1 && i->to != 0)
            --equ[i->to][i->from];
    }
    equ[0][0] = equ[n - 1][n - 1] = 1;
    equ[0][n] = 0;
    equ[n - 1][n] = 1;
}
inline bool isZero(const Real v)
{
    return fabs(v) <= eps;
}
void gaussian(const unsigned int n)
{
    for (unsigned int i = 0; i < n; ++i)
    {
        if (isZero(equ[i][i]))
        {
            for (unsigned int j = i + 1; j < n; ++j)
                if (!isZero(equ[j][i]))
                {
                    swap_ranges(equ[j], equ[j] + n + 1, equ[i]);
                    break;
                }
        }
        if (isZero(equ[i][i]))
            continue;
        for (unsigned int j = i + 1; j < n; ++j)
        {
            if (isZero(equ[j][i]))
                continue;
            const Real rat = equ[j][i] / equ[i][i];
            for (unsigned int k = i; k <= n; ++k)
                equ[j][k] -= rat * equ[i][k];
        }
    }
    for (unsigned int j = n - 1; j > 0; --j)
    {
        if (isZero(equ[j][j]))
            continue;
        equ[j][n] /= equ[j][j];
        f[j] = equ[j][n];
        for (unsigned int i = 0; i < j; ++i)
            equ[i][n] -= equ[i][j] * equ[j][n];
    }
}
static Real buildSolution(const unsigned int n, const unsigned int m)
{
    Real miv = numeric_limits<Real>::max();
    for (const Edge* i = ed; i < ed + m; ++i)
        if (!isZero(f[i->to] - f[i->from]))
            miv = min(miv, fabs(i->c / (f[i->to] - f[i->from])));
    for (unsigned int i = 0; i < n; ++i)
        f[i] *= miv;
    Real ret = 0;
    for (const Edge* i = ed; i < ed + m; ++i)
    {
        if (i->to == n - 1 && f[i->from] < f[n - 1])
            ret += f[n - 1] - f[i->from];
        else if (i->from == n - 1 && f[i->to] < f[n - 1])
            ret += f[n - 1] - f[i->to];
    }
    return ret;
}
int main()
{
#ifndef APTEST
    ios_base::sync_with_stdio(false);
#endif

    feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW);

    unsigned int n, m;
    cin >> n >> m;
    for (Edge* i = ed; i < ed + m; ++i)
    {
        cin >> i->from >> i->to >> i->c;
        --(i->from);
        --(i->to);
        ++degree[i->from];
        ++degree[i->to];
    }
    buildEquation(n, m);
    gaussian(n);
    ios_base::sync_with_stdio(true);
    printf("%.5lf\n", buildSolution(n, m));
    for (const Edge* i = ed; i < ed + m; ++i)
        printf("%.5lf\n", f[i->to] - f[i->from]);
    return 0;
}


#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

int a[5005],b[5005],c[5005];
double m[102][102];

int main() {
//  freopen("input.txt","r", stdin);
//  freopen("output.txt", "w", stdout);
    int n,e,deg=0;
    scanf("%d%d",&n,&e);
    for(int i=0;i<e;++i) {
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
        --a[i]; --b[i];
    }
    for(int i=1;i<n-1;++i) {
        for(int j=0;j<e;++j) {
            if (a[j]==i) {
                m[i-1][b[j]]+=1;
                m[i-1][a[j]]-=1;
            }
            if (b[j]==i) {
                m[i-1][b[j]]-=1;
                m[i-1][a[j]]+=1;
            }
        }
    }
    const double eps=1e-8;
    for(int i=0;i<n-2;++i) {
        int ind=-1;
        for(int j=i;j<n-2;++j)
            if (fabs(m[j][i+1])>eps) {
                ind=j;
                break;
            }
        if (ind==-1) {
//          puts("fail\n");
//          return 0;
            continue;
        }
        for(int j=0;j<n;++j)
            swap(m[i][j],m[ind][j]);
        double a=m[i][i+1];
        for(int j=i+1;j<n;++j)
            m[i][j]/=a;
        for(int j=i+1;j<n-2;++j) {
            double coef=-m[j][i+1];
            for(int t=i+1;t<n;++t)
                m[j][t]+=m[i][t]*coef;
        }
    }
    for(int i=n-3;i>=0;--i) {
        for(int j=i-1;j>=0;--j) {
            double coef=-m[j][i+1];
            for(int t=i+1;t<n;++t)
                m[j][t]+=m[i][t]*coef;
        }
    }
    m[n-2][n-2]=1; m[n-2][n-1]=-1;
/*  for(int i=0;i<=n-2;++i) {
        for(int j=0;j<n;++j)
            cout << m[i][j] << "    ";
        cout << endl;
    }*/
    double l=0,r=1e+25;
    for(int i=0;i<e;++i) {
        if (fabs((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0))<eps) continue;
        if ((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)>0)
            r=min(r,c[i]/((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)));
        else
            l=max(l,c[i]/((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)));
        if ((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)<0)
            r=min(r,-c[i]/((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)));
        else
            l=max(l,-c[i]/((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)));
//      l=max(l,c[i]/(m[a[i]][n-1]-m[b[i]][n-1]));
    }
    if (n==100&&e=197)
        cout << l << "  " << r << endl;
    if (l-eps>r) r=0;
    double ans=0;
    for(int i=0;i<e;++i) {
        if (b[i]==n-1)
            ans+=r*((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0));
        if (a[i]==n-1)
            ans-=r*((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0));
    }
    printf("%.8lf\n",ans);
    for(int i=0;i<e;++i) {
        printf("%.8lf\n",r*((a[i] ? m[a[i]-1][n-1] : 0)-(b[i] ? m[b[i]-1][n-1] : 0)));
    }
    return 0;
}


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

const int maxn=109,maxm=10009;
const double EPS=1e-8;
//高斯消元，系数矩阵为a[i][j],i=1…n,j=1…n,常数为a[i][n+1],i=1…n，答案存在x[i]
double x[maxn], a[maxn][maxn];
void guass(int n) {
    for(int i=1;i<=n;i++) {
        int j = -1;
        for(int k=i;k<=n;k++) {
            if (abs(a[k][i])>EPS) {
                j=k; break;
            }
        }
        if (j==-1) continue;
        if (i!=j) for(int k=i;k<=n+1;k++) swap(a[i][k], a[j][k]);
        for(int k=i+1;k<=n;k++) {
            double x = a[i][i], y = a[k][i];
            if (abs(y) < EPS) continue;
            for(int p=i;p<=n+1;p++) {
                a[k][p] = a[i][p] - x / y * a[k][p];
            }
        }
    }
    for(int i=n;i>=1;i--) {
        double tmp = 0;
        for(int  j=i+1;j<=n;j++) {
            tmp += a[i][j] * x[j];
        }
        tmp = a[i][n+1] - tmp;
        if (abs(a[i][i])>EPS)
            x[i] = tmp / a[i][i];
        else {

        }
    }
}

int u[maxm],v[maxm];double val[maxm];

int main(){
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int &U=u[i],&V=v[i];
        scanf("%d%d%lf",&U,&V,val+i);
        if(U<n&&U>1)a[U][V]--,a[U][U]++;
        if(V<n&&V>1)a[V][U]--,a[V][V]++;
    }
    a[1][1]=a[n][n]=1;a[1][n+1]=1;a[n][n+1]=2;

    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            printf("%3.0f ",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");*/

    guass(n);

    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            printf("%3.0f ",a[i][j]);
        }
        printf("\n");
    }

    for(int i=1;i<=n;i++){
        printf("%d :%.4f\n",i,a[i][n+1]);
    }*/

    double mul=1e18;
    for(int i=1;i<=m;i++){
        int &U=u[i],&V=v[i];
        mul=min(mul,val[i]/fabs(x[U]-x[V]));
    }
    if(n==100&m==9)printf("mul=%.4f\n",mul)
    double ans=0;
    for(int i=1;i<=m;i++){
        int &U=u[i],&V=v[i];
        val[i]=-(x[U]-x[V])*mul;
        if(U==1||V==1)
            ans+=fabs(val[i]);
    }
    printf("%.10f\n",ans);
    for(int i=1;i<=m;i++){
        printf("%.10f\n",val[i]);
    }
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 110
#define K 5100
using namespace std;
long double m[N][N], x[N];
int a[N][N], u[N], y[N], s[K], e[K];
vector <int> v[N], d[N];
void dfs(int i)
{
    int j;
    u[i]=1;
    for(j=0; j<v[i].size(); a[i][v[i][j]]++, a[i][i]--, j++)
        if(!u[v[i][j]]) dfs(v[i][j]);
}
int main()
{
    int n, k, i, j, t, l;
    long double b, z;
    for(scanf("%d%d", &n, &l), k=0; k<l; k++)
    {
        scanf("%d%d%d", &i, &j, &t);
        i--;
        j--;
        s[k]=i;
        e[k]=j;
        v[i].push_back(j);
        v[j].push_back(i);
        d[i].push_back(t);
        d[j].push_back(t);
    }
    dfs(0); a[0][0]--;
    for(i=0; i<n-1; m[i][j]=-a[i][0], i++)
        for(j=0; j<n-1; m[i][j]=a[i][j+1], j++);
    for(i=0; i<n-1; i++)
    {
        if(!u[i]) continue;
        for(j=0, t=0; t<n-1; j=fabs(m[i][t])>fabs(m[i][j])?t:j, t++);
        if(fabs(m[i][j])<1E-9) break;
        y[i]=j;
        for(k=0; k<n-1; k++)
            if(k!=i)
                for(z=m[k][j]/m[i][j], t=0; t<n; m[k][t]-=m[i][t]*z, t++);

    }
    if(i<n-1)
    {
        for(printf("0.0\n"), i=0; i<l; printf("0.0\n"), i++);
        return 0;
    }
    for(x[0]=1, i=0; i<n-1; i++)
        if(u[i]) x[y[i]+1]=m[i][n-1]/m[i][y[i]];
    for(b=1E20, i=0; i<n; i++)
        if(u[i])
            for(j=0; j<v[i].size(); j++)
            {
                z=fabs(x[v[i][j]]-x[i]);
                if(z>1E-9 && d[i][j]/z<b) b=d[i][j]/z;
            }
    printf("%.13lf\n", (double)b);
    for(i=0; i<l; printf("%.13lf\n", (double)((x[e[i]]-x[s[i]])*b)), i++);
    int temp=0;
    if(temp==1)
    cout<<1<<endl;
    return 0;
}


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

int main()
{
	int a, h, w;
	cin >> a >> h >> w;

	if (a <= 1000000)
	{
		int x = max(1, (w - a) / (2 * a)), y = max(1, (h - a) / (2 * a));
		double ans = 1e100;
		while (w >= a * x && h >= a * y)
		{
			double d1 = double(w - a * x) / (x + 1);
			double d2 = double(h - a * y) / (y + 1);

			if ((w - a * x) * 1ll * (y + 1) == (h - a * y) * 1ll * (x + 1))
			{
				ans = min(ans, d1);
				++x; ++y;
			}
			else if (d1 > d2) ++x;
			else ++y;
		}

		if (ans == 1e100) printf("-1\n");
		else printf("%.9f\n", ans);
	}
	else
	{
		double ans = 1e100;
		for (int x = 1; w >= a * x; x++)
		{
			for (int y = 1; h >= a * y; y++)
			{
				if ((w - a * x) * 1ll * (y + 1) == (h - a * y) * 1ll * (x + 1)) ans = min(ans, double(w - a * x) / (x + 1));
			}
		}

		if (ans == 1e100) printf("-1\n");
		else printf("%.9f\n", ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const double eps = (double)(1e-18);
const double inf = (double)(1e9);

ll a, h, w;

int main(){
	cin >> a >> w >> h;
	if(w % a == 0 && h % a == 0){
		cout << 0.0;
		return 0;
	}
	double ans = inf;
	if(a <= 10){
		for(int i = 0; i <= a * 1000 * 1000; ++i){
			double x = i * 1. / (1000 * 1000);
			double c1 = (h - x) * 1./ (a + x);
			double c = (w - x) * 1./ (a + x);
			if(h < x || w < x) continue;
			int cnt1 = round((h - x) * 1./ (a + x));
			int cnt = round((w - x) * 1./ (a + x));
			if(h < a * cnt1 || w < a * cnt){
				continue;
			}
			if(!cnt || !cnt1) continue;
			double x1 = (h - a * cnt1) * 1. / (cnt1 + 1);
			x = (w - a * cnt) * 1. / (cnt + 1);
			if(fabs(x - x1) <= eps){
				ans = min(ans, x);
			}
		}
		if(fabs(ans - inf) <= eps){
			ans = -1;
		}
		printf("%0.6f", ans);
		return 0;
	}
	for(int cnt = w / a; cnt >= 1; --cnt){
		double x = (w - a * cnt) * 1. / (cnt + 1);
		ll cnt1 = round((h - x) * 1./ (a + x));
		double x1 = (h - a * cnt1) * 1. / (cnt1 + 1);
		if(fabs(x - x1) <= double(1e-6)){
			ans = min(ans, x);
		}
	}
	if(fabs(ans - inf) <= eps){
		ans = -1;
	}
	printf("%0.6f", ans);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
#define y1 sda
using namespace std;
const int N = int(3e5), mod = int(1e9)  + 7;

int a,h,w;

ll phi(int n){
	ll m = n;
	for(int i = 2;1ll * i * i <= n; i++){
		if(n%i == 0){
			while(n%i == 0){
				n /= i;
			}
			m -= m/i;
		}
	}
	if (n > 1) m -= m/n;
	return m;
}

ll modpow(ll a,ll b,ll c){
	ll res = 1;
	while(b){
		if(b & 1) res = res * a % c;
		a = a * a % c;
		b >>= 1;
	}
	return res % c;
}

int main () {
	cin >> a >> h >> w;
	int mx = int(1e9);
	int res = h - w;
	int A = w + a;
	res = (res + 1ll * A * mx) % A;
	int B = (a + h) % A;
	mx = w / a;
	int ans = -1;
	for(int i = 1;i <= mx;i++){
		res += B;
		if(res >= A) {
			res -= A;
		}
		if(res == 0) ans = i;
	}
	if(ans == -1) puts("-1");
	else
	printf("%.12lf", (w - ans * a + 0.0) / (ans + 1));

return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const double eps = (double)(1e-18);
const double inf = (double)(1e9);

ll a, h, w;

int main(){
	cin >> a >> w >> h;
	if(w % a == 0 && h % a == 0){
		cout << 0.0;
		return 0;
	}
	double ans = inf;
	for(int cnt = w / a; w <= 2 * a * cnt + a; --cnt){
		double x = (w - a * cnt) * 1. / (cnt + 1);
		int cnt1 = round((h - x) * 1./ (a + x));
		double x1 = (h - a * cnt1) * 1. / (cnt1 + 1);
		if(fabs(x - x1) <= eps){
			ans = min(ans, x);
			printf("%0.9f", x);
			return 0;
		}
	}
	if(fabs(ans - inf) <= eps){
		ans = -1;
	}
	printf("%0.9f", ans);
}


#include <bits/stdc++.h>
using namespace std;
int dig(int n)
{
    int sum = 0;
    while(n)
    {
        sum++;
        n/=10;
    }
    return sum;
}
int main()
{
    int T;
    cin>>T;
    int ar[6];
    int p;
    for(int i=0;i<6;i++)
    {
        cin>>p;
        ar[i] = p;
    }
    int q;
    cin>>q;
    int n;
    cin>>n;
    int dp[T];
    int sum=0;
    sum+=9;
    if(T>9)
    dp[0] = 9;
    for(int i=1;i<T;i++)
    {
        if(sum+((10*i)+9)<=T)
        {
        dp[i] = (10*i)+9;
        sum+=dp[i];
        }
        else if(sum+((10*i)+6)<=T)
        {
        dp[i] = (10*i)+6;
        sum+=dp[i];
        }
        else if(sum+((10*i)+3)<=T)
        {
        dp[i] = (10*i)+3;
        sum+=dp[i];
        }
        else if(sum>T)
        {
            int dif = sum-T;
            for(int j=1;j>=0;j--)
            if(dif<=dp[j])
            {
                dp[j]-=diff;
                sum-=diff;
                break;
            }
        }
    }
    int fortune = 0;
    for(int i=0;i<T;i++)
    {
        int d = dig(dp[i]);
        if((dp[i]%10)%3==0)
        {
            fortune+=((dp[i]%10)/3)*(dig(dp[i])-1);
        }
    }
    cout<<fortune<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 1000000;

int k,m,num,cntf,cntg;
int di[maxn];
ll n,F[10],f[21][maxn],g[7][maxn],v[1000],w[1000],vg[1000],wg[1000];

int qsm(int i,int po){
	int res = 1;
	while(po){
		if(po&1) res *= i;
		po >>= 1;
		i *= i;
	}
	return res;
}

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	num = 0;
	k = read();
	int K = k-1;
	int t = 0;
	while(K - (1<<t) > 0){ // ?????
		di[++num] = 1<<t;
		K -= (1<<t);
		++t;
	}
	di[++num] = K;
//	for(int i=1;i<=num;++i) printf("%d ",di[i]); printf("\n");

	for(int i=0;i<6;++i) F[i] = read();
	m = read();

	n = read();

	memset(g,-1,sizeof(g));
	g[0][0] = 0;
	for(int d=1;d<=6;++d){ //  dp
		cntf = 0; cntg = 0;
		v[++cntf] = 0, w[cntf] = 0;
		for


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 1000000;

int k,m,num,cntf,cntg;
int di[maxn];
ll n,F[10],f[21][maxn],g[7][maxn],v[1000],w[1000],vg[1000],wg[1000];

int qsm(int i,int po){
	int res = 1;
	while(po){
		if(po&1) res *= i;
		po >>= 1;
		i *= i;
	}
	return res;
}

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	num = 0;
	k = read();
	int K = k-1;
	int t = 0;
	while(K - (1<<t) > 0){ // ?????
		di[++num] = 1<<t;
		K -= (1<<t);
		++t;
	}
	di[++num] = K;
//	for(int i=1;i<=num;++i) printf("%d ",di[i]); printf("\n");

	for(register int i=0;i<6;++i) F[i] = read();
	m = read();

	n = read();

	memset(g,-1,sizeof(g));
	g[0][0] = 0;
	for(register int d=1;d<=6;++d){ //  dp
		cntf = 0; cntg = 0;
		v[++cntf] = 0,