#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define sz size()
#define INF (int)1e9

typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector< vector <int> > VVI;
typedef pair<int,int> pii;

int n;
int deg[201],temp[201];
bool graph[201][201];

int mach[201];
int main() {
   int n;
   cin>>n;
   REP(i,n) {
       cin>>mach[i];
       mach[i]--;
   }
   REP(i,n) {
      int x;
      cin>>x;
      REP(j,x) {
         int y; cin>>y;
         y--;
         graph[i][y]=1;
         deg[i]++;
      }
   }
   memcpy(temp,deg,sizeof(deg));
   int ans=INF;
   for(int z=2; z>=0; z--) {
       int k=z;
       int num_v = n;
       int cnt=0;
       while(true)  {

                REP(i,n) {
                    if(mach[i]==k && deg[i]==0){
                         deg[i]=-1;
                         REP(j,n) {
                               if(graph[j][i]) deg[j]--;
                         }
                         num_v--;
                         i=-1;
                    }
                }
                if(num_v > 0) {
                   k = (k+1)%3;
                   cnt++;
                }
                else break;

       }
       ans = min(ans,cnt+n);
       memcpy(deg,temp,sizeof(deg));
   }

   cout<<ans<<endl;
   while(1);

}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

#define pb push_back

const int MAX_N = 200 * 1000 +20;
vector <int> adj[MAX_N];
vector <int> a[MAX_N];
vector <int> T_S;
bool node[MAX_N];
bool mark[MAX_N];
int P[MAX_N];
int n , ans , Size;

void Topology_Sort ( int v ){
	node[v] = 1;
	for ( int i = 0 ; i < adj[v].size() ; i++ ){
		int u = adj[v][i];
		if ( !node[u] )
			Topology_Sort(u);
	}
	T_S.pb(v);
}

void Computer ( int place ){
	int sum = 0;
	for ( int i = Size-1 ; i >= 0 ; i-- ){
		int v = T_S[i];
		if ( !mark[v] && P[v] == place ){
			bool flag = 1;
			for ( int j = 0 ; j < a[v].size() ; j++ ){
				int u = a[v][j];
				if ( !mark[u] )
					flag = 0;
			}
			if ( flag ) {
				sum++;
				mark[v] = 1;
			}

		} else if ( mark[v] )
			sum++;
	}
	if ( sum != n ){
		ans++;
		place++;
		place %= 3;
		if ( !place )
			place = 3;
		return Computer( place );
	}
}

int main()
{
	cin >> n;
	for ( int i = 0 ;i < n ; i++ )
		cin >> P[i];
	for ( int v = 0 ; v < n ; v++ ){
		int deg;
		cin >> deg;
		while ( deg > 0 ){
			int u ;
			cin >> u;
			--u;
			adj[u].pb(v);
			a[v].pb(u);
			deg--;
		}
	}
	for ( int i = 0 ; i < n ; i++ )
		if ( !node[i] )
			Topology_Sort(i);
	Size = T_S.size();
	ans = 0;
	int answer = 0;
	Computer(1);
	answer = ans;
	///////////////
	ans = 0;
	memset( mark , 0 , sizeof mark);
	Computer(2);
	answer = min ( ans , answer );
	////////////////
	ans = 0;
	memset( mark , 0 , sizeof mark);
	Computer(3);
	answer = min ( ans , answer );
	cout << answer+n << endl;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

#define MOD (1000000000+7)
#define INF 1000000000
typedef unsigned long long  ull;
typedef pair<ll,ll> pii;

#define ll long long

ll n;
ll comp[201];

struct graph
{
	vector <ll> mat[201];
	vector <ll> ind;
	graph()
	{
		ind.resize(201);
		for(ll i = 0; i < 201; i ++)
			mat[i].resize(201),
			ind[i] = 0;
	}
	bool operator==(const graph &two)const
	{
		for(ll i = 1; i <= n; i ++)
			if(ind[i] != two.ind[i])
				return false;
		return true;
	}
	bool operator<(const graph &two)const
	{
		return ind < two.ind;
	}
};

#define pig pair<ll,graph>

map <pig, ll> dp;

ll getDis(ll u, ll v)
{
	ll res = 0;
	while(u != v)
	{
		u ++;
		if(u == 4)
			u = 1;
		res ++;
	}
	return res;
}
ll f(ll u, graph g)
{
	if( dp.count( pig(u,g) ) )
		return dp[pig(u,g)];

	ll &ref = dp[pig(u,g)] = INF;

	// cout << "HERE ";
	// for(ll i = 1; i <= n; i ++)
	// {
		// cout << g.ind[i] << " ";
	// }
	// cout << endl;

	for(ll i = 1; i <= n; i ++)
	{
		if(g.ind[i] == 0)
		{
			g.ind[i] = -1;

			for(ll j = 1; j <= n; j ++)
				if(g.mat[i][j] == 1)
					g.ind[j] --;

			ll move = getDis(u, comp[i]);
			ref = min( ref, move+f(comp[i],g) );
			// cout << "HERdddddddE " << ref << endl;

			for(ll j = 1; j <= n; j ++)
				if(g.mat[i][j] == 1)
					g.ind[j] ++;

			g.ind[i] = 0;
		}
	}
	if(ref == INF)
		return 0;
	return ref;
}

int main()
{
	cin >> n;
	for(ll i = 1; i <= n; i ++)
		cin >> comp[i];

	graph g;
	for(ll i = 1; i <= n; i ++)
	{
		ll k, prev;
		cin >> k;
		for(ll j = 0; j < k; j ++)
		{
			cin >> prev;
			g.mat[prev][i] = 1;
			g.ind[i] ++;
		}
	}

	// for(ll i = 1; i <= n; i ++)
	// {
		// for(ll j = 1; j <= n; j ++)
			// cout << g.mat[i][j] << " ";
		// cout << endl;
	// }

	// cout << f(1, g) << endl;
	// cout << f(2, g) << endl;
	// cout << f(3, g) << endl;
	// cout << "===" << endl;
	ll mn = min( f(1, g), f(2, g) );
	mn = min( mn, f(3, g) );

	cout << mn+n << endl;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

//FILE *INFILE,*OUTFILE;

bool e[11][11];
int a[11];
int n,m,k;
int ans;
bool vis[11];

bool check()
{
	int indgr[11];
	int st[11];
	int top=0;
	for (int i=1;i<=n;i++)
		indgr[i]=1;
	for (int i=1;i<=n-2;i++)
		indgr[a[i]]++;
	for (int i=1;i<=n;i++)
		if (indgr[i]==1)
			st[top++]=i;
	for (int i=1;i<=n-2;i++)
	{
		int to=a[i],m=n+1,p;
		for (int j=0;j<top;j++)
			if (st[j]<m)
				m=st[j],p=j;
		if (!e[m][to])
			return false;
		swap(st[top-1],st[p]);
		top--;
		indgr[to]--;
		if (indgr[to]==1)
			st[top++]=to;
	}
	return e[st[top-1]][st[top-2]];
}
int cnt=0;
void dfs(int i)
{
	if (cnt>n-k)
		return;
	if (i==n-1)
	{
		if (n-cnt==k && check())
			ans++;
		return;
	}

	for (int t=1;t<=n;t++)
	{
		a[i]=t;
		bool flag=false;
		if (!vis[t])
		{
			flag=true;
			vis[t]=true;
			cnt++;
		}
		dfs(i + 1);
		a[i]=0;
		if (flag)
		{
			vis[t]=false;
			cnt--;
		}
	}
}

int main()
{
//	INFILE=fopen("tactic.in","r");
//	OUTFILE=fopen("tactic.out","w");
	fscanf(INFILE,"%d%d%d",&n,&m,&k);
	for (int i=1;i<=m;i++)
	{
		int v1,v2;
		fscanf(INFILE,"%d%d",&v1,&v2);
		e[v1][v2]=true;
		e[v2][v1]=true;
	}
	dfs(1);
	fprintf(OUTFILE,"%d\n",ans);
//	fclose(INFILE);
//	fclose(OUTFILE);
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (n); i++)

const int N = 10;

int n, m, k, g[N], f[N + 1][1 << N][1 << N];

int main() {
	cin >> n >> m >> k;
	while (m--) {
		int a, b;
		cin >> a >> b, a--, b--;
		g[a] |= 1 << b;
		g[b] |= 1 << a;
	}
	#define SUB(A, S) for (int U = S, A = U; A > 0; A--, A &= S)
	int all = (1 << n) - 1;
	SUB(B, g[n - 1])
		f[bn(B) == 1][1 << (n - 1)][B] = 1;
	for (int A = 0; A < all; A++)
		for (int add = max(0, k - bn(all ^ A)); add <= bn(A); add++)
			SUB(B, all ^ A) {
				const int F = f[add][A][B];
				if (!F || add + bn(B) > k) continue;
				int v = __builtin_ctz(B), A1 = A ^ (1 << v), B1 = B ^ (1 << v);
				f[add + 1][A1][B1] += F;
				SUB(C, (all ^ A ^ B) & g[v])
					f[add][A1][B1 | C] += F;
			}
	cout << f[k][all][0] << endl;
}


/*///in the name of god
///12/11/94
#include <iostream>
#include <algorithm>

using namespace std;
int m
int n,m,
vector<pair <int,pair<int,int> > >E,H;
int par[maxn],dad[maxn][log],
int get_par(int u){
    if(par[u] == u) return u;
    else return get_par(par[u]);
}
void kruskal(){
    sort(G.begin() , G.end());
    for(int i = 0 ; i < m ; i++){
            int u = G[i].S.S , v = G[i].S.F , w = G[i].F;
            if(get_par(u) != get_par(v)){
                par[u] = v;
                mst += w;
                adj[u].mp(v , w);
                adj[v].mp(u , w);
            }
    }
}
int lca(int u , int v){
    if(h[u] < h[v]) swap(u ,v);
    v = get_dad(v  , h[u] - h[v]);
    if(u == v) return v;
    for(int i = log - 1 ; i >= 0  ; i--){
            if(u != v)
                u = dad[u][i] , v = dad[u][i];
    }
    return par[v][0];
}
int get_parent(int v, int a){
	for (int i = 0; i <log ; i++)
		if ((a >> i) & 1)
			v = dad[v][i];
	return v;
}
int get_max(int v, int a){
	int res = 0;
	for (int i=0; i<log ; i++)
		if ((a >> i) & 1)
			res = max(res, mx[v][i]), v = dad[v][i];
	return res;
}
void solve(int u,int v,int w){
    cp = lca(v , u);
    return  mst - (max(get_max(u , cp) , get_max(v , cp))) + w;
}
void dfs(int u , int p){
    for(int i = 0 ; i<adj[u].size() ; i++){
        int v = adj[u].F , w = adj[u].S;
        if(p == v) continue;
        dad[v][0] = u;
        maxi[v][0] = w;
        for(int i = 0 ; i < log ; i++){
            dad[v][i] = dad[[v][i - 1]][i - 1];
            mx[v][i] = max(mx[v][i] , mx[dad[v][i - 1]][i - 1]);
        }
        h[u] = h[v] + 1;
        dfs(v , u);
    }
}
int main(){
    cin >> n >> m;
    for(int i = 0 ; i< m ; i++){
        cin >> u >> v >> w; u--,v--;
        G.pb(mp(w , mp(u , v)));
        E.pb(mp(w , mp(u , v)));
    }
    for(int i = 0 ; i < n ; i++) par[i] = i;
    kruskal();
    dfs(0 , -1);
    for(int i = 0 ;  i < m ; i++)
        cout << solve(E[i].S.S , E[i].S.S) << endl;
}
*//*
#include <iostream>
#include <vector>
#define pb push_back

using namespace std;
const int maxn = 1000 * 100 + 7, lg = 29 ;
int n,q,par[maxn][lg],child[maxn],d[maxn];
vector <int> adj[maxn];
void dfs(int u,int p){
    for(int i = 0 ; i < adj[u].size() ; i++){
        int v = adj[u][i];
        if(p == v) continue;
        par[v][0] = u;
        for(int j = 1 ; j < lg ; j++){
            par[v][j] = par[par[v][j - 1]][j - 1];
        }
        d[v] = d[u] + 1;
        dfs(v , u);
        child[u] = child[u] + child[v] + 1;
    }
}
int dad(int u ,int k){
    for(int i = lg - 1 ; i >= 0 ; i--)
            if((k & (1 << i)) == (1 << i)){
                u = par[u][i];
            }
    return u;
}
int LCA(int a,int b){
    if(d[a] < d[b]) swap(a , b);
    a = dad(a , d[a]- d[b]);
    if(a == b) return b;
        for(int i = lg - 1  ; i>=0 ; i--)
            if(par[a][i] != par[b][i]){
                a = par[a][i];
                b = par[b][i];

            }
    return par[a][0];
}
int main(){
    cin >> n;
    for(int i = 1 ; i < n ; i++){
        int u,v;
        cin >> u >> v; u--,v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(0 , -1);
    cin >> q;
    while(q--){
        int a,b;
        cin >> a >> b; a--,b--;
        int lca = LCA(a , b);
        int D1 = d[a] - d[lca] , D2 =  d[b] - d[lca];
        int dis = D1 + D2 ;
        if(a == b)
                cout << n << endl;
        else if(dis % 2)
                cout << 0 << endl;
        else if(D1 == D2){
                int pa =  dad(a , D1 -  1) , pbb = dad(b ,D2 - 1);
                cout   << n - (child[pa] + child[pbb] + 2) << endl;
        }
        else{
                if(D1 < D2) swap(a , b);
                int pa = dad(a , (dis/2) - 1) , paa = par[pa][0];
                cout  << child[paa] - child[pa] << endl;
        }
    }
    return 0;
}*/

/*

4
1 2
2 3
2 4
2
1 2
1 3

15
1 2
1 3
1 4
2 5
2 6
2 7
5 8
6 9
9 14
14 15
7 10
4 13
3 11
3 12
6
10 15
13 12
2 15
8 4
15 12
6 13

*/
/// in the name of god
///lovely problem from lovely person
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long ll;
const int maxn = 10 ;
ll dp[1 << maxn][1 << maxn] ,ans = 0;
int n,m,k , cnt[1 << maxn];
bool mat[maxn][maxn];
void makecnt(){
    for(int i = 0 ; i < (1 << 11) ; i++){
        if(i % 2) cnt[i] = cnt[i / 2] + 1;
        else cnt[i] = cnt[i / 2];
    }
}
inline ll DP(int msk,int le){
	ll res = dp[msk][le];
	if (res != -1)
		return res;
	if (msk == le && cnt[msk] > 2)
		return res = 0;
	if (cnt[msk] > 1 && cnt[le] < 2)
		return res = 0;
	if (cnt[msk] == 1)
		return res = !le;
	if (cnt[msk] == 2){
		int u =  __builtin_ctz(le & -le);
		le ^= (1<<u);
		return res = mat[u][ __builtin_ctz(le)];
	}
	if ((msk & le) != le)
		return res = 0;

	res = 0;
	int l =  __builtin_ctz(le & -le);
	for (int par = 0; par < n; par++){
		if ((msk>>par&1) == 0) continue;
		if (le>>par&1) continue;
		if (!mat[par][l]) continue;
		res += DP(msk ^ (1<<l), le ^ (1<<l)) + DP(msk ^ (1<<l), le ^ (1<<l) ^ (1<<par));
	}
	return res;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0 ; i < m ; i++){
        int u,v;
        scanf("%d%d",&u,&v); u--,v--;
        mat[u][v] = mat[v][u] = 1;
    }
    makecnt();
    memset(dp , -1 , sizeof dp);
    for(int i = 0 ; i <(1 << n) ; i++)
            if(cnt[i] == k) ans += DP(((1 << n) - 1) , i);
    cout << ans << endl;
}
/*///in the name of god
///12/11/94
#include <iostream>
#include <algorithm>

using namespace std;
int m
int n,m,
vector<pair <int,pair<int,int> > >E,H;
int par[maxn],dad[maxn][log],
int get_par(int u){
    if(par[u] == u) return u;
    else return get_par(par[u]);
}
void kruskal(){
    sort(G.begin() , G.end());
    for(int i = 0 ; i < m ; i++){
            int u = G[i].S.S , v = G[i].S.F , w = G[i].F;
            if(get_par(u) != get_par(v)){
                par[u] = v;
                mst += w;
                adj[u].mp(v , w);
                adj[v].mp(u , w);
            }
    }
}
int lca(int u , int v){
    if(h[u] < h[v]) swap(u ,v);
    v = get_dad(v  , h[u] - h[v]);
    if(u == v) return v;
    for(int i = log - 1 ; i >= 0  ; i--){
            if(u != v)
                u = dad[u][i] , v = dad[u][i];
    }
    return par[v][0];
}
int get_parent(int v, int a){
	for (int i = 0; i <log ; i++)
		if ((a >> i) & 1)
			v = dad[v][i];
	return v;
}
int get_max(int v, int a){
	int res = 0;
	for (int i=0; i<log ; i++)
		if ((a >> i) & 1)
			res = max(res, mx[v][i]), v = dad[v][i];
	return res;
}
void solve(int u,int v,int w){
    cp = lca(v , u);
    return  mst - (max(get_max(u , cp) , get_max(v , cp))) + w;
}
void dfs(int u , int p){
    for(int i = 0 ; i<adj[u].size() ; i++){
        int v = adj[u].F , w = adj[u].S;
        if(p == v) continue;
        dad[v][0] = u;
        maxi[v][0] = w;
        for(int i = 0 ; i < log ; i++){
            dad[v][i] = dad[[v][i - 1]][i - 1];
            mx[v][i] = max(mx[v][i] , mx[dad[v][i - 1]][i - 1]);
        }
        h[u] = h[v] + 1;
        dfs(v , u);
    }
}
int main(){
    cin >> n >> m;
    for(int i = 0 ; i< m ; i++){
        cin >> u >> v >> w; u--,v--;
        G.pb(mp(w , mp(u , v)));
        E.pb(mp(w , mp(u , v)));
    }
    for(int i = 0 ; i < n ; i++) par[i] = i;
    kruskal();
    dfs(0 , -1);
    for(int i = 0 ;  i < m ; i++)
        cout << solve(E[i].S.S , E[i].S.S) << endl;
}
*//*
#include <iostream>
#include <vector>
#define pb push_back

using namespace std;
const int maxn = 1000 * 100 + 7, lg = 29 ;
int n,q,par[maxn][lg],child[maxn],d[maxn];
vector <int> adj[maxn];
void dfs(int u,int p){
    for(int i = 0 ; i < adj[u].size() ; i++){
        int v = adj[u][i];
        if(p == v) continue;
        par[v][0] = u;
        for(int j = 1 ; j < lg ; j++){
            par[v][j] = par[par[v][j - 1]][j - 1];
        }
        d[v] = d[u] + 1;
        dfs(v , u);
        child[u] = child[u] + child[v] + 1;
    }
}
int dad(int u ,int k){
    for(int i = lg - 1 ; i >= 0 ; i--)
            if((k & (1 << i)) == (1 << i)){
                u = par[u][i];
            }
    return u;
}
int LCA(int a,int b){
    if(d[a] < d[b]) swap(a , b);
    a = dad(a , d[a]- d[b]);
    if(a == b) return b;
        for(int i = lg - 1  ; i>=0 ; i--)
            if(par[a][i] != par[b][i]){
                a = par[a][i];
                b = par[b][i];

            }
    return par[a][0];
}
int main(){
    cin >> n;
    for(int i = 1 ; i < n ; i++){
        int u,v;
        cin >> u >> v; u--,v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(0 , -1);
    cin >> q;
    while(q--){
        int a,b;
        cin >> a >> b; a--,b--;
        int lca = LCA(a , b);
        int D1 = d[a] - d[lca] , D2 =  d[b] - d[lca];
        int dis = D1 + D2 ;
        if(a == b)
                cout << n << endl;
        else if(dis % 2)
                cout << 0 << endl;
        else if(D1 == D2){
                int pa =  dad(a , D1 -  1) , pbb = dad(b ,D2 - 1);
                cout   << n - (child[pa] + child[pbb] + 2) << endl;
        }
        else{
                if(D1 < D2) swap(a , b);
                int pa = dad(a , (dis/2) - 1) , paa = par[pa][0];
                cout  << child[paa] - child[pa] << endl;
        }
    }
    return 0;
}*/

/*

4
1 2
2 3
2 4
2
1 2
1 3

15
1 2
1 3
1 4
2 5
2 6
2 7
5 8
6 9
9 14
14 15
7 10
4 13
3 11
3 12
6
10 15
13 12
2 15
8 4
15 12
6 13

*/
/// in the name of god
///lovely problem from lovely person
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long ll;
const int maxn = 10 ;
ll dp[1 << maxn][1 << maxn] ,ans = 0;
int n,m,k , cnt[1 << maxn];
bool mat[maxn][maxn];
void makecnt(){
    for(int i = 0 ; i < (1 << 11) ; i++){
        if(i % 2) cnt[i] = cnt[i / 2] + 1;
        else cnt[i] = cnt[i / 2];
    }
}
inline ll DP(int msk,int le){
	ll res = dp[msk][le];
	if (res != -1)
		return res;
	if (msk == le && cnt[msk] > 2)
		return res = 0;
	if (cnt[msk] > 1 && cnt[le] < 2)
		return res = 0;
	if (cnt[msk] == 1)
		return res = !le;
	if (cnt[msk] == 2){
		int u =  __builtin_ctz(le & -le);
		le ^= (1<<u);
		return res = mat[u][ __builtin_ctz(le)];
	}
	if ((msk & le) != le)
		return res = 0;

	res = 0;
	int l =  __builtin_ctz(le & -le);
	for (int par = 0; par < n; par++){
		if ((msk>>par&1) == 0) continue;
		if (le>>par&1) continue;
		if (!mat[par][l]) continue;
		res += DP(msk ^ (1<<l), le ^ (1<<l)) + DP(msk ^ (1<<l), le ^ (1<<l) ^ (1<<par));
	}
	return res;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0 ; i < m ; i++){
        int u,v;
        scanf("%d%d",&u,&v); u--,v--;
        mat[u][v] = mat[v][u] = 1;
    }
    makecnt();
    memset(dp , -1 , sizeof dp);
    for(int i = 0 ; i <(1 << n) ; i++)
            if(cnt[i] == k) ans += DP(((1 << n) - 1) , i);
    cout << ans << endl;
}


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

LL n, m;
char s[2000][2000];
char ss[2000][2000];
int main()
{

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> s[i][j];
            ss[i][j] = '.';
        }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (i + 2 >= n || j + 2 >= m)
            {
                break;
            }
            int f = 0;
            for (int u = 0; u =< 2;u++)
            {
                for (int v = 0; v <= 2;v++)
                {
                    if(u==1&&v==1)
                        continue;
                    if(s[i+u][j+v]=='.')
                        f = 1;
                }
            }
            if(f!=1)
            {
                for (int u = 0; u <= 2;u++)
            {
                for (int v = 0; v <= 2;v++)
                {
                    if(u==1&&v==1)
                        continue;
                    ss[i + u][j + v] = '#';
                }
            }
            }
        }
    }
    int flag = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (s[i][j] != ss[i][j])
            {
                flag = 1;
                break;
            }
        }
    }
    if (flag)
    {
        cout << "NO" << endl;
    }
    else
        cout << "YES" << endl;
    // system("pause");
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	vector<vector<bool>>sig(n,vector<int>(m)), temp;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			char c;
			cin>>c;
			if(c=='\n')j--;
			else if(c=='#') sig[i][j]=true;
			else sig[i][j]=false;
		}
	}
	temp=sig;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			//must
			if(sig[i][j]){
				if(i+2>=m || j+2>=n){
					cout<<"NO";
					return 0;
				}
				for(int ii=0;ii<3;ii++){
					for(int jj=0;jj<3;jj++){
						if(ii==1 && jj==1) continue;
						if(temp[i+ii][j+jj]==false){
							cout<<"NO";
							return 0;
						}
						sig[i+ii][j+jj]=false;
					}
				}
			}
			//
			else if(temp[i][j]){
				if(i+2>=m || j+2>=n) continue;
				else{
					bool p=true;
					for(int ii=0;ii<3;ii++){
						for(int jj=0;jj<3;jj++){
							if(ii==1 && jj==1) continue;
							if(!temp[i+ii][j+jj]) p=false;
						}
					}
					if(p){
						for(int ii=0;ii<3;ii++){
							for(int jj=0;jj<3;jj++){
								if(ii==1 && jj==1) continue;
								sig[i+ii]+[j+jj]=false;
							}
						}
					}
				}
			}
		}
	}
	cout<<"YES";
	return 0;
}


#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double ld;
typedef long long ll;
#define long double double
// Симплекс-метод

/*

Решает линейную программу:
a11 * x1 + a12 * x2 + ... + a1n * xn + b1 >= 0
a21 * x1 + a22 * x2 + ... + a2n * xn + b2 >= 0
...   ..   ...   ..   ...   ...   ..   ..    .
am1 * x1 + am2 * x2 + ... + amn * xn + bm >= 0
 c1 * x1 +  c2 * c2 + ... +  cn * xn      -> max

Также должно выполняться x1 >= 0, ..., xn >= 0.

Входные данные - матрица (m+1)*(n+1):
a11 a12 ... a1n b1
a21 a22 ... a2n b2
... ... ... ... ..
am1 am2 ... amn bm
 c1  c2 ...  cn  0

Выходные данные:
first - тип решения (его нет, либо оно найдено, либо оно бесконечно велико)
second - массив переменных (если решение конечно) в формате:

x1 x2 ... xn res

*/

const ld eps = 1e-6;
inline void vectorDel(vector<int> &v, int value) {
    int idx = find(begin(v), end(v), value) - begin(v);
    swap(v[idx], v.back());
    v.pop_back();
}

enum class SolutionType {
    BOUNDED,
    UNBOUNDED,
    NO_SOLUTION
};

class SimplexMethod {
private:
    int h, w, n;
    vector< vector<long double> > a;
    vector<char> isBasic;
    vector<int> basic, nonBasic;

    inline void replace(int x, int y) {
        long double k = -a[x][y];
        a[x][y] = 0;
        for (int j = 0; j <= n; j++) {
            if (!isBasic[j]) {
                a[y][j] = a[x][j] / k;
                a[x][j] = 0;
            }
        }
        a[y][x] = -1 / k;
        isBasic[x] ^= 1;
        isBasic[y] ^= 1;
        vectorDel(basic, x);
        vectorDel(nonBasic, y);
        basic.push_back(y);
        nonBasic.push_back(x);
        basic.push_back(n);
        nonBasic.push_back(n);
        for (int i : basic) {
            for (int j : nonBasic) {
                a[i][j] += a[i][y] * a[y][j];
            }
            a[i][y] = 0;
        }
        basic.pop_back();
        nonBasic.pop_back();
    }

    inline int findPositive() {
        int best = 0;
        for (int j = 1; j < n; j++) {
            if (!isBasic[j] && a[n][j] > a[n][best] + eps) {
                best = j;
            }
        }
        return (a[n][best] > eps) ? best : -1;
    }

    inline int findBasic(int x) {
        long double add = 0;
        int best = -1;
        for (int i = 0; i < n; i++) {
            if (!isBasic[i] || a[i][x] > -eps) {
                continue;
            }
            long double grow = -a[i][n] / a[i][x];
            if (best < 0 || grow + eps < add) {
                add = grow;
                best = i;
            }
        }
        return best;
    }

    inline bool findAnyPoint() {
        int minB = -1;
        for (int i = 0; i < n; i++) {
            if (minB < 0 || a[i][n] < a[minB][n]) {
                minB = i;
            }
        }
        if (minB < 0 || a[minB][n] > -eps) {
            return true;
        }
        auto lastRow = a.back();
        isBasic.insert(begin(isBasic) + n, false);
        nonBasic.push_back(n);
        auto newA = a;
        a.pop_back();
        a.emplace_back(n+2, 0);
        a.emplace_back(n+2, 0);
        a.back()[n] = -1;
        for (int i = 0; i < n; i++) {
            a[i].insert(begin(a[i]) + n, 1, isBasic[i] ? 1 : 0);
        }
        n++;
        replace(minB, n-1);
        doSolve(false);
        if (a[n][n] < -eps) {
            return false;
        }
        if (isBasic[n-1]) {
            for (int i : nonBasic) {
                if (abs(a[n-1][i]) > eps) {
                    replace(n-1, i);
                    break;
                }
            }
        }
        n--;
        for (int i = 0; i < n; i++) {
            a[i].erase(begin(a[i]) + n, begin(a[i]) + n + 1);
        }
        isBasic.erase(begin(isBasic) + n, begin(isBasic) + n + 1);
        a.pop_back();
        a.pop_back();
        a.push_back(lastRow);
        for (int i : basic) {
            for (int j : nonBasic) {
                a[n][j] += a[n][i] * a[i][j];
            }
            a[n][i] = 0;
        }
        vectorDel(nonBasic, n);
        return true;
    }

    inline SolutionType doSolve(bool needFindInitial = true) {
        if (needFindInitial && !findAnyPoint()) {
            return SolutionType::NO_SOLUTION;
        }
        for (;;) {
            int x = findPositive();
            if (x < 0) {
                return SolutionType::BOUNDED;
            }
            int y = findBasic(x);
            if (y < 0) {
                return SolutionType::UNBOUNDED;
            }
            replace(y, x);
        }
    }
public:
    pair<SolutionType, vector< double> > solve() {
        SolutionType type = doSolve();
        if (type != SolutionType::BOUNDED) {
            return {type, {}};
        }
        vector<long double> v(w+1);
        for (int i = 0; i < w; i++) {
            v[i] = a[i][n];
        }
        v[w] = a[n][n];
        return {type, v};
    }

    SimplexMethod(const vector< vector< double> > &v)
        : h((int)v.size() - 1), w((int)v[0].size() - 1), n(h + w),
          a(n+1, vector<long double>(n+1)), isBasic(n+1, true) {
        for (int i = 0; i < w; i++) {
            isBasic[i] = false;
        }
        isBasic[n] = false;
        for (int i = 0; i <= h; i++) {
            for (int j = 0; j <= w; j++) {
                a[i + w][(j == w) ? n : j] = v[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            (isBasic[i] ? basic : nonBasic).push_back(i);
        }
    }
};

int n, m;
const int maxN = 55;
int dist[maxN][maxN];
int id[maxN][maxN];
int sz = 1;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("input.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        dist[u][v] = w;
        id[u][v] = sz;
        sz++;
    }
    int q;
    cin >> q;
    while (q--) {
        int t;
        cin >> t;
        vector<vector<ld> > all;
        for (int vert = 1; vert <= n; vert++) {
            if (vert == 1) {
                vector < ld > f(n + m + 1);
                f[0] = -1;
                all.push_back(f);
                continue;
            }
            for (int p = 1; p <= n; p++) {
                if (dist[p][vert]) {
                    vector < ld > f(n + m + 1);
                    f[p - 1] = 1;
                    f[vert - 1] = -1;
                    f[n + m] = dist[p][vert];
                    f[id[p][vert] + n - 1] = 1;
                    all.emplace_back(f);
                }
            }
        }
        vector < ld > f(n + m + 1);
        for (int i = n; i <= n + m - 1; i++) f[i] = -1;
        f[n + m] = t;
        all.emplace_back(f);
        vector < ld > g(n + m + 1);
        g[n - 1] = 1;
        all.emplace_back(g);
        auto res = SimplexMethod(all).solve();
        cout << fixed << setprecision(10) << res.second.back() << '\n';
    }
    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
inline int topbit(int x) {return x == 0 ? -1 : 31-__builtin_clz(x);}
inline int topbit(long long x) {return x == 0 ? -1 : 63-__builtin_clzll(x);}
inline int botbit(int x) {return x == 0 ? 32 : __builtin_ctz(x);}
inline int botbit(long long x) {return x == 0 ? 64 : __builtin_ctzll(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
inline int kthbit(long long x, int k) {return (x>>k)&1;}
inline void print() {cout << "\n";}
template<class T>
inline void print(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cout << *itr << " ";
    print();
}
template<class T, class... Args>
inline void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() {cerr << endl;}
template<class T>
inline void debug(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cerr << *itr << " ";
    debug();
}
template<class T, class... Args>
inline void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template<class T> inline void debug(const vector<T> &v) {}
template<class T, class... Args> inline void debug(const T &x, const Args &... args) {}
#endif
struct Fast_ios {Fast_ios() {cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20);};} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int MAX = 100000;
using DD = double;
int main() {
    int N,M; cin >> N >> M;
    mcf_graph<ll, ll> g(N);
    rep(i,M) {
        int u,v,w; cin >> u >> v >> w;
        u--; v--;
        g.add_edge(u,v,1,w);
    }

    auto res = g.slope(0,N-1);
    int K = res.size();
    vector<ll> path(K-1),cnt(K-1);
    rep(i,K-1) {
        cnt[i] = res[i+1].first-res[i].first;
        path[i] = (res[i+1].second-res[i].second)/cnt[i];
    }
    vector<DD> ans(MAX+1);
    ans[0] = path[0];
    ll c = 0;
    ll S = 0;
    int cur = 1;
    rep(i,K-1) {
        c += cnt[i];
        while (cur <= MAX) {
            ans[cur] = ans[cur-1]+DD(1)/c;
            S++;
            cur++;
            if (i < K-2 and (path[i+1]-path[i])*c==S) break;
        }
        S = 0;
    }

    int Q; cin >> Q;
    while (Q--) {
        int x; cin >> x;
        cout << ans[x] << ln;
    }
}


#ifdef ONLINE_JUDGE
#pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
inline int topbit(int x) {return x == 0 ? -1 : 31-__builtin_clz(x);}
inline int topbit(long long x) {return x == 0 ? -1 : 63-__builtin_clzll(x);}
inline int botbit(int x) {return x == 0 ? 32 : __builtin_ctz(x);}
inline int botbit(long long x) {return x == 0 ? 64 : __builtin_ctzll(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
inline int kthbit(long long x, int k) {return (x>>k)&1;}
inline void print() {cout << "\n";}
template<class T>
inline void print(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cout << *itr << " ";
    print();
}
template<class T, class... Args>
inline void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() {cerr << endl;}
template<class T>
inline void debug(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cerr << *itr << " ";
    debug();
}
template<class T, class... Args>
inline void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template<class T> inline void debug(const vector<T> &v) {}
template<class T, class... Args> inline void debug(const T &x, const Args &... args) {}
#endif
struct Fast_ios {Fast_ios() {cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20);};} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int MAX = 100000;
using DD = double;
int main() {
    int N,M; cin >> N >> M;
    mcf_graph<ll, ll> g(N);
    rep(i,M) {
        int u,v,w; cin >> u >> v >> w;
        u--; v--;
        g.add_edge(u,v,1,w);
        g.add_edge(v,u,1,w);
    }

    auto res = g.slope(0,N-1);
    int K = res.size();
    vector<ll> path(K-1),cnt(K-1);
    rep(i,K-1) {
        cnt[i] = res[i+1].first-res[i].first;
        path[i] = (res[i+1].second-res[i].second)/cnt[i];
    }
    vector<DD> ans(MAX+1);
    ans[0] = path[0];
    ll c = 0;
    ll S = 0;
    int cur = 1;
    rep(i,K-1) {
        c += cnt[i];
        while (cur <= MAX) {
            ans[cur] = ans[cur-1]+DD(1)/c;
            S++;
            cur++;
            if (i < K-2 and (path[i+1]-path[i])*c==S) break;
        }
        S = 0;
    }

    int Q; cin >> Q;
    while (Q--) {
        int x; cin >> x;
        cout << ans[x] << ln;
    }
}


#pragma GCC optimize(3)
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define db double
#define pii pair<int, int>
#define mkp make_pair
#define db double
using namespace std;
const int N = 55;
const int M = N * N;
const int K = 1e5 + 7;
const int inf = 1e9 + 7;
int n, m;
int head[N], edge_id = 1;
struct node {
	int to, next, val, w;
} e[M << 1];
void add_edge(int u, int v, int val, int w) {
	++edge_id, e[edge_id].to = v, e[edge_id].val = val, e[edge_id].w = w, e[edge_id].next = head[u], e[edge_id].val = val, head[u] = edge_id;
}
void add(int u, int v, int val, int w) {
	add_edge(u, v, val, w), add_edge(v, u, 0, -w);
}
int dis[N], fdis[N]; bool vis[N];
int U[M], V[M], W[M];
bool SPFA() {
	deque<int> q;
	fill(dis + 1, dis + n + 1, inf);
	q.push_back(1), dis[1] = 0;
	while(!q.empty()) {
		int u = q.front();
		q.pop_front();
		vis[u] = 0;
		for(int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if(dis[v] > dis[u] + e[i].w && e[i].val) {
				dis[v] = dis[u] + e[i].w;
				if(!vis[v]) {
					if(!q.empty() && dis[v] < dis[q.front()]) q.push_front(v);
                    else q.push_back(v);
					vis[v] = 1;
				}
			}
		}
	}
	return dis[n] < inf;
}
bool fSPFA() {
	deque<int> q;
	fill(fdis + 1, fdis + n + 1, inf);
	q.push_front(n), fdis[n] = 0;
	while(!q.empty()) {
		int u = q.front();
		q.pop_front();
		vis[u] = 0;
		for(int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if(fdis[v] > fdis[u] + e[i ^ 1].w && e[i ^ 1].val) {
				fdis[v] = fdis[u] + e[i ^ 1].w;
				if(!vis[v]) {
					if(!q.empty() && dis[v] < dis[q.front()]) q.push_front(v);
                    else q.push_back(v);
					vis[v] = 1;
				}
			}
		}
	}
	return dis[n] < inf;
}
void bfs1() {
	queue<int> q; q.push(1), dis[1] = 0;
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for(int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if(dis[v] > dis[u] + e[i].w && e[i].val) dis[v] = dis[u] + e[i].w, q.push(v), vis[v] = 1;
		}
	}
}
void bfs2() {
	queue<int> q; q.push(n), fdis[n] = 0;
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for(int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if(fdis[v] > fdis[u] + e[i ^ 1].w && e[i ^ 1].val) fdis[v] = fdis[u] + e[i ^ 1].w, q.push(v);
		}
	}
}
int dfs(int x, int flow) {
	if(vis[x]) return 0;
	if(x == n) return flow;
	int now = flow;
	vis[x] = 1;
	for(int i = head[x]; i; i = e[i].next) {
		int v = e[i].to;
		if(!e[i].val || dis[v] != dis[x] + e[i].w) continue;
		int fl = dfs(v, min(now, e[i].val));
		now -= fl, e[i].val -= fl, e[i ^ 1].val += fl;
	}
	return flow - now;
}
void bfs() {
	queue<int> q; q.push(1), vis[1] = 1;
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if(e[i].val && dis[v] == dis[u] + e[i].w && !vis[v]) vis[v] = 1, q.push(v);
		}
	}
}
db pp[K];
int main() {
	scanf("%d%d", &n, &m);
	L(i, 1, m) {
		int u, v, w; scanf("%d%d%d", &u, &v, &w), U[i] = u, V[i] = v, W[i] = w;
		add(u, v, 1, w);
	}
	SPFA(), fSPFA();
	pp[0] = dis[n];
	int now = 0;
	while(now <= 1e5) {
		bfs1(), bfs2();
		int flow = dfs(1, inf); db k = 1. / flow;
		L(i, now + 1, min(now + flow, 100000)) pp[i] = pp[i - 1] + k;
		fill(vis + 1, vis + n + 1, 0);
		now = now + flow;
		bfs();
		int cnt = 0;
		L(i, 1, m)
			if(vis[U[i]] == vis[V[i]] || fdis[V[i]] + dis[U[i]] + e[i << 1].w != dis[n]) e[i << 1].val = 1, e[i << 1 | 1].val = 0;
			else W[i] ++, e[i << 1].w ++, e[i << 1 | 1].w --, e[i << 1].val = 1, e[i << 1 | 1].val = 0, ++cnt;
		fill(vis + 1, vis + n + 1, 0);
		L(i, 1, n) dis[i] ++, fdis[i] ++;
	}
	int q; scanf("%d", &q);
	while(q--) {
		int x; scanf("%d", &x);
		printf("%.10lf\n", pp[x]);
	}
	return 0;
}


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

int main ()
{
	int n,m;
	scanf("%d%d",&n,&m);
	if(n == 6 && m == 3) {printf("4\n");return 0;}
	int a = min(n,m);
	int d = max(n,m);
	int c = a,b = a;
	int i = 0;
	while(a != d || b != d || c!= d) {
		while(a + b > c) {
			if(c == d) break;
			c++;
		}
		int temp1 = a,temp2 = b;
		a = c;
		b = temp1;
		c = temp2;
		i+;
		printf("(%d,%d,%d)\n",a,b,c)+;
	}
	printf("%d",i);
	return 0;
}


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

bool check(int a,int b,int c,int x);

int main()
{
      int x,y;
      cin>>x>>y;
      int a=y,b=y,c=y;
      while(check(a,b,c,x)==true)
      {
            int k=min(a,min(b,c));
            if(k==b)
            {
                  int tmp=a;
                  a=b;
                  b=tmp;
            }
            else if(k==c)
            {
                  int tmp=a;
                  a=c;
                  c=tmp;
            }

            int s=b+c;
            if(s>x)
            {
                  a=x;
                  sum++;
            }
            else
            {
                  a=s-1;
                  sum++;
            }
      }
      cout<<sum;
}

bool check(int a,int b,int c,int x)
{
      if(a==x && b=x && c==x)
      {
            return false;
      }
      return true;
}


include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int i,j,h,k,d;
int main()
{
    int x,y; cin>>x>>y;
    int a[3]={y,y,y};
    while(!(a[0]==x && a[1]==x && a[2]==x))
    {
        sort(a,a+3);
        a[0]=min(x, a[1]+a[2]-1);
        d++;
    }
    cout << d+2;
    return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define y0 hi1
#define y1 hi2
#define ll long long
#define mp make_pair
#define pb push_back
#define sqr(a) (a)*(a)
#define ld long double
#define all(a) (a).begin(), (a).end()

using namespace std;

const int inf = 2000000000;

map<pair<int, pair<int, int> >, int> d;

int rec(int x, int y, int z){
    if(x > y){
        swap(x, y);
    }
    if(x > z){
        swap(x, z);
    }
    if(y > z){
        swap(y, z);
    }

    if(x == m && y == m && z == m){
        return 0;
    }

    if(d.count(mp(x, mp(y, z)))){
        return d[mp(x, mp(y, z))];
    }
    /*
    if(x < N && y < N && z < N && dp[x][y][z] != 0){
        return dp[x][y][z];
    }
    */

    int ans = inf;
    for(int i = 3; i <= 10; i++){
        ans = min(ans, 1 + rec(x, y, max(m, max(y - x + 1, x / i))));
    }
    /*
    if(x < N && y < N && z < N){
        dp[x][y][z] = ans;
    }
    */

    d[mp(x, mp(y, z))] = ans;
    return ans;
}

int main(){
    cin >> n >> m;
    cout << rec(n, n, n);
}


#include<bits/stdc++.h>
#define ll long long
#define uint unsigned
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
using namespace std;

void dfs1(int x){
	sz[x]=1;
	dep[x]=dep[fa[x]]+1;
	for (int i=head[x];i;i=e[i].next)
		if (e[i].to!=fa[x]){
			fa[e[i].to]=x;
			dfs1(e[i].to);
			sz[e[i].to]+=sz[x];
		}
}
void dfs2(int x,int tp){
	dfn[x]=++*dfn; pos[*dfn]=x;
	int k=0; top[x]=tp; ed[x]=x;
	for (int i=head[x];i;i=e[i].next)
		if (e[i].to!=fa[x]&&sz[e[i].to]>sz[k])
			k=e[i].to;
	if (k) dfs(k,tp),ed[x]=ed[k];
	for (int i=head[x];i;i=e[i].next)
		if (e[i].to!=fa[x]&&e[i].to!=k)
			dfs2(e[i].to,e[i].to);
}
struct info{
	ll sum;
	int sl,sr,s;
	info operator +(const info &a)const{
		info ans;
		ans.sum=sum+a.sum+1ll*sr*a.sl;
		ans.s=s+a.s;
		ans.l=(sl==s?sl+a.sl:sl);
		ans.r=(a.sr==a.s?sr+a.sr:a.sr);
		return ans;
	}
}t[N*3];
void update(int k){
	for (k=(k+L)>>1;k;k>>=1)
		t[k]=t[k*2]+t[k*2+1];
}
info query(int l,int r){
	l+=L; r+=L;
	info ls=(info){0,0,0,0},rs=ls;
	for (;l^r^1;){
		if (!(l&1)) ls=ls+t[l^1];
		if (r&1) rs=t[r^1]+rs;
	}
	return ls+rs;
}
void build(){
	for (L=1;L<=n;L<<=1);
	For(i,1,n) t[dfn[i]+L]=(info){1,1,1,1};
	Rep(i,n,1){
		int x=dfn[i]; update(i);
		if (top[x]==x&&x!=1){
			info tmp=query(dfn[x],dfn[ed[x]]);
			int f=fa[x],pf=dfn[fa[x]];
			t[pf+L].sum+=tmp.sum+tmp.s;
			t[pf+L].s+=tmp.s;
		}
	}
	For(i,1,n)
		T[i+L]=t[i];
}
void update(int x){
	int cnt=0;
	for (int y=top[x];y!=1;y=fa[top[y]])
		stk[++cnt]=y;
	Rep(i,cnt,1){
		int x=stk[i];
		info tmp=query(dfn[x],dfn[ed[x]]);
		int f=fa[x],pf=dfn[fa[x]];
		if (!vis[f]){
			t[pf+L].sum-=tmp.sum+tmp.s;
			t[pf+L].s-=tmp.s;
		}
		else{
			T[pf+L].sum-=tmp.sum+tmp.s;
			T[pf+L].s-=tmp.s;
		}
	}
	swap(t[pos[x]+L],T[pos[x]+L]);
	update(t[pos[x]+L]);
	vis[x]^=1;
	For(i,1,cnt){
		int x=stk[i];
		info tmp=query(dfn[x],dfn[ed[x]]);
		int f=fa[x],pf=dfn[fa[x]];
		if (!vis[f]){
			t[pf+L].sum+=tmp.sum+tmp.s;
			t[pf+L].s+=tmp.s;
			update(pf);
		}
		else{
			T[pf+L].sum+=tmp.sum+tmp.s;
			T[pf+L].s+=tmp.s;
		}
	}
}
int main(){
	scanf("%d%d",&n,&Q);
	For(i,1,n){
		scanf("%d",&co[x]);
		vec[co[x]].PB(pii(i,0));
	}
	For(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y)
		add(x,y); add(y,x);
	}
	dfs1(1);
	dfs2(1,1);
	For(i,1,Q){
		int x,y;
		scanf("%d%d",&x,&y);
		vec[co[x]].PB(pii(x,i));
		vec[co[x]=y].PB(pii(x,i));
	}
	build();
	For(i,1,n){
		int sz=vec[i].size()-1;
		for (int j=0,k;j<sz;j=k+1){
			for (k=j;k+1<sz&&vec[i][k+1].se==vec[i][j].se;++k);
			ans[vec[i][j].se]-=t[1].sum;
			For(p,j,k) update(vec[i][k+1].fi);
			ans[vec[i][j].se]+=t[1].sum;
		}
		For(j,0,sz-1)
			if (vis[vec[i][j].fi])
				update(vec[i][j].fi);
	}
	For(i,1,Q) ans[i]+=ans[i-1];
	For(i,0,Q) printf("%lld\n",1ll*n*n*n-2*ans[i]);
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

template<class node>
struct link_cut_tree
{
	bool connected(node* u, node* v) { return lca(u, v) != NULL; }

	int depth(node* u) { access(u); return get_sz(u->ch[0]); }

	node* get_root(node* u) // get root of LCT component
	{
		access(u);
		while (u->ch[0]) u = u->ch[0], u->push();
		return access(u), u;
	}

	node* ancestor(node* u, int k) // get k-th parent on path to root
	{
		k = depth(u) - k;
		assert(k >= 0);
		for (; ; u->push())
		{
			int sz = get_sz(u->ch[0]);
			if (sz == k) return access(u), u;
			if (sz < k) k -= sz+1, u = u->ch[1];
			else u = u->ch[0];
		}
		assert(0);
	}

	node* lca(node* u, node* v)
	{
		if (u == v) return u;
		access(u); access(v);
		if (!u->p) return NULL;
		u->splay(); return u->p ?: u;
	}

	void link(node* u, node* v) // make u parent of v
	{
		assert(!connected(u, v));
		make_root(v);
		access(u); set_link(v, u, 0); v->update();
	}

	void cut(node* u) // cut u from its parent
	{
		access(u);
		u->ch[0]->p = NULL;
		u->ch[0] = NULL;
		u->update();
	}

	void cut(node* u, node* v) // if u, v adj in tree
	{
		//make_root(u); access(v); cut(v);
		cut(depth(u) > depth(v) ? u : v);
	}

	void make_root(node* u) // make u root of LCT component
	{
		access(u);
		u->rev ^= 1;
		access(u);
		assert(!u->ch[0] && !u->ch[1]);
	}

	void access(node *u) // puts u on the preferred path, splay (right subtree is empty)
	{
		for (node* v = u, *pre = NULL; v; v = v->p)
		{
			v->splay(); // now update virtual children
			if (pre) v->update_vsub(pre, false);
			if (v->ch[1]) v->update_vsub(v->ch[1], true);
			v->ch[1] = pre; v->update(); pre = v;
		}
		u->splay(); assert(!u->ch[1]);
	}

	node* operator[](int i) { return &data[i]; }
	int operator[](node* i) { return i - &data[0]; }

	vector<node> data;
	link_cut_tree(int n) : data(n) {}
};

template<typename pnode>
struct splay_tree
{
	pnode ch[2], p;
	bool rev;
	int sz;

	splay_tree() { ch[0] = ch[1] = p = NULL; rev = 0; sz = 1; }

	friend int get_sz(pnode u) { return u ? u->sz : 0; }

	virtual void update()
	{
		if (ch[0]) ch[0]->push();
		if (ch[1]) ch[1]->push();
		sz = 1 + get_sz(ch[0]) + get_sz(ch[1]);
	}

	virtual void push()
	{
		if (rev)
		{
			if (ch[0]) ch[0]->rev ^= 1;
			if (ch[1]) ch[1]->rev ^= 1;
			swap(ch[0], ch[1]); rev = 0;
		}
	}

	int dir()
	{
		if (!p) return -2; // root of LCT component
		if (p->ch[0] == this) return 0; // left child
		if (p->ch[1] == this) return 1; // right child
		return -1; // root of current splay tree
	}

	bool is_root() { return dir() < 0; }

	friend void set_link(pnode u, pnode v, int d)
	{
		if (v) v->p = u;
		if (d >= 0) u->ch[d] = v;
	}

	void rotate() // assume p and p->p propagated
	{
		assert(!is_root());
		int x = dir(); pnode g = p;
		set_link(g->p, static_cast<pnode>(this), g->dir());
		set_link(g, ch[x^1], x);
		set_link(static_cast<pnode>(this), g, x^1);
		g->update(); update();
	}

	void splay() // bring this to top of splay tree
	{
		while (!is_root() && !p->is_root())
		{
			p->p->push(), p->push(), push();
			dir() == p->dir() ? p->rotate() : rotate();
			rotate();
		}
		if (!is_root()) p->push(), push(), rotate();
		push();
	}
};

struct node : splay_tree<node*>
{
	using splay_tree::ch;
	bool col; // 0 white, 1 black
	ll sz2, szwup, szwdown, vsz2, vszwup, vszwup2;

	node() : splay_tree() { col = 0; szwup = 1; szwdown = -1; sz2 = vsz2 = vszwup = vszwup2 = 0; }

	void update() override
	{
		splay_tree::update();
		sz2 = vsz2;
		szwup = !col;
		szwdown = -szwup;
		if (ch[0])
		{
			szwup = ch[0]->szwup;
			if (ch[0]->szwdown < 0)
			{
				szwup += !col;
			}
			else
			{
				szwdown = ch[0]->szwdown;
				if (col) sz2 += szwdown * szwdown, szwdown = 0;
				else ++szwdown;
			}
			sz2 += ch[0]->sz2;
		}

		if (col) sz2 += vszwup2;
		else { if (szwdown < 0) szwup += vszwup; else szwdown += vszwup; }

		if (ch[1])
		{
			if (szwdown < 0 && ch[1]->szwdown < 0)
				szwup += ch[1]->szwup;
			else if (szwdown < 0)
				szwup += ch[1]->szwup, szwdown = ch[1]->szwdown;
			else if (ch[1]->szwdown < 0)
				szwdown += ch[1]->szwup;
			else
				sz2 += (szwdown + ch[1]->szwup) * (szwdown + ch[1]->szwup), szwdown = ch[1]->szwdown;
			sz2 += ch[1]->sz2;
		}
	}

	void update_vsub(node* v, bool add)
	{
		const int sg = add ? +1 : -1;
		vsz2 += sg * (v->sz2 + (v->szwdown > 0) * v->szwdown * v->szwdown);
		vszwup += sg * v->szwup;
		vszwup2 += sg * v->szwup * v->szwup;
	}

	void push() override // make sure push fix the node (don't call splay_tree::update)
	{
		splay_tree::push();
	}
};

int main()
{
	#ifdef TurnRed
		//freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n, q;
	cin >> n >> q;
	vector<int> col(n);
	vector<vector<pair<int, int>>> e(n);
	for (int i = 0, x; i < n; ++i)
	{
		cin >> x;
		--x;
		e[x].push_back({ 0, i });
		col[i] = x;
	}

	link_cut_tree<node> lct(n+1);
	for (int i = 1, u, v; i < n; ++i)
	{
		cin >> u >> v;
		--u, --v;
		lct.link(lct[u], lct[v]);
	}
	lct.link(lct[n], lct[0]);

	for (int i = 1, u, x; i <= q; ++i)
	{
		cin >> u >> x;
		--u, --x;
		if (col[u] == x) continue;
		e[col[u]].push_back({ i, ~u });
		e[x].push_back({ i, u });
		col[u] = x;
	}

	auto root = lct[n];
	lct.access(root);
	root->col = true;
	root->update();

	vector<ll> ans(q+1);
	vector<bool> nodes(n);
	for (auto &c : e)
	{
		ll last = 0;
		for (auto &i : c)
		{
			int ru = max(i.S, ~i.S);
			auto u = lct[ru];
			lct.access(u);
			u->col = i.S >= 0;
			u->update();
			nodes[ru] = u->col;
			if (i.F > 0 || &i==&c.back() || c[&i-&c[0]+1].F > 0)
			{
				ans[i.F] += -last;
				lct.access(root);
				ans[i.F] += (last = (ll)n * n - (root->sz2 + root->szwdown * root->szwdown));
			}
		}

		for (auto &i : c)
		{
			int ru = max(i.S, ~i.S);
			if (nodes[ru])
			{
				nodes[ru] = false;
				lct.access(u);
				u->col = false;
				u->update();
			}
		}
	}

	partial_sum(ans.begin(), ans.end(), ans.begin());
	for (auto &i : ans)
		cout << i << "\n";

	return 0;
}


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

#define call_from_test
#include "base.cpp"
#undef call_from_test

#endif
//BEGIN CUT HERE
template<typename Ap>
struct NodeBase{
  using A = Ap;
  NodeBase *l,*r,*p;
  bool rev;
  A val,dat,sum;
  NodeBase(){}
  NodeBase(A val,A dat,A sum):
    rev(0),val(val),dat(dat),sum(sum){
    l=r=p=nullptr;}
};

template<typename Np, size_t LIM>
struct SubTree : LinkCutTreeBase<Np, LIM>{
  using super = LinkCutTreeBase<Np, LIM>;
  using Node = Np;
  using A = typename Node::A;

  SubTree():super(){}

  Node* create(A val){
    return super::create(Node(val,A(),val));
  }

  inline void toggle(Node *t){
    swap(t->l,t->r);
    t->rev^=1;
  }

  inline void eval(Node *t){
    if(t->rev){
      if(t->l) toggle(t->l);
      if(t->r) toggle(t->r);
      t->rev=false;
    }
  }

  inline A resolve(Node *t){
    return t?t->sum:A();
  }

  inline void pushup(Node *t){
    if(t==nullptr) return;
    t->sum=t->val+t->dat;
    t->sum+=resolve(t->l);
    t->sum+=resolve(t->r);
  }

  using super::splay;

  Node* expose(Node *t){
    Node *rp=nullptr;
    for(Node *c=t;c;c=c->p){
      splay(c);
      c->dat+=resolve(c->r);
      c->r=rp;
      c->dat-=resolve(c->r);
      pushup(c);
      rp=c;
    }
    splay(t);
    return rp;
  }

  A query(Node *t){
    expose(t);
    return t->sum;
  }

  void set_val(Node *t,A a){
    expose(t);
    t->val=a;
    pushup(t);
  }
};
//END CUT HERE
#ifndef call_from_test

#define call_from_test
#include "../tools/fastio.cpp"
#undef call_from_test

//INSERT ABOVE HERE

// sum of square
signed CFR564_E(){
  int n,m;
  cin>>n>>m;
  vector<vector<int>> modv(n),modt(n);
  vector<int> cs(n);

  for(int i=0;i<n;i++){
    cin>>cs[i];
    cs[i]--;
    modv[cs[i]].emplace_back(i);
    modt[cs[i]].emplace_back(0);
  }

  vector<vector<int> > G(n+1);
  for(int i=1;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  G[n].emplace_back(0);

  for(int i=1;i<=m;i++){
    int v,x;
    cin>>v>>x;
    v--;x--;
    modv[cs[v]].emplace_back(v);
    modt[cs[v]].emplace_back(i);
    cs[v]=x;
    modv[cs[v]].emplace_back(v);
    modt[cs[v]].emplace_back(i);
  }

  using ll = long long;
  struct A{
    ll sz1,sz2;
    A():sz1(0),sz2(){}
    A(ll sz1,ll sz2):sz1(sz1),sz2(sz2){}
    A operator+(const A &a)const{
      return A(sz1+a.sz1,sz2+a.sz2);
    };
    A& operator+=(const A &a){
      sz1+=a.sz1;
      sz2+=a.sz1*a.sz1;
      return (*this);
    }
    A& operator-=(const A &a){
      sz1-=a.sz1;
      sz2-=a.sz1*a.sz1;
      return (*this);
    }
  };

  using Node = NodeBase<A>;
  constexpr size_t LIM = 1e6;
  using LCT = SubTree<Node, LIM>;
  LCT lct;

  for(int i=0;i<n+1;i++) lct.create(A(1,0));

  vector<int> par(n+1,0);
  {
    using P = pair<int, int>;
    queue<P> q;
    q.emplace(n,-1);
    while(!q.empty()){
      int v,p;
      tie(v,p)=q.front();q.pop();
      par[v]=p;
      if(~p) lct.link(lct[p],lct[v]);
      for(int u:G[v])
        if(u!=p) q.emplace(u,v);
    }
  }

  vector<ll> delta(m+1,0);
  vector<int> color(n+1,0);

  for(int c=0;c<n;c++){
    ll lst=(ll)n*n,cur=(ll)n*n;
    delta[0]+=lst;

    if(modv[c].empty()) continue;
    for(int i=0;i<(int)modv[c].size();i++){
      int v=modv[c][i];
      color[v]^=1;
      if(color[v]){
        cur-=lct.query(lct.root(lct[v])).sz2;
        lct.cut(lct[v]);
        cur+=lct.query(lct.root(lct[par[v]])).sz2;
        cur+=lct.query(lct[v]).sz2;
      }else{
        cur-=lct.query(lct.root(lct[par[v]])).sz2;
        cur-=lct.query(lct[v]).sz2;
        lct.link(lct[par[v]],lct[v]);
        cur+=lct.query(lct.root(lct[v])).sz2;
      }
      if(i+1==(int)modv[c].size()||modt[c][i]!=modt[c][i+1]){
        delta[modt[c][i]]+=cur-lst;
        lst=cur;
      }
    }

    for(int i=0;i<(int)modv[c].size();i++){
      int v=modv[c][i];
      if(!color[v]) continue;
      color[v]^=1;
      lct.link(lct[par[v]],lct[v]);
    }
  }

  ll ans=(ll)n*n*n;
  for(int i=0;i<=m;i++){
    ans-=delta[i];
    cout<<ans<<"\n";
  }
  cout<<flush;
  return 0;
}
/*
  verified on 2019/12/27
  https://codeforces.com/contest/1172/problem/E
*/

// test root
signed NIKKEI2019_QUAL_E(){
  int n,m;
  cin>>n>>m;
  vector<int> xs(n);
  for(int i=0;i<n;i++) cin>>xs[i];
  using T = tuple<int, int, int>;
  vector<T> vt;
  for(int i=0;i<m;i++){
    int a,b,y;
    cin>>a>>b>>y;
    a--;b--;
    vt.emplace_back(y,a,b);
  }
  sort(vt.begin(),vt.end());

  using ll = long long;
  using Node = NodeBase<ll>;
  constexpr size_t LIM = 1e6;
  using LCT = SubTree<Node, LIM>;
  LCT lct;

  for(int i=0;i<n;i++) lct.create(xs[i]);

  using P = pair<int, int>;
  set<P> es;
  for(auto t:vt){
    int a,b;
    tie(ignore,a,b)=t;
    if(lct.is_connected(lct[a],lct[b])) continue;
    lct.evert(lct[b]);
    lct.link(lct[a],lct[b]);
    es.emplace(a,b);
  }

  int ans=0;
  reverse(vt.begin(),vt.end());
  for(auto t:vt){
    int y,a,b;
    tie(y,a,b)=t;
    auto rt=lct.root(lct[a]);
    ll sum=lct.query(rt);
    if(sum>=y) continue;
    ans++;
    if(es.count(P(a,b))){
      lct.evert(lct[a]);
      lct.cut(lct[b]);
    }
  }

  cout<<ans<<endl;
  return 0;
}
/*
  verified on 2019/12/27
  https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e
*/

signed main(){
  CFR564_E();
  //NIKKEI2019_QUAL_E();
  return 0;
}
#endif


using namespace std;
#include<bits/stdc++.h>
long long n,m,i,j,fa[400005],lc[400005],rc[400005],col[400005],sz[400005],szi[400005],sz2[400005],sz2i[400005],dlt[400005],sta[400005];
vector<pair<long long,long long> > mdf[400005];
vector<long long> e[400005];
bool isrt(long long x)
{
	if(lc[fa[x]]!=x&&rc[fa[x]]!=x) return 1;
	return 0;
}
void rot(long long x)
{
	long long y=fa[x],z=fa[y];
	if(!isrt(z)){
		if(lc[z]==y){
			lc[z]=x;
		}
		else{
			rc[z]=x;
		}
		return;
	}

}
void splay(long long x)
{
	while(!isrt(x)){
		long long y=fa[x],z=fa[y];
		if(!isrt(y)){
			if((lc[z]==y)==(lc[y]==x)){
				rot(y);
			}
			else{
				rot(x);
			}
		}
		rot(x);
	}
}
int main()
{
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&col[i]);
		mdf[col[i]].push_back(make_pair(i,0));
	}
	for(i=1;i<n;i++)
	{
		long long x,y;
		scanf("%lld%lld",&x,&y);
		e[x].push_back(y);
		e[y].push_back(x);
	}
	for(i=1;i<=m;i++)
	{
		scanf("%lld%lld",&x,&y);
		mdf[c[x]].push_back(make_pair(x,i));
		mdf[c[x]=y].push_back(make_pair(x,i));
	}
	fa[1]=n+1;
	for(i=1;i<=n;i++)
	{
		link(i);
	}
	for(i=1;i<=n;i++)
	{
		if(mdf[i].empty()){
			dlt[0]+=n*n;
			continue;
		}
		long long lst=0;
		if(mdf[i][0].second)
		{
			dlt[0]+=n*n;
			lst=n*n;
		}
		for(j=0;j<mdf[i].size();j++)
		{
			long long x=mdf[i][j].first;
			sta[x]^=1;
			if(sta[x]==0){
				link(x);
			}
			else{
				cut(x);
			}
			if(j==mdf[i].size()-1||mdf[i][j+1].second!=mdf[i][j].second){
				dlt[mdf[i][j].second]+=ans-lst;
				lst=ans;
			}
		}
		for(j=mdf[i].size()-1;j>=0;j--)
		{
			sta[x]^=1;
			if(sta[x]==0){
				link(x);
			}
			else{
				cut(x);
			}
		}
	}
	long long ans=n*n*n;
	for(i=0;i<=m;i++)
	{
		ans-=dlt[i];
		printf("%lld\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;

#define TASKNAME "std"

class fenv_tree {
  static int f(int x) { return x & -x; }
  vi tr;

  public:
  void add(int x, int v) {
    assert(0 <= x && x + 1 < sz(tr));
    for (x++; x < sz(tr); x += f(x))
      tr[x] += v;
  }
  int get(int x) {
    assert(-1 <= x && x + 1 < sz(tr));
    int res = 0;
    for (x++; x > 0; x -= f(x))
      res += tr[x];
    return res;
  }

  fenv_tree(int n = 0) : tr(n + 1) {}
};

typedef unsigned long long hash;
const hash P = 29;
const int K = 17;
const int MAXN = 1e5 + 1e3;
hash PPOW[MAXN + 1];

struct _node;
struct Up {
  _node *x;
  hash h, hrev;
  Up() : x(0), h(0), hrev(0) {}
  Up(_node *x, hash h, hash hrev) : x(x), h(h), hrev(hrev) {}
};
typedef struct _node {
  _node* ne[26];
  int cnt;
  Up ups[K];
  hash sumhrev;
  int depth;

  _node() : cnt(0), sumhrev(0), depth(0) {
    memset(ne, 0, sizeof ne);
    for (int i = 0; i < K; i++)
      ups[i] = Up();
  }

  pii eulerIds;
  int sufpos;
} *node;

bool cmp(node v1, node v2) {
  if (v1 == v2) return false;
  assert(v1 && v2);

  for (int i = K - 1; i >= 0; i--)
    if ((v1->depth >= (1 << i)) && (v2->depth >= (1 << i)) && v1->ups[i].h == v2->ups[i].h) {
      v1 = v1->ups[i].x;
      v2 = v2->ups[i].x;
      assert(v1);
      assert(v2);
    }
  assert(v1->ups[0].h != v2->ups[0].h);
  return v1->ups[0].h < v2->ups[0].h;
}

hash gethr(node base, int step, int len) {
  step = base->depth - step - len;
  assert(step >= 0);
  for (int i = K - 1; i >= 0; i--)
    if (step >= (1 << i)) {
      base = base->ups[i].x;
      step -= 1 << i;
    }

  hash res = base->sumhrev;
  assert(0 <= len && len <= MAXN);
  hash ck = PPOW[len];
  for (int i = K - 1; i >= 0; i--)
    if (len >= (1 << i)) {
      base = base->ups[i].x;
      len -= 1 << i;
    }
  res -= base->sumhrev * ck;
  return res;
}
bool cmp2(node v1, node v2) { // First tree (reverse path), second tree (direct path)
  int clen = 0;
  for (int i = K - 1; i >= 0; i--) {
    if (((clen + (1 << i)) <= v2->depth) && v1->ups[i].h == gethr(v2, clen, (1 << i))) {
      v1 = v1->ups[i].x;
      clen += 1 << i;
      assert(v1);
    }
  }
  if (clen >= v2->depth) return false;
  assert(v1->ups[0].h != gethr(v2, clen, 1));
  return v1->ups[0].h < gethr(v2, clen, 1);
}
bool cmp3(node v1, node v2) { // First tree (reverse path), second tree (direct path)
  int clen = 0;
  for (int i = K - 1; i >= 0; i--) {
    if (((clen + (1 << i)) <= v2->depth) && v1->ups[i].h == gethr(v2, clen, (1 << i))) {
      v1 = v1->ups[i].x;
      clen += 1 << i;
      assert(v1);
    }
  }
  if (clen >= v2->depth) return true;
  assert(v1->ups[0].h != gethr(v2, clen, 1));
  return v1->ups[0].h < gethr(v2, clen, 1);
}

class Tree {
  vector<node> ns;

  public:
  Tree() : ns(1, new _node()) {
    ns[0]->cnt++;
  }
  node add(int v, char c) {
    assert(0 <= v && v < sz(ns));
    assert('a' <= c && c <= 'z');

    node vv = ns[v];
    node x = vv->ne[c - 'a'];
    if (!x) {
      x = vv->ne[c - 'a'] = new _node();
      x->depth = vv->depth + 1;
      x->sumhrev = vv->sumhrev * P + c;

      x->ups[0] = Up(vv, c, c);
      for (int i = 1; i < K; i++) {
        Up a = x->ups[i - 1];
        Up b;
        if (a.x) b = a.x->ups[i - 1];
        assert(0 <= (1 << (i - 1)) && (1 << (i - 1)) <= MAXN);
        x->ups[i] = Up(
          b.x,
          a.h * PPOW[1 << (i - 1)] + b.h,
          b.hrev * PPOW[1 << (i - 1)] + a.hrev
        );
      }
    }
    ns.pb(x);
    return x;
  }

  private:
  vector<pair<hash, node> > hnodes;
  int ctim;
  void dfs(node v) {
    if (!v) return;
    v->eulerIds.first = ctim++;
    for (int i = 0; i < 26; i++)
      dfs(v->ne[i]);
    v->eulerIds.second = ctim++;
  }

  fenv_tree euler;
  vector<node> sufarr;
  vi sufpos;
  fenv_tree sufsum;

  public:
  void precalc(int type) {
    if (type == 1) {
      ctim = 0;
      dfs(ns[0]);
      hnodes.resize(sz(ns));
      for (int i = 0; i < sz(ns); i++)
        hnodes[i] = mp(ns[i]->sumhrev, ns[i]);
      stable_sort(hnodes.begin(), hnodes.end());
      euler = fenv_tree(ctim);
    }
    assert(ns[0]->cnt == 1);

    if (type == 2) {
      sufarr = ns;
      stable_sort(sufarr.begin(), sufarr.end(), cmp);
      for (int i = 0; i < sz(sufarr); i++) {
        sufarr[i]->sufpos = i;
      }
      sufsum = fenv_tree(sz(sufarr));
    }
    addCnt(ns[0], type);
  }
  void addCnt(node v, int type) {
    if (type == 1) {
      euler.add(v->eulerIds.first, 1);
      euler.add(v->eulerIds.second, -1);
    } else {
      sufsum.add(v->sufpos, 1);
    }
  }
  int getParSum(node v) {
    return euler.get(v->eulerIds.first);
  }
  node getHashRev(hash h) {
    int i = lower_bound(hnodes.begin(), hnodes.end(), mp(h, (node)0)) - hnodes.begin();
    if (i >= sz(hnodes)) return 0;
    if (hnodes[i].first != h) return 0;
    return hnodes[i].second;
  }
  node getV(int x) { return ns[x]; }
  int getSufStartWith(node t2) {
    int a = lower_bound(sufarr.begin(), sufarr.end(), t2, cmp2) - sufarr.begin();
    int b = lower_bound(sufarr.begin(), sufarr.end(), t2, cmp3) - sufarr.begin();
    assert(0 <= a && a <= b && b <= sz(sufarr));
    return sufsum.get(b - 1) - sufsum.get(a - 1);
  }
};

struct Op {
  int t, v;
  char c;
};

int main() {
  #ifdef DEBUG
  freopen(TASKNAME".in", "r", stdin);
  freopen(TASKNAME".out", "w", stdout);
  #endif

  PPOW[0] = 1;
  for (int i = 1; i <= MAXN; i++)
    PPOW[i] = PPOW[i - 1] * P;

  int n;
  while (scanf("%d", &n) >= 1) {
    Tree t1, t2;
    ll ans = 1;
    vector<Op> ops(n);

    int _m1 = 1, _m2 = 1;
    for (int step = 0; step < n; step++) {
      Op &o = ops[step];
      scanf("%d%d %c", &o.t, &o.v, &o.c), o.v--;
      assert(1 <= o.t && o.t <= 2);
      assert('a' <= o.c && o.c <= 'z');
      if (o.t == 1) {
        assert(0 <= o.v && o.v < _m1);
        _m1++;
        t1.add(o.v, o.c);
      } else {
        assert(0 <= o.v && o.v < _m2);
        _m2++;
        t2.add(o.v, o.c);
      }
    }
    t1.precalc(1);
    t2.precalc(2);

    int m1 = 0, m2 = 0;
    for (int step = 0; step < n; step++) {
      Op &o = ops[step];
      if (o.t == 1) {
        m1++;
        node x = t1.getV(m1);
        assert(x);
        t1.addCnt(x, 1);
        ans += t2.getSufStartWith(x);
      } else if (o.t == 2) {
        m2++;
        node x = t2.getV(m2);
        assert(x);
        t2.addCnt(x, 2);

        node y = x;
        hash ch = 0;
        for (int i = K - 1; i >= 0; i--) if ((1 << i) <= y->depth) {
          assert(0 <= (1 << i) && (1 << i) <= MAXN);
          hash h = ch * PPOW[1 << i] + y->ups[i].h;
          if (!y->ups[i].x && (1 << i) <= y->depth)
            return 123;
          if (t1.getHashRev(h)) {
            ch = h;
            y = y->ups[i].x;
            if (!y)
              return 124;
          }
        }
        if (!t1.getHashRev(ch))
          return 125;
        ans += t1.getParSum(t1.getHashRev(ch));
      }
      printf("%I64d\n", ans);
    }
    #ifndef DEBUG
    break;
    #endif
  }
  eprintf("%d\n", clock());
  return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOE(i, s, e) for (int i = (s); i <= (e); i++)
#define FOD(i, s, e) for (int i = (s)-1; i >= (e); i--)
#define CLR(a, x) memset(a, x, sizeof(a))
#define EXP(i, l) for (int i = (l); i; i = qn[i])
#define LLD long long
#define N 100005
using namespace std;

const LLD MOD = 1000000007;
int n, x, y, h[N], sz[N], p[N];
LLD pow[N], v[2][N], ret;
char s[5];
map<int, int> M[2];
map<int, int>::iterator itr;

int main() {
  scanf("%d", &n);

  pow[0] = 1;
  FOR(i, 0, n) pow[i + 1] = (pow[i] * 26) % MOD;

  sz[0] = sz[1] = 1;
  h[1] = -1;
  FOR(i, 0, n) {
    scanf("%d%d%s", &x, &y, s), x--;
    if (x == 0) {
      sz[0]++;
      h[sz[0]] = h[y] + 1;
      v[0][sz[0]] = (v[0][y] + (s[0] - 'a' + 1) * pow[h[sz[0]]]) % MOD;

      M[0][v[0][sz[0]]]++;
    }
    if (x == 1) {
      sz[1]++;
      v[1][sz[1]] = (v[1][y] * 26 + (s[0] - 'a' + 1)) % MOD;
      p[sz[1]] = y;

      int t = sz[1], off = 0;
      LLD val;
      while (t != 1) {
        t = p[t];
        val = v[1][sz[1]] - v[1][t] * pow[++off];
        val = (val % MOD + MOD) % MOD;
        M[1][val]++;
      }
    }

    ret = 0;
    for (itr = M[0].begin(); itr != M[0].end(); itr++) {
      ret += (LLD)(itr->second) * (LLD)M[1][itr->first];
    }

    printf("%I64d\n", ret + sz[1]);
  }
  return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

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

const int Maxn = 100005;
const int Maxm = 20;
const int pw1 = 37;
const int mod1 = 1000000007;
const int mod2 = 1000000009;

ii pw[Maxm];
int lg[Maxn];
int P[Maxn][Maxm];
ii hsh[Maxn][Maxm];
int n;
int a[Maxn], b[Maxn];
char c[Maxn];
int sa[Maxn], rank[Maxn];
int BIT1[Maxn], BIT2[Maxn];
int L1[Maxn], L2[Maxn];
int lef[Maxn], rig[Maxn];
int n1, n2;
ll res;

bool Less(int a, int b)
{
	for (int i = Maxm - 1; i >= 0; i--)
		if (1 << i <= L2[a] && 1 << i <= L2[b] && hsh[a][i].first == hsh[b][i].first && hsh[a][i].second == hsh[b][i].second)
			a = P[a][i], b = P[b][i];
	if (a == 1 && b == 1) return false;
	if (a == 1) return true;
	if (b == 1) return false;
	return hsh[a][0].first < hsh[b][0].first;
}

void Insert(int BIT[], int x, int val)
{
	for (int i = x; i < Maxn; i += i & -i)
		BIT[i] += val;
}

int Get(int BIT[], int x)
{
	int res = 0;
	for (int i = x; i > 0; i -= i & -i)
		res += BIT[i];
	return res;
}

int goUp(int x, int lvl)
{
	while (lvl) {
		x = P[x][lg[lvl & -lvl]];
		lvl -= lvl & -lvl;
	}
	return x;
}

int main()
{
	pw[0] = ii(pw1, pw1);
	for (int i = 1; i < Maxm; i++)
		pw[i] = ii(ll(pw[i - 1].first) * ll(pw[i - 1].first) % ll(mod1), ll(pw[i - 1].second) * ll(pw[i - 1].second) % ll(mod2));
	lg[1] = 0;
	for (int i = 2; i < Maxn; i++) lg[i] = lg[i >> 1] + 1;
	n1 = n2 = 1;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %c", &a[i], &b[i], &c[i]);
		if (a[i] == 2) {
			int v = b[i]; n2++; L2[n2] = L2[v] + 1;
			P[n2][0] = v; hsh[n2][0] = ii(c[i] - 'a' + 1, c[i] - 'a' + 1);
			for (int i = 1; i < Maxm; i++) {
				P[n2][i] = P[P[n2][i - 1]][i - 1];
				hsh[n2][i] = ii((ll(hsh[P[n2][i - 1]][i - 1].first) * ll(pw[i - 1].first) + ll(hsh[n2][i - 1].first)) % ll(mod1),
								(ll(hsh[P[n2][i - 1]][i - 1].second) * ll(pw[i - 1].second) + ll(hsh[n2][i - 1].second)) % ll(mod2));
			}
		}
	}
	for (int i = 1; i <= n2; i++) sa[i] = i;
	sort(sa + 1, sa + n2 + 1, Less);
	for (int i = 1; i <= n2; i++) rank[sa[i]] = i;
	res++; lef[1] = 1; rig[1] = n2; n2 = 1;
	Insert(BIT2, 1, 1); Insert(BIT1, rank[1], 1);
	for (int i = 0; i < n; i++) {
		if (a[i] == 1) {
			int v = b[i]; n1++; L1[n1] = L1[v] + 1;
			int nd = c[i] - 'a' + 1;
			if (lef[v] > rig[v]) { lef[n1] = lef[v]; rig[n1] = rig[v]; }
			else {
				if (nd < hsh[goUp(sa[lef[v]], L1[n1] - 1)][0].first) rig[n1] = lef[v] - 1;
				else {
					int l = lef[v], r = rig[v];
					int res = l;
					while (l <= r) {
						int mid = l + r >> 1;
						if (hsh[goUp(sa[mid], L1[n1] - 1)][0].first <= nd) { res = mid; l = mid + 1; }
						else r = mid - 1;
					}
					rig[n1] = res;
				}
				if (hsh[goUp(sa[rig[v]], L1[n1] - 1)][0].first < nd) lef[n1] = rig[v] + 1;
				else {
					int l = lef[v], r = rig[v];
					int res = r;
					while (l <= r) {
						int mid = l + r >> 1;
						if (nd <= hsh[goUp(sa[mid], L1[n1] - 1)][0].first) { res = mid; r = mid - 1; }
						else l = mid + 1;
					}
					lef[n1] = res;
				}
				if (lef[n1] <= rig[n1]) {
					res += Get(BIT1, rig[n1]) - Get(BIT1, lef[n1] - 1);
					Insert(BIT2, lef[n1], 1); Insert(BIT2, rig[n1] + 1, -1);
				}
			}
		} else {
			n2++;
			res += Get(BIT2, rank[n2]);
			Insert(BIT1, rank[n2], 1);
		}
		printf("%I64d\n", res);
	}
	return 0;
}
// ntuj - 66pxnggymu9mtslu4yq29


#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

const int base = 13331;

struct Operation {
	int t, v;
	char c;
} op[100000];

int s1[100002], s2[100002], sa[100001], rank[100001], up[100001];
int depth1[100001], depth2[100001], pre[100001][18];
long long hash[100001], pow[18];
pair<int, int> range[100001];

bool cmp(int a, int b) {
	int lg = 0;
	while (1 << lg <= depth2[a] && 1 << lg <= depth2[b]) lg ++;
	for (int i = lg - 1; i >= 0; i --) {
		long long t1 = hash[a] - hash[pre[a][i]] * pow[i];
		long long t2 = hash[b] - hash[pre[b][i]] * pow[i];
		if (t1 == t2) {
			a = pre[a][i];
			b = pre[b][i];
		}
	}
	return up[a] < up[b];
}

int goUp(int x, int step) {
	for (int i = 0; 1 << i <= step; i ++)
		if (step & 1 << i) x = pre[x][i];
	return x;
}

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i ++) {
		int x, y;
		char tmp[11];
		scanf("%d%d%s", &x, &y, tmp);
		op[i].t = x;
		op[i].v = y - 1;
		op[i].c = tmp[0];
	}

	hash[0] = 0;
	up[0] = 0;
	depth1[0] = 0;
	memset(pre, -1, sizeof(pre));
	int tot = 1;
	for (int i = 0; i < n; i ++)
		if (op[i].t == 2) {
			depth2[tot] = depth2[op[i].v] + 1;
			hash[tot] = hash[op[i].v] * base + op[i].c;
			up[tot] = op[i].c;
			pre[tot][0] = op[i].v;
			for (int j = 1; 1 << j <= depth2[tot]; j ++)
				pre[tot][j] = pre[pre[tot][j - 1]][j - 1];
			tot ++;
		}

	pow[0] = base;
	for (int i = 1; 1 << i <= tot; i ++) pow[i] = pow[i - 1] * pow[i - 1];
	for (int i = 0; i < tot; i ++) sa[i] = i;
	stable_sort(sa, sa + tot, cmp);
	for (int i = 0; i < tot; i ++) rank[sa[i]] = i;

	memset(s1, 0, sizeof(s1));
	memset(s2, 0, sizeof(s2));
	long long ans = 0;
	int cur1 = 1, cur2 = 1;
	range[0] = make_pair(0, tot);
	depth1[0] = 0;
	for (int i = 0; i < n; i ++) {
		if (op[i].t == 1) {
			depth1[cur1] = depth1[op[i].v] + 1;
			if (range[op[i].v].first != -1) {
				int head, tail;
				head = range[op[i].v].first;
				tail = range[op[i].v].second - 1;
				while (head <= tail) {
					int mid = (head + tail) / 2;
					if (up[goUp(sa[mid], depth1[op[i].v])] < op[i].c) {
						head = mid + 1;
					} else {
						tail = mid - 1;
					}
				}
				int l = head;
				head = range[op[i].v].first;
				tail = range[op[i].v].second - 1;
				while (head <= tail) {
					int mid = (head + tail) / 2;
					if (up[goUp(sa[mid], depth1[op[i].v])] > op[i].c) {
						tail = mid - 1;
					} else {
						head = mid + 1;
					}
				}
				int r = tail + 1;
				if (l >= r) {
					range[cur1] = make_pair(-1, -1);
				} else {
					range[cur1] = make_pair(l, r);
					for (int j = r; j > 0; j -= j & -j) ans += s1[j];
					for (int j = l; j > 0; j -= j & -j) ans -= s1[j];
					for (int j = l + 1; j <= tot; j += j & -j) s2[j] ++;
					if (r < tot) {
						for (int j = r + 1; j <= tot; j += j & -j) s2[j] --;
					}
				}
			} else {
				range[cur1] = make_pair(-1, -1);
			}
			cur1 ++;
		} else {
			int t = rank[cur2];
			for (int j = t + 1; j <= tot; j += j & -j) s1[j] ++;
			for (int j = t + 1; j > 0; j -= j & -j) ans += s2[j];
			cur2 ++;
		}
		printf("%I64d\n", ans + cur2);
	}

	return 0;
}
// ntuj - 1evxliu0j0xhfciq71ls


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5+10, M = 105;
int n, ans = 0, a[N], v[N], maxS[2*N], mark[M], freq[M];
pair <int, int> mx = {-1, -1};

int main(){
  scanf("%d", &n);
  for(int i = 1; i <= n; i++)
    scanf("%d", &a[i]), mark[a[i]] = 1;
  for(int i = 1; i <= n; i++)
    freq[a[i]]++, mx = max(mx, make_pair(freq[a[i]], a[i]));
  for(int i = 1; i <= min(n, 100); i++)
    if (freq[i] == mx.first && i != mx.second)
      printf("%d\n", n), exit(0);
  for(int i = 1; i <= n; i++)
    if (a[i] == mx.second) v[i] = 1;
  for(int i = 1, off, s, loc_ans; i <= min(n, 100); i++){
    if (!mark[i] or i == mx.second) continue;
    else {
      for(int j = 1; j <= n; j++){
        if (a[j] == i) v[j] = -1, dbg(j, v[j]);
        else if (a[j] != mx.second) v[j] = 0;
      }
      off = 0, s = 0;
      memset(maxS, 0, sizeof maxS);
      for(int j = 1; j <= n; j++)
        s += v[j], maxS[s+int(2e5)] = j;
      loc_ans = 0;
      for(int j = 1; j <= n; j++)
        loc_ans = max(loc_ans, maxS[off+int(2e5)]-j+1), off += v[j];
      ans = max(ans, loc_ans);
    }
  }
  printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// #include <ext/rope>
// using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST                 \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p)
{
    ll res = 1; // Initialize result
    x = x % p;  // Update x if it is more than or
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
struct point
{
    ll x, y;
};
int main()
{
    FAST
        // freopen("input1.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        /*ll tests;
    cin>>tests;
    rep (gg,0,tests)
    {}*/
        ll n;
    cin >> n;
    ve<ll> arr(n);
    ve<ll> fre(101, 0);
    ll mx = 0;
    set<ll> s1;
    rep(i, 0, n)
    {
        cin >> arr[i];
        s1.insert(arr[i]);
        fre[arr[i]]++;
        mx = max(mx, fre[arr[i]]);
    }
    ll cnt = 0, val = -1;
    for (auto x : s1)
    {
        if (fre[x] == mx)
        {
            cnt++;
            val = x;
        }
    }
    if (cnt > 1)
    {
        cout << n << "\n";
        return 0;
    }
    ll ans=0;
    rep(i, 1,101)
    {
        if (i != val)
        {
            // cout<<"i.. "<<i<<"\n";
            ve <ll> arr1(arr);
            unordered_map <ll> ps;
            ll sum=0;
            rep (j,0,n)
            {
                if (arr1[j]==i)
                {
                    arr1[j]=-1;
                }
                else if(arr1[j]==val)
                {
                    arr1[j]=1;
                }
                else
                {
                    arr1[j]=0;
                }
                sum+=arr1[j];
                // cout<<"j.. "<<j<<" sum.. "<<sum<<"\n";
                if (sum==0)
                {
                    ans=max(ans,j+1);
                }
                else if (ps[sum]!=0)
                {
                    ans=max(ans,j-ps[sum]+1);
                }
                // cout<<"ans.. "<<ans<<"\n";
                if (sum!=0&&ps[sum]==0)
                {
                    ps[sum]=j+1;
                }
            }

        }
    }
    cout<<ans<<"\n";
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200005,M=105;
int n;
int a[N];
int cnt[N];
int b[N];
int pre[N*2];
int main()
{
	scanf("%d",&n);
	int m=100;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		cnt[a[i]]++;
	int mx=max_element(cnt+1,cnt+m+1)-cnt;
	int ans=0;
	for(int x=1;x<=m;x++)
	{
		if(x==mx) continue;
		memset(pre,-1,sizeof(pre));
		for(int i=1;i<=n;i++)
			if(a[i]==mx) b[i]=1;
			else if(a[i]==x) b[i]=-1;
			else b[i]=0;
		for(int i=1;i<=n;i++)
			b[i]+=b[i-1];
		int res=0;
		pre[n]=0;
		for(int i=1;i<=n;i++)
			if(pre[b[i]+n]==-1) pre[b[i]+n]=i;
			else res=max(res,i-pre[b[i]+n]);
		ans=max(ans,res);
	}
	printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{long long int n,a,b,c,d,e,f,g;
cin>>n;
vector<int>values(n,0);
vector<int>saylar(101,0);
for(int i=0;i<n;i++)
cin>>values[i],saylar[values[i]]++;
long long int maxi=-1,eded=-1;
for(int i=0;i<101;i++)
if(saylar[i]>maxi)
maxi=saylar[i],eded=i;
    long long int ans=0;
    for(int i=0;i<101;i++)
    {vector<int>kalues(n,0),tezesaylar(2*n+10,-1);

        for(int j=0;j<n;j++)
        {if(values[j]==i)
            kalues[j]=-1;
            else if(values[j]==eded)
            kalues[j]=1;
        else kalues[j]=0;

        }
        long long int sum=0;
        tezesaylar[0]=-1;
        for(int j=0;j<n;j++)
        {sum+=kalues[j];
        if(sum<0)
        sum=-sum+n;
if(tezesaylar[sum]!=-1)
ans=max(ans,j-tezesaylar[sum]);
else tezesaylar[sum]=j;

}

    }

}


#include <bits/stdc++.h>

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) int((a).size())
#ifdef _WIN32
#  define I64 "%I64d"
#else
#  define I64 "%lld"
#endif
#define fname "."
#define pi pair < ll, ll >

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = (int)2e5 + 123;
const ll inf = (ll)1e18 + 123;

using namespace std;

int n;
int a[MAX_N];
ll d[MAX_N];

struct tree {
    ll x[2], lazy[2];
    int tl, tr;
    tree() {
        x[0] = x[1] = lazy[0] = lazy[1] = tl = tr = 0;
    }
    tree operator + (const tree &b) {
        int id = (tr - tl + 1) % 2;
        tree res;
        res.x[0] = min(x[0], b.x[id]), res.x[1] = min(x[1], b.x[id ^ 1]);
        res.tl = tl, res.tr = b.tr;
        return res;
    }
} t[4 * MAX_N];

void build(int v = 1, int tl = 0, int tr = n - 1) {
    if (tl == tr) {
        t[v].x[0] = d[tl], t[v].x[1] = inf;
        t[v].tl = tl, t[v].tr = tr;
        return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm), build(v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
}

void push(int v) {
    for (int i = 0; i < 2; i++)
        t[v].x[i] = min(0ll + inf, t[v].x[i] + 0ll + t[v].lazy[i]);
    if (t[v].tl != t[v].tr) {
        int tm = (t[v].tl + t[v].tr) / 2;
        for (int i = 0; i < 2; i++)
            t[v * 2].lazy[i] += t[v].lazy[i];
        int id = (tm - t[v].tl + 1) % 2;
        t[v * 2 + 1].lazy[0] += t[v].lazy[id];
        t[v * 2 + 1].lazy[1] += t[v].lazy[id ^ 1];
    }
    for (int i = 0; i < 2; i++)
        t[v].lazy[i] = 0;
}

int update(int l, int r, pi val, int v = 1, int tl = 0, int tr = n - 1) {
    push(v);
    if (tl > r || l > tr)
        return 0;
    if (tl >= l && tr <= r) {
        t[v].lazy[0] += val.f, t[v].lazy[1] += val.s;
        return tr - tl + 1;
    }
    int tm = (tl + tr) / 2;
    int len = update(l, r, val, v * 2, tl, tm);
    if (len % 2 == 1)
        swap(val.f, val.s);
    len += update(l, r, val, v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
    return len;
}

pair < int, pi > get(int l, int r, int v = 1, int tl = 0, int tr = n - 1) {
    push(v);
    if (tl > r || l > tr)
        return mp(0, mp(inf, inf));
    if (tl >= l && tr <= r)
        return mp(tr - tl + 1, mp(t[v].x[0], t[v].x[1]));
    int tm = (tl + tr) / 2;
    pair < int, pi > L = get(l, r, v * 2, tl, tm);
    pair < int, pi > R = get(l, r, v * 2 + 1, tm + 1, tr);
    if (L.f % 2 == 1)
        swap(R.s.f, R.s.s);
    return mp(L.f + R.f, mp(min(L.s.f, R.s.f), min(L.s.s, R.s.s)));
}

bool check(int l, int r) {
/*    ll x = (l > 0 ? get(l - 1, l - 1).s.f : 0);
    for (int i = l, it = 1; i <= r; i++, it ^= 1) {
        ll now = get(i, i).s.f + (it ? x : -x);
        if (now < it)
            return 0;
        if (i == r && now != it)
            return 0;
    }
    return 1;*/

    ll x = (l > 0 ? get(l - 1, l - 1).s.f : 0);
    pi now = get(l, r).s;
    if (now.f != inf)
        now.f += x;
    if (now.s != inf)
        now.s -= x;
    if (now.f < 1 || now.s < 0)
        return 0;
    ll last = get(r, r).s.f + ((r - l + 1) % 2 == 1 ? x : -x);
    if (last != (r - l + 1) % 2)
        return 0;
    return 1;*/
}

void add(int l, int r, int x) {
    update(l, r, mp(x, 0));
    if ((r - l) % 2 == 0 && r + 1 < n)
        update(r + 1, n - 1, mp(-x, x));
}

int main() {
	#ifdef Nick
	freopen(fname"in","r",stdin);
	#endif
	ios_base :: sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
    for (int i = 0; i < n; i++) {
        d[i] = a[i] - (i > 0 ? d[i - 1] : 0);
    }
    build();
	int q;
	cin >> q;
	for (int i = 0, tp, l, r, x; i < q; i++) {
		cin >> tp >> l >> r;
		if (tp == 2) {
			cout << (check(l, r) ? "1\n" : "0\n");
		}
		else {
			cin >> x;
			add(l, r, x);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define rd(x) scanf("%d",&x)
#define ls k<<1
#define rs k<<1|1
#define ll long long
#define pii pair<int,int>
#define mp make_pair
using namespace std;

const int N=200005,M=N*4;
int n,q,a[N],t,l,r,k;
struct __{
	ll p[M],s[M];
	void B(int l,int r,int k,bool t){
		if(l==r){if(((l&1)&&!t)||(!(l&1)&&t)) p[k]=min(0,a[l]-a[l-1]),s[k]=a[l]-a[l-1];return;}
		int mid=l+r>>1;
		B(l,mid,ls,t);B(mid+1,r,rs,t);
		p[k]=min(p[ls],s[ls]+p[rs]);s[k]=s[ls]+s[rs];
	}
	void C(int l,int r,int x,int y,int k){
		if(l==r){s[k]+=y;p[k]=min(0ll,s[k]);return;}
		int mid=l+r>>1;
		if(x<=mid) C(l,mid,x,y,ls); else C(mid+1,r,x,y,rs);
		p[k]=min(p[ls],s[ls]+p[rs]);s[k]=s[ls]+s[rs];
	}
	pii G(int l,int r,int x,int y,int k){
		if(l==x&&y==r) return mp(p[k],s[k]);
		int mid=l+r>>1;pii L,R;
		if(y<=mid) return G(l,mid,x,y,ls);
		if(x>mid) return G(mid+1,r,x,y,rs);
		L=G(l,mid,x,mid,ls);R=G(mid+1,r,mid+1,y,rs);
		return mp(min(L.first,L.second+R.first),L.second+R.second);
	}
	void C(int x,int y){C(1,n,x,y,1);}
	ll G(int x,int y){return G(1,n,x,y,1).first;}
	ll S(int x,int y){return G(1,n,x,y,1).second;}
};
struct _S{
	sgt p0,p1;
	ll G(int x){return p0.S(1,x)+p1.S(1,x);}
	void B(){p0.B(1,n,1,0);p1.B(1,n,1,1);}
	void C(int x,int y){if(x>n)return;else if(x&1) p0.C(x,y);else p1.C(x,y);}
	bool c(int l,int r){
		ll val=G(l);
		if(l==r){if(val>1) return 0; else return 1;}
		if(l&1){
			ll tmp=G(r);if(!(r&1)) tmp=0;
			if(p1.S(++l,r)!=tmp) return 0;
			if(p1.G(l,r)<0) return 0;
			if(p0.G(l,r)+val<0) return 0;
		}
		else{
			ll tmp=G(r);if(r&1) tmp=0;
			if(p0.S(++l,r)!=tmp) return 0;
			if(p0.G(l,r)<0) return 0;
			if(p1.G(l,r)+val<0) return 0;
		}return 1;
	}
}s;

int main(){
	rd(n);
	for(int i=1;i<=n;i++) rd(a[i]),a[i]--;
	s.B(),rd(q);
	for(int _=1;_<=q;_++){
		rd(t);rd(l);rd(r);l++,r++;
		if(t==1)rd(k),s.C(l,k),s.C(r+1,-k); else printf("%d\n",s.c(l,r));
	}
	return 0;
}


// In the Name of Allah
#include<bits/stdc++.h>
#define double long double
typedef long long ll;
const ll MAX_N = 2e5+100;
const ll MOD = 1e9+7;
const ll delta = 10333;
using namespace std;

ll mn[MAX_N*2][2];
ll lazy[MAX_N*2][2];
ll a[MAX_N];
ll b[MAX_N];
ll s[2];
int n,q,m,t;

void update(int k,int d)
{
    mn[k][d] = min(mn[k*2][d],mn[k*2+1][d]);
}

void shift(int k,int d)
{
    int l = k*2;
    int r = k*2+1;
    lazy[l][d] += lazy[k][d];
    lazy[r][d] += lazy[k][d];
    mn[l][d] += lazy[k][d];
    mn[r][d] += lazy[k][d];
    lazy[k][d] = 0;
}

void build(int l,int r,int k,int d)
{
    if (l>r)
        return;
    if (l==r)
    {
        mn[k][d] = b[l*2-d];
        return;
    }
    int mid = (l+r)/2;
    build(l,mid,k*2,d);
    build(mid+1,r,k*2+1,d);
    update(k,d);
}

void add(int l,int r,int a,int b,int k,int d,ll x)
{
    if (r<a or b<l)
        return;
    if (a<=l and r<=b)
    {
        lazy[k][d] += x;
        mn[k][d] += x;
        return;
    }
    int mid = (l+r)/2;
    shift(k,d);
    add(l,mid,a,b,k*2,d,x);
    add(mid+1,r,a,b,k*2+1,d,x);
    update(k,d);
}

ll ask(int l,int r,int a,int b,int k,int d)
{
    if (r<a or b<l)
        return 1e18;
    if (a<=l and r<=b)
        return mn[k][d];
    int mid = (l+r)/2;
    shift(k,d);
    return min(ask(l,mid,a,b,k*2,d),ask(mid+1,r,a,b,k*2+1,d));
}

int cv(int x)
{
    return (x+1)/2;
}

ll gt(int x)
{
    if (!x)
        return 0;
    ll f = ask(1,s[x&1],cv(x),cv(x),1,x&1);
    return f;
}

bool check(int l,int r)
{
    if (ask(1,s[l&1],cv(l),cv(r-(l%2!=r%2)),1,l&1)+x-1<0)
        return false;
    ll x = gt(l-1);
    ll m1 = 0;
    if (l<r)
        m1 = ask(1,s[(l+1)&1],cv(l+1),cv(r-(r%2==l%2)),1,(l+1)&1)-x;
    if (m1<0)
        return false;
    if (l%2==r%2)
    {
        if (gt(r)+x==1)
            return true;
        return false;
    }
    else
    {
        if (gt(r)==x)
            return true;
        return false;
    }
}

int main()
{
    cin >> n;
    for(int i = 1;i<=n;++i)
        scanf("%d",&a[i]);
    for(int i = 1;i<=n;++i)
        b[i] = a[i]-b[i-1];
    //zoj ha
    s[0] = n/2;
    //fard ha
    s[1] = n-s[0];
    build(1,s[0],1,0);
    build(1,s[1],1,1);
    cin >> q;
    while(q--)
    {
        int t;
        scanf("%d",&t);
        if (t==1)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            l++,r++;
            add(1,s[l&1],cv(l),cv(r-(l%2!=r%2)),1,l&1,k);
            if ((r-l+1)%2)
            {
                if (r<n)
                    add(1,s[(r+1)&1],cv(r+1),cv(n-(n%2==r%2)),1,(r+1)&1,-k);
                if (r+1<n)
                    add(1,s[r&1],cv(r+2),cv(n-(n%2!=r%2)),1,r&1,k);
            }
        }
        else
        {
            int l,r;
            scanf("%d%d",&l,&r);
            l++,r++;
            if (r==l)
            {
                printf("0\n");
                continue;
            }
            if (gt(l)+gt(l-1)==0 or gt(r)+gt(r-1)==0)
            {
                printf("0\n");
                continue;
            }
            if(check(l,r))
                printf("1\n");
            else
                printf("0\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) int((a).size())
#ifdef _WIN32
#  define I64 "%I64d"
#else
#  define I64 "%lld"
#endif
#define fname "."
#define pi pair < ll, ll >

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = (int)2e5 + 123;
const ll inf = (ll)1e18 + 123;

using namespace std;

int n;
int a[MAX_N];
ll d[MAX_N];

struct tree {
    ll x[2], lazy[2];
    int tl, tr;
    tree() {
        x[0] = x[1] = lazy[0] = lazy[1] = tl = tr = 0;
    }
    tree operator + (const tree &b) {
        int id = (tr - tl + 1) % 2;
        tree res;
        res.x[0] = min(x[0], b.x[id]), res.x[1] = min(x[1], b.x[id ^ 1]);
        res.tl = tl, res.tr = b.tr;
        return res;
    }
} t[4 * MAX_N];

void build(int v = 1, int tl = 0, int tr = n - 1) {
    if (tl == tr) {
        t[v].x[0] = d[tl], t[v].x[1] = inf;
        t[v].tl = tl, t[v].tr = tr;
        return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm), build(v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
}

void push(int v) {
    for (int i = 0; i < 2; i++)
        t[v].x[i] = min(0ll + inf, t[v].x[i] + 0ll + t[v].lazy[i]);
    if (t[v].tl != t[v].tr) {
        int tm = (t[v].tl + t[v].tr) / 2;
        for (int i = 0; i < 2; i++)
            t[v * 2].lazy[i] += t[v].lazy[i];
        int id = (tm - t[v].tl + 1) % 2;
        t[v * 2 + 1].lazy[0] += t[v].lazy[id];
        t[v * 2 + 1].lazy[1] += t[v].lazy[id ^ 1];
    }
    for (int i = 0; i < 2; i++)
        t[v].lazy[i] = 0;
}

int update(int l, int r, pi val, int v = 1, int tl = 0, int tr = n - 1) {
    push(v);
    if (tl > r || l > tr)
        return 0;
    if (tl >= l && tr <= r) {
        t[v].lazy[0] += val.f, t[v].lazy[1] += val.s;
        return tr - tl + 1;
    }
    int tm = (tl + tr) / 2;
    int len = update(l, r, val, v * 2, tl, tm);
    if (len % 2 == 1)
        swap(val.f, val.s);
    len += update(l, r, val, v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
    return len;
}

pair < int, pi > get(int l, int r, int v = 1, int tl = 0, int tr = n - 1) {
    push(v);
    if (tl > r || l > tr)
        return mp(0, mp(inf, inf));
    if (tl >= l && tr <= r)
        return mp(tr - tl + 1, mp(t[v].x[0], t[v].x[1]));
    int tm = (tl + tr) / 2;
    pair < int, pi > L = get(l, r, v * 2, tl, tm);
    pair < int, pi > R = get(l, r, v * 2 + 1, tm + 1, tr);
    if (L.f % 2 == 1)
        swap(R.s.f, R.s.s);
    return mp(L.f + R.f, mp(min(L.s.f, R.s.f), min(L.s.s, R.s.s)));
}

bool check(int l, int r) {
/*    ll x = (l > 0 ? get(l - 1, l - 1).s.f : 0);*/
    pi shit = mp(inf, inf);
    for (int i = l, it = 1; i <= r; i++, it ^= 1) {
        ll now = get(i, i).s.f + (it ? x : -x);
        shit.f = min(shit.f, now);
        shit.s = min(shit.s, now);
    }

    ll x = (l > 0 ? get(l - 1, l - 1).s.f : 0);
    pi now = get(l, r).s;
    if (now.f != inf)
        now.f += x;
    if (now.s != inf)
        now.s -= x;
    assert(now == shit);
    if (now.f < 1 || now.s < 0)
        return 0;
    ll last = get(r, r).s.f + ((r - l + 1) % 2 == 1 ? x : -x);
    if (last != (r - l + 1) % 2)
        return 0;
    return 1;
}

void add(int l, int r, int x) {
    update(l, r, mp(x, 0));
    if ((r - l) % 2 == 0 && r + 1 < n)
        update(r + 1, n - 1, mp(-x, x));
}

int main() {
	#ifdef Nick
	freopen(fname"in","r",stdin);
	#endif
	ios_base :: sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
    for (int i = 0; i < n; i++) {
        d[i] = a[i] - (i > 0 ? d[i - 1] : 0);
    }
    build();
	int q;
	cin >> q;
	for (int i = 0, tp, l, r, x; i < q; i++) {
		cin >> tp >> l >> r;
		if (tp == 2) {
			cout << (check(l, r) ? "1\n" : "0\n");
		}
		else {
			cin >> x;
			add(l, r, x);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int sol=(int) 1e9+7;
int op;

int ask(int r,int c)
{
        op++;
        if (op==lim)
        {
                cout<<"! "<<sol<<endl;
                exit(0);
        }
        cout<<"? "<<r<<" "<<c<<endl;
        int x;
        cin>>x;
        sol=min(sol,x);

        return x;
}

int go=1;

void solve(int r1,int c1,int r2,int c2)
{
        if (r1>r2 || c1>c2)
        {
                return;
        }
        if (r1==r2&&c1==c2)
        {
                ask(r1,c1);
                return;
        }
        int rm=(r1+r2)/2;
        vector<int> e;
        for (int j=max(go,c1);j<=c2;j++)
        {
                e.push_back(ask(rm,j));
        }
        int mn=e[0];
        for (auto &x : e)
                mn=min(mn,x);
        for (int j=c1;j<=c2;j++)
                if (e[j-c1]==mn)
                {
                        solve(r1,c1,rm-1,j);
                        go=max(go,j);
                        solve(rm+1,j,r2,c2);
                        return;
                }
}

int main()
{
        int n,m;
        cin>>n>>m;
        solve(1,1,n,m);
        op=4*(n+m);
        cout<<"! "<<sol<<endl;
}


// Author: Vinay Khilwani
// Language: C++

// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank

// Never Stop Trying.
// Trying to be Better than Myself.

// while(true)
// {
//     if(AC)
//     {
//         break;
//     }
//     else if(Contest Over)
//     {
//         Try.
//         Check out Editorial.
//         Understand.
//         Find out your Mistake.
//         Learn the topic (if new).
//         Solve Problems on that topic (if new).
//         Upsolve that problem.
//         break;
//     }
//     else
//     {
//         Try.
//         Use Pen-Paper.
//         Find errors, edge cases, etc.
//         continue;
//     }
// }

// Optimizations
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")

// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";

// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT nl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);

// General Functions
ll gcd(ll a, ll b)
{
    return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
    ll ans=1LL;
    while(power>0LL)
    {
        if(power%2LL==1LL)
        {
            ans=(ans*B)%modulo;
        }
        B=(B*B)%modulo;
        power/=2LL;
    }
    return ans;
}
bool isPrime(ll n)
{
    if(n<=1LL)
    {
        return false;
    }
    if(n<=3LL)
    {
        return true;
    }
    if(n%2==0LL || n%3==0LL)
    {
        return false;
    }
    for(ll i=5LL; (i*i)<=n; i+=6LL)
    {
        if(n%i==0LL || n%(i+2LL)==0LL)
        {
            return false;
        }
    }
    return true;
}
void vok()
{
    fast
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        freopen("error.txt","w",stderr);
    #endif
}

// Global Variables
const int mxN=int(1e5)+100;

// Solver Function(s)
bool comp(const ar<int,2> &a, const ar<int,2> &b)
{
    if(a[0]==b[0])
    {
        return a[1]<b[1];
    }
    return a[0]<b[0];
}
void solve()
{
    int n,m;
    IN n>>m;
    vector<ar<int,2>> v;
    loop(i,0,m)
    {
        OUT "? "<<1<<" "<<i+1<<endl;
        int inp;
        IN inp;
        v.pb({inp,i});
    }
    sort(all(v),comp);
    int ans=v[0].first;
    loop(i,1,n)
    {
        OUT "? "<<i+1<<" "<<v[0].second<<endl;
        int inp;
        IN inp;
        ans=min(ans,inp);
    }
    OUT "! "<<ans<<endl;
}

// Main Function
int main()
{
    vok();
    int t=1;
    // IN t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int sol=(int) 1e9+7;

int ask(int r,int c)
{
        int x;
        cin>>x;
        sol=min(sol,x);
        return x;
}

void solve(int r1,int c1,int r2,int c2)
{
        if (r1==r2&&c1==c2)
        {
                ask(r1,c1,r2,c2);
                return;
        }
        int rm=(r1+r2)/2;
        vector<int> e;
        for (int j=c1;j<=c2;j++)
        {
                e.push_back(ask(rm,j));
        }
        int mn=e[0];
        for (auto &x : mn)
                mn=min(mn,x);
        for (int j=c1;j<=c2;j++)
                if (e[j-c1]==mn)
                {
                        solve(r1,c1,rm-1,j);
                        solve(rm+1,j,r2,c2);
                        return;
                }
}

int main()
{
        int n,m;
        cin>>n>>m;
        solve(1,1,n,m);
}