#include <stdio.h>
#include <algorithm>
using namespace std;
int dp[205][13120];
int main()
{
	int n,k;
    scanf("%d%d",&n,&k);

    for(int i=0;i<=k;i++)
        for(int j=0;j<13120;j++)
            dp[i][j]=-999999;

    dp[0][0]=0;
    long long x;
    for(int i=0;i<n;i++)
    {
        int n2=0,n5=0;
        scanf("%I64d",&x);
        long long t=x;
        while(t%2==0) t/=2,n2++;
        while(x%5==0) x/=5,n5++;
        for(int j=k;j>=1;j--)
            for(int l=n2;l<13120;l++)
                dp[j][l]=max(dp[j][l],dp[j-1][l-n2]+n5);
    }

    int ans=0;
    for(int i=1;i<13120;i++)
        ans=max(ans,min(i,dp[k][i]));

    printf("%d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int num1[205],num2[205];
int dp[205][12805];
int judge(long long a)
{
	int sum=0;
	while(1)
	{
		if(a%2==0)
			sum++;
		a/=2;
		if(a%2==1)
			break;
	}
	return sum;
}
int work(long long a)
{
	int sum=0;
	while(1)
	{
		if(a%2==1)
			break;
		a/=2;
	}
	while(1)
	{
		if(a%5==0)
			sum++;
		if(a%5!=0)
			break;
		a/=5;
	}
	return sum;
}
int min(int a,int b)
{
	if(a<b) return a;
	return b;
}
int main()
{
	int n,k,i,j,l,sum=0,ans;
	long long a[205];
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		num1[i]=judge(a[i]);
		num2[i]=work(a[i]);
		sum+=num1[i];
	}
	for(i=1;i<=n;i++)
		for(j=k;j>=1;j--)
			for(l=num1[i];l<=sum;l++)
			{
				dp[j][l]=max(dp[j][l],dp[j-1][l-num1[i]]+num2[i]);
			}
	for(i=0;i<=sum;i++)
	{
		if(min(i,dp[k][i])>ans)
			ans=min(i,dp[k][i]);
	}
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;
int p2[205]={0};
int p5[205]={0};
int dp[205][205*32];// 选i个 5有j个，2最多几个
int main(){
//	freopen("a.txt","r",stdin);
	int n,k;
	cin>>n>>k;
	long long temp;
	for(int i=1;i<=n;i++){
		cin>>temp;
		while(temp%2==0&&temp>0){
			p2[i]++;
			temp/=2;
		}
		while(temp%5==0&&temp>0){
			p5[i]++;
			temp/=5;
		}
	}
	memset(dp,0x9f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1;i<=n;i++){// 考察第i个物品选不选
		for(int j=1;j<=k;j++){// 一共选j个
			for(int t=p5[i];t<=200*32;t++){// 一共几个5
				dp[j][t]=max(dp[j][t],dp[j-1][t-p5[i]]+p2[i]);
			}
		}
	}
	int res=0;
	for(int i=1;i<=k;i++){
		for(int t=1;t<=200*32;t++){// 一共几个5
			res=max(res,min(dp[i][t],t));
		}
	}
	cout<<res<<endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int num1[205],num2[205];
int dp[205][12805];
int min(int a,int b)
{
	if(a<b) return a;
	return b;
}
int main()
{
	int n,k,i,j,l,sum=0,ans;
	long long a[205];
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		while(a[i]%2==0)
		{
			a[i]/=2;
			num1[i]++;
		}
		while(a[i]%5==0)
		{
			a[i]/=5;
			num2[i]++;
		}
		sum+=num1[i];
	}
	for(i=1;i<=n;i++)
		for(j=k;j>=1;j--)
			for(l=sum;l>=num1[i];l--)
			{
				dp[j][l]=max(dp[j][l],dp[j-1][l-num1[i]]+num2[i]);
			}
	for(i=0;i<=sum;i++)
	{
		if(min(i,dp[k][i])>ans)
			ans=min(i,dp[k][i]);
	}
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;
int p2[205]={0};
int p5[205]={0};
int dp[205][205*32];// 选i个 5有j个，2最多几个
int main(){
//	freopen("a.txt","r",stdin);
	int n,k;
	cin>>n>>k;
	long long temp;
	for(int i=1;i<=n;i++){
		cin>>temp;
		while(temp%2==0&&temp>0){
			p2[i]++;
			temp/=2;
		}
		while(temp%5==0&&temp>0){
			p5[i]++;
			temp/=5;
		}
	}
	memset(dp,0x9f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1;i<=n;i++){// 考察第i个物品选不选
		for(int j=k;j>=1;j--){// 一共选j个
			for(int t=200*32;t>=p5[i];t--){// 一共几个5
				dp[j][t]=max(dp[j][t],dp[j-1][t-p5[i]]+p2[i]);
			}
		}
	}
	int res=0;
	for(int i=1;i<=k;i++){
		for(int t=1;t<=200*32;t++){// 一共几个5
			res=max(res,min(dp[i][t],t));
		}
	}
	cout<<res<<endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int num1[205],num2[205];
int dp[205][12805];
int min(int a,int b)
{
	if(a<b) return a;
	return b;
}
int main()
{
	int n,k,i,j,l,sum=0,ans=0;
	long long a[205];
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		while(a[i]%2==0)
		{
			a[i]/=2;
			num1[i]++;
		}
		while(a[i]%5==0)
		{
			a[i]/=5;
			num2[i]++;
		}
		sum+=num1[i];
	}
	for(i=1;i<=n;i++)
		for(j=k;j>=1;j--)
			for(l=sum;l>=num1[i];l--)
			{
				dp[j][l]=max(dp[j][l],dp[j-1][l-num1[i]]+num2[i]);
			}
	for(i=0;i<=sum;i++)
	{
		if(min(i,dp[k][i])>ans)
			ans=min(i,dp[k][i]);
	}
	printf("%d\n",ans);
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int num1[205],num2[205];
int dp[205][12805];
int min(int a,int b)
{
	if(a<b) return a;
	return b;
}
int max(int a,int b)
{
	if(a>b) return a;
	return b;
}
int main()
{
	int n,k,i,j,l,sum=0,ans=0;
	long long a[205];
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		while(a[i]%2==0)
		{
			a[i]/=2;
			num1[i]++;
		}
		while(a[i]%5==0)
		{
			a[i]/=5;
			num2[i]++;
		}
		sum+=num1[i];
	}
	memset(dp,-1,sizeof(dp));
	for(i=0;i<=n;i++)
		dp[i][0]=0;
	for(i=1;i<=n;i++)
	{
 		for(j=min(i,k);j>=1;j--)
		{
        	for(l=sum;l>=num1[i];l--)
		  	{

                if(dp[j-1][l-num1[i]]>=0)
					dp[j][l]=max(dp[j][l],dp[j-1][l-num1[i]]+num2[i]);
        	}
        }
    }
	for(i=0;i<=sum;i++)
	{
		if(min(i,dp[k][i])>ans)
			ans=min(i,dp[k][i]);
	}
	printf("%d\n",ans);
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const long long mod=1e9+7;
const double eps=1e-7;
int k,n;
long long a[maxn];
int num2[maxn],num5[maxn];
set<pair<pair<int,int> ,int> > two,five;
int main()
{
    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>a[i];
    for (int i=0;i<n;i++)
    {
        int cnt=0;
        while (a[i]%2==0)
        {
            a[i]/=2; cnt++;
        }
        num2[i]=cnt;
        cnt=0;
        while (a[i]%5==0)
        {
            a[i]/=5; cnt++;
        }
        num5[i]=cnt;
    }
    for (int i=0;i<n;i++)
    {
        two.insert(make_pair(make_pair(num2[i],num5[i]),i));
        five.insert(make_pair(make_pair(num5[i],num2[i]),i));
    }
    int twonum=0;
    int fivenum=0;
    if (k==1)
    {
        int Max=0;
        for (int i=0;i<n;i++)
        {
            Max=max(Max,min(num2[i],num5[i]));
        }
        cout<<Max<<endl;
        return 0;
    }
    int flag=0;
    if (k&1)
    {
        flag=1;
        k--;
    }
    for (int i=0;i<k;i+=2)
    {
        pair<pair<int,int> ,int> temp,t;
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end(); it--;
        temp=*(it);
        twonum+=temp.first.first;
        fivenum+=temp.first.second;
        two.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        five.erase(t);
        it=five.end(); it--;
        temp=(*it);
        twonum+=temp.first.second;
        fivenum+=temp.first.first;
        five.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        two.erase(t);
    }
    //cout<<111<<endl;
    int Max=0;
    if (flag==1)
    {
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end();
        it--;
        pair<pair<int,int> ,int> temp,t;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.first,fivenum+temp.first.second));
        it=five.end();
        it--;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.second,fivenum+temp.first.first));
    }
    else
    {
        Max=max(Max,min(twonum,fivenum));
    }
    cout<<Max<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const long long mod=1e9+7;
const double eps=1e-7;
int k,n;
long long a[maxn];
int num2[maxn],num5[maxn];
set<pair<pair<int,int> ,int> > two,five;
int num10[maxn];
int dp[210][64*210];
int main()
{
    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>a[i];
    for (int i=0;i<n;i++)
    {
        int cnt=0;
        while (a[i]%2==0)
        {
            a[i]/=2; cnt++;
        }
        num2[i]=cnt;
        cnt=0;
        while (a[i]%5==0)
        {
            a[i]/=5; cnt++;
        }
        num5[i]=cnt;
    }
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for (int q=0;q<n;q++)
    {
        for (int i=k-1;i>=0;i--)
        {
            for (int j=0;j<=6010;j++)
            {
                if (dp[i][j]!=-1)
                {
                    dp[i+1][j+num2[q]]=max(dp[i+1][j+num2[q]],dp[i][j]+num5[q]);
                }
            }
        }
    }
    int Max=0;
    for (int i=0;i<=6010;i++)
    {
        Max=max(Max,min(dp[k][i],i));
    }
    cout<<Max<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const long long mod=1e9+7;
const double eps=1e-7;
int k,n;
long long a[maxn];
int num2[maxn],num5[maxn];
set<pair<pair<int,int> ,int> > two,five;
int num10[maxn];
int main()
{
    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>a[i];
    for (int i=0;i<n;i++)
    {
        int cnt=0;
        while (a[i]%2==0)
        {
            a[i]/=2; cnt++;
        }
        num2[i]=cnt;
        cnt=0;
        while (a[i]%5==0)
        {
            a[i]/=5; cnt++;
        }
        num5[i]=cnt;
    }
    for (int i=0;i<n;i++)
    {
        two.insert(make_pair(make_pair(num2[i],num5[i]),i));
        five.insert(make_pair(make_pair(num5[i],num2[i]),i));
    }
    int twonum=0;
    int fivenum=0;
    if (k==1)
    {
        int Max=0;
        for (int i=0;i<n;i++)
        {
            Max=max(Max,min(num2[i],num5[i]));
        }
        cout<<Max<<endl;
        return 0;
    }
    int Max=0;
    int flag=0;
    if (k&1)
    {
        flag=1;
        k--;
    }
    for (int i=0;i<n;i++) num10[i]=min(num2[i],num5[i]);
    sort(num10,num10+n);
    reverse(num10,num10+n);
    for (int i=0;i<k;i++) Max+=num10[i];
    for (int i=0;i<k;i+=2)
    {
        pair<pair<int,int> ,int> temp,t;
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end(); it--;
        temp=*(it);
        twonum+=temp.first.first;
        fivenum+=temp.first.second;
        two.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        five.erase(t);
        it=five.end(); it--;
        temp=(*it);
        twonum+=temp.first.second;
        fivenum+=temp.first.first;
        five.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        two.erase(t);
    }
    //cout<<111<<endl;
    if (flag==1)
    {
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end();
        it--;
        pair<pair<int,int> ,int> temp,t;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.first,fivenum+temp.first.second));
        it=five.end();
        it--;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.second,fivenum+temp.first.first));
    }
    else
    {
        Max=max(Max,min(twonum,fivenum));
    }
    cout<<Max<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const long long mod=1e9+7;
const double eps=1e-7;
int k,n;
long long a[maxn];
int num2[maxn],num5[maxn];
int dp[210][64*210];
int main()
{
    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>a[i];
    for (int i=0;i<n;i++)
    {
        int cnt=0;
        while (a[i]%2==0)
        {
            a[i]/=2; cnt++;
        }
        num2[i]=cnt;
        cnt=0;
        while (a[i]%5==0)
        {
            a[i]/=5; cnt++;
        }
        num5[i]=cnt;
    }
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for (int q=0;q<n;q++)
    {
        for (int i=k-1;i>=0;i--)
        {
            for (int j=0;j<=64*200;j++)
            {
                if (dp[i][j]!=-1)
                {
                    dp[i+1][j+num2[q]]=max(dp[i+1][j+num2[q]],dp[i][j]+num5[q]);
                }
            }
        }
    }
    int Max=0;
    for (int i=0;i<=64*200;i++)
    {
        Max=max(Max,min(dp[k][i],i));
    }
    cout<<Max<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const long long mod=1e9+7;
const double eps=1e-7;
int k,n;
long long a[maxn];
int num2[maxn],num5[maxn];
set<pair<pair<int,int> ,int> > two,five;
int main()
{
    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>a[i];
    for (int i=0;i<n;i++)
    {
        int cnt=0;
        while (a[i]%2==0)
        {
            a[i]/=2; cnt++;
        }
        num2[i]=cnt;
        cnt=0;
        while (a[i]%5==0)
        {
            a[i]/=5; cnt++;
        }
        num5[i]=cnt;
    }
    for (int i=0;i<n;i++)
    {
        if (num2[i]==0&&num5[i]==0) continue;
        two.insert(make_pair(make_pair(num2[i],num5[i]),i));
        five.insert(make_pair(make_pair(num5[i],num2[i]),i));
    }
    if (two.size()<k)
    {
        cout<<0<<endl;
        return 0;
    }
    int twonum=0;
    int fivenum=0;
    if (k==1)
    {
        int Max=0;
        for (int i=0;i<n;i++)
        {
            Max=max(Max,min(num2[i],num5[i]));
        }
        cout<<Max<<endl;
        return 0;
    }
    int flag=0;
    if (k&1)
    {
        flag=1;
        k--;
    }
    for (int i=0;i<k;i+=2)
    {
        pair<pair<int,int> ,int> temp,t;
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end(); it--;
        temp=*(it);
        twonum+=temp.first.first;
        fivenum+=temp.first.second;
        two.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        five.erase(t);
        it=five.end(); it--;
        temp=(*it);
        twonum+=temp.first.second;
        fivenum+=temp.first.first;
        five.erase(it);
        t=make_pair(make_pair(temp.first.second,temp.first.first),temp.second);
        two.erase(t);
    }
    //cout<<111<<endl;
    int Max=0;
    if (flag==1)
    {
        set< pair<pair<int,int> ,int> >::iterator it;
        it=two.end();
        it--;
        pair<pair<int,int> ,int> temp,t;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.first,fivenum+temp.first.second));
        it=five.end();
        it--;
        temp=*it;
        Max=max(Max,min(twonum+temp.first.second,fivenum+temp.first.first));
        cout<<Max<<endl;
    }
    else
    {
        Max=max(Max,min(twonum,fivenum));
        cout<<Max<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define debug(x) cerr << #x << ' '  << x << '\n';
using namespace std;
template <typename T > void maximize(T &a, T b) {a = max(a, b);}
const int N = 206;

int n, k;
int a[N], b[N];
int dp[206][5200];
int tmp[206][5200];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("spoj.inp", "r", stdin);
        freopen("spoj.out", "w", stdout);
    #endif
    cin >> n >> k;
    long long x;
    for (int i = 1; i <= n; i++){
        cin >> x;
        while (x % 2 == 0) {x /= 2; a[i]++;}
        while (x % 5 == 0) {x /= 5; b[i]++;}
    }

    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;

    for (int i = 0; i < n; i++){

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++) tmp[j][t] = dp[j][t];

        for (int j = 0; j < k; j++)
            for (int t = 0; t <= 5200 - b[i + 1]; t++)
            if (tmp[j][t] != -1)
            maximize(dp[j + 1][t + b[i + 1]], tmp[j][t] + a[i + 1]);

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++)
            if (tmp[j][t] != -1){
                maximize(dp[j][t], tmp[j][t]);
            }

    }

    int res = 0;
    for (int i = 0; i <= 5200; i++)
        res = max(res, min(i, dp[k][i]));
    cout << res;
}


#include<bits/stdc++.h>
#define debug(x) cerr << #x << ' '  << x << '\n';
using namespace std;
template <typename T > void maximize(T &a, T b) {a = max(a, b);}
const int N = 206;

int n, k;
int a[N], b[N];
int dp[206][5200];
int tmp[206][5200];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("spoj.inp", "r", stdin);
        freopen("spoj.out", "w", stdout);
    #endif
    cin >> n >> k;
    long long x;
    for (int i = 1; i <= n; i++){
        cin >> x;
        while (x % 2 == 0) {x /= 2; a[i]++;}
        while (x % 5 == 0) {x /= 5; b[i]++;}
    }

    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;

    for (int i = 0; i < n; i++){

        memset(tmp, -1, sizeof tmp);

        for (int j = 0; j < k; j++)
            for (int t = 0; t <= 5200 - b[i + 1]; t++)
            if (dp[j][t] != -1) maximize(tmp[j + 1][t + b[i + 1]], dp[j][t] + a[i + 1]);

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++)
            if (dp[j][t] != -1) maximize(tmp[j][t], dp[j][t]);

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++) dp[j][t] = tmp[j][t];

    }

    int res = 0;
    for (int i = 0; i <= 5200; i++)
        res = max(res, min(i, dp[k][i]));
    cout << res;
}


#include<bits/stdc++.h>
#define debug(x) cerr << #x << ' '  << x << '\n';
using namespace std;
template <typename T > void maximize(T &a, T b) {a = max(a, b);}
const int N = 206;

int n, k;
int a[N], b[N];
int dp[206][206][5201];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("spoj.inp", "r", stdin);
        freopen("spoj.out", "w", stdout);
    #endif
    cin >> n >> k;
    long long x;
    for (int i = 1; i <= n; i++){
        cin >> x;
        while (x % 2 == 0) {x /= 2; a[i]++;}
        while (x % 5 == 0) {x /= 5; b[i]++;}
    }

    memset(dp, -1, sizeof dp);
    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++)
        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++)
            if (dp[i][j][t] != -1){
                maximize(dp[i + 1][j][t], dp[i][j][t]);
                maximize(dp[i + 1][ j + 1][t + b[i + 1]], dp[i][j][t] + a[i + 1]);
            }
    int res = 0;
    for (int i = 0; i <= 5200; i++)
        res = max(res, min(i, dp[n][k][i]));
    cout << res;
}


#include<bits/stdc++.h>
#define debug(x) cerr << #x << ' '  << x << '\n';
using namespace std;
template <typename T > void maximize(T &a, T b) {a = max(a, b);}
const int N = 206;

int n, k;
int a[N], b[N];
int dp[206][5200];
int tmp[206][5200];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("spoj.inp", "r", stdin);
        freopen("spoj.out", "w", stdout);
    #endif
    cin >> n >> k;
    long long x;
    for (int i = 1; i <= n; i++){
        cin >> x;
        while (x % 2 == 0) {x /= 2; a[i]++;}
        while (x % 5 == 0) {x /= 5; b[i]++;}
    }

    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;

    for (int i = 0; i < n; i++){

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++) tmp[j][t] = dp[j][t];

        for (int j = 0; j < k; j++)
            for (int t = 0; t <= 5200 - b[i]; t++)
            if (tmp[j][t] != -1)
            maximize(dp[j + 1][t + b[i]], tmp[j][t] + a[i]);

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++)
            if (tmp[j][t] != -1){
                maximize(dp[j][t], tmp[j][t]);
            }

    }
    int res = 0;
    for (int i = 0; i <= 5200; i++)
        res = max(res, min(i, dp[k][i]));
    cout << res;
}


#include<bits/stdc++.h>
#define debug(x) cerr << #x << ' '  << x << '\n';
using namespace std;
template <typename T > void maximize(T &a, T b) {a = max(a, b);}
const int N = 206;

int n, k;
int a[N], b[N];
int dp[206][5200];
int tmp[206][5200];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("spoj.inp", "r", stdin);
        freopen("spoj.out", "w", stdout);
    #endif
    cin >> n >> k;
    long long x;
    for (int i = 1; i <= n; i++){
        cin >> x;
        while (x % 2 == 0) {x /= 2; a[i]++;}
        while (x % 5 == 0) {x /= 5; b[i]++;}
    }

    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;

    for (int i = 0; i < n; i++){

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++) tmp[j][t] = dp[j][t];

        for (int j = 0; j <= k; j++)
            for (int t = 0; t <= 5200; t++)
            if (tmp[j][t] != -1){
                maximize(dp[j][t], tmp[j][t]);
                maximize(dp[ j + 1][t + b[i + 1]], tmp[j][t] + a[i + 1]);
            }

    }
    int res = 0;
    for (int i = 0; i <= 5200; i++)
        res = max(res, min(i, dp[k][i]));
    cout << res;
}


/*

Code for problem D by cookiedoth
Generated 22 Sep 2019 at 12.57 P

█▒▒▒▒▒▒▒▒▒
10%
███▒▒▒▒▒▒▒
30%
█████▒▒▒▒▒
50%
███████▒▒▒
70%
██████████
100%

^_^
-_-
z_z

*/

#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <complex>
#include <cassert>
#include <random>
#include <cstring>
#include <numeric>
#define ll long long
#define ld long double
#define null NULL
#define all(a) a.begin(), a.end()
#define debug(a) cerr << #a << " = " << a << endl
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(a) (int)a.size()

using namespace std;

template<class T> int chkmax(T &a, T b) {
	if (b > a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class T> int chkmin(T &a, T b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class iterator> void output(iterator begin, iterator end, ostream& out = cerr) {
	while (begin != end) {
		out << (*begin) << " ";
		begin++;
	}
	out << endl;
}

template<class T> void output(T x, ostream& out = cerr) {
	output(x.begin(), x.end(), out);
}

void fast_io() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

const int mx = 1e5 + 228;
int n, m;
ll ans, out_deg[mx];
vector<vector<int> > in_edges;

signed main() {
	fast_io();
	cin >> n >> m;
	in_edges.resize(n);
	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		if (u > v) {
			swap(u, v);
		}
		out_deg[v]++;
		in_edges[u].push_back(v);
	}
	for (int i = 0; i < n; ++i) {
		ans += out_deg[i] * (ll)in_edges[i].size();
	}
	cout << ans << '\n';
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int v;
		cin >> v;
		v--;
		ans -= (ll)in_edges[v].size() * out_deg[v];
		out_deg[v] += (ll)in_edges[v].size();
		for (auto u : in_edges[v]) {
			out_deg[u]--;
			ans += out_deg[u];
			ans -= (ll)in_edges[u].size();
			in_edges[u].push_back(v);
		}
		in_edges[v].clear();
		cout << ans << '\n';
	}
}


#include <bits/stdc++.h>

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L

#define long long long int

using namespace std;

// @author: pashka

int main() {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    vector<long> s1(n);
    vector<long> s2(n);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        if (x > y) swap(x, y);
        g[x].push_back(y);
        s1[x]++;
        s2[y]++;
    }
    long res = 0;
    for (int i = 0; i < n; i++) {
        res += s1[i] * s2[i];
    }
    cout << res << "\n";
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        x--;
        for (int y : g[x]) {
            g[y].push_back(x);
            res -= s1[y] * s2[y];
            s1[y]++;
            s2[y]--;
            res += s1[y] * s2[y];
        }
        res -= s1[x] * s2[x];
        s2[x] += s1[x];
        s1[x] = 0;
        g[x].clear();
        cout << res << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=1e5+5;
int n,m,d[N],z[N],q;
vector<int>e[N];LL a;
LL C(int x){return (LL)z[x]*(d[x]-z[x]);}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		d[x]++;d[y]++;
		if (x>y) swap(x,y);
		e[x].push_back(y);z[x]++;
	}
	for (int i=1;i<=n;i++) a+=C(i);
	printf("%lld\n",a);scanf("%d",&q);
	for (int v;q--;){
		scanf("%d",&v);a-=C(v);
		for (int u,i=0;i<z[v];i++){
			u=e[v][i];a-=C(u);
			e[u].push_back(v);
			z[u]++;a+=C(u);
		}
		z[v]=0;e[v].clear();
		printf("%lld\n",a);
	}
	return 0;
}


#include<bits/stdc++.h>
#define maxn 100005
#define bound 320
#define int long long
#define endl '\n'
using namespace std;
int arr[maxn];
vector<int> G[maxn];
int deg[maxn];
vector<int> gtrs[maxn];
inline int cal(int u) {
	return (deg[u]-(int)gtrs[u].size()) * (int)gtrs[u].size();
}
int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin >> n >> m;
	for(int i=1,u,v;i<=m;i++) {
		cin >> u >> v;
		G[u].emplace_back(v);
		G[v].emplace_back(u);
	}
	iota(arr+1,arr+1+n,1);
	int ans = 0;
	for(int u=1;u<=n;u++) {
		deg[u] = (int)G[u].size();
		for(int v : G[u])
			if(arr[v] > arr[u])
				gtrs[u].emplace_back(v);
		ans += cal(u);
	}
	cout << ans << endl;
	int q;
	cin >> q;
	for(int t=1;t<=q;t++) {
		int u;
		cin >> u;
		ans -= cal(u);
		for(int v : gtrs[u])
			if(arr[u] < arr[v]) {
				ans -= cal(v);
				gtrs[v].emplace_back(u);
				ans += cal(v);
			}
		gtrs[u].clear();
		arr[u] = n + t;
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define maxn 100005
#define bound 320
#define int long long
#define endl '\n'
using namespace std;
int arr[maxn];
vector<int> G[maxn];
int deg[maxn];
int cnt[maxn];	// for small vertex
vector<int> gtrs[maxn];		// for large vertex
inline int cal(int u) {
	return deg[u] > bound ? (deg[u]-(int)gtrs[u].size()) * (int)gtrs[u].size() : (deg[u]-cnt[u]) * cnt[u];
}
int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin >> n >> m;
	for(int i=1,u,v;i<=m;i++) {
		cin >> u >> v;
		G[u].emplace_back(v);
		G[v].emplace_back(u);
	}
	iota(arr+1,arr+1+n,1);
	int ans = 0;
	for(int u=1;u<=n;u++) {
		deg[u] = (int)G[u].size();
		if(deg[u] > bound) {
			for(int v : G[u])
				if(arr[v] > arr[u])
					gtrs[u].emplace_back(v);
		}
		else {
			for(int v : G[u])
				if(arr[v] > arr[u])
					cnt[u]++;
		}
		ans += cal(u);
	}
	cout << ans << endl;
	int q;
	cin >> q;
	for(int t=1;t<=q;t++) {
		int u;
		cin >> u;
		ans -= cal(u);
		vector<int>& tem = (deg[u] > bound ? gtrs[u] : G[u]);
		for(int v : tem)
			if(arr[u] < arr[v]) {
				ans -= cal(v);
				if(deg[v] > bound)
					gtrs[v].emplace_back(u);
				else
					cnt[v]++;
				ans += cal(v);
			}
		if(deg[u] > bound)
			gtrs[u].clear();
		else
			cnt[u] = 0;
		arr[u] = n + t;
		cout << ans << endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
int n,m,cnt,x,y,q;ll ans;
int head[N],s1[N],s2[N],bin[N];
vector<int>h[N];
struct pl{int v,nex;}e[2*N];
void add(int a,int b){cnt++;e[cnt].v=b;e[cnt].nex=head[a];head[a]=cnt;bin[a]++;}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
	for(int i=1;i<=n;i++){
		int j=head[i];
		while(j!=0){
			if(e[j].v>i) s2[i]++,h[i].push_back(e[j].v);
			else s1[i]++;
			j=e[j].nex;
		}
	}
	for(int i=1;i<=n;i++) ans+=(ll)s1[i]*s2[i];
	printf("%lld\n",ans);
	scanf("%d",&q);
	while(q--){
		scanf("%d",&x);ans-=(ll)s1[x]*s2[x];s1[x]=bin[x];s2[x]=0;
		for(int i=0;i<h[x].size();i++){
			int tmp=h[x][i];
			ans-=(ll)s1[tmp]*s2[tmp];
			s1[tmp]--;s2[tmp]++;
			ans+=(ll)s1[tmp]*s2[tmp];
			h[tmp].push_back(x);
		}
		h[x].clear();
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
int n,m,cnt,x,y,q;ll ans;
int head[N],s1[N],s2[N];
vector<int>h[N];
struct pl{int v,nex;}e[2*N];
void add(int a,int b){cnt++;e[cnt].v=b;e[cnt].nex=head[a];head[a]=cnt;}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
	for(int i=1;i<=n;i++){
		int j=head[i];
		while(j!=0){
			if(e[j].v>i) s2[i]++,h[i].push_back(e[j].v);
			else s1[i]++;
			j=e[j].nex;
		}
	}
	for(int i=1;i<=n;i++) ans+=(ll)s1[i]*s2[i];
	printf("%lld\n",ans);
	scanf("%d",&q);
	while(q--){
		scanf("%d",&x);ans-=(ll)s1[x]*s2[x];s1[x]=n-1;s2[x]=0;
		for(int i=0;i<h[x].size();i++){
			int tmp=h[x][i];
			ans-=(ll)s1[tmp]*s2[tmp];
			s1[tmp]--;s2[tmp]++;
			ans+=(ll)s1[tmp]*s2[tmp];
			h[tmp].push_back(x);
		}
		h[x].clear();
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define INF 1000000000ll
#define MAX 100005

set<int> adj[MAX], big[MAX], small[MAX];
vector<ll> val(MAX);

void init(){
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	init();
	int n, m;
	cin >> n >> m;
	for(int i = 1; i<=n; i++){
		val[i] = i;
	}
	int u, v;
	for(int i = 0; i<m; i++){
		cin >> u >> v;
		adj[u].insert(v);
		adj[v].insert(u);
	}

	for(int i = 1; i<=n; i++){
		for(auto it : adj[i]){
			if(val[it] < val[i]){
				small[i].insert(it);
			}
			else if(val[it] > val[i]){
				big[i].insert(it);
			}
		}
	}

	ll total = 0ll;
	for(int i = 1; i<=n; i++){
		total += 1ll*(big[i].size())*(small[i].size());
	}
	int q;
	cin >> q;
	cout << total << "\n";
	for(int d = 1; d<=q; d++){
		cin >> v;
		int value = d+n;
		total -= 1ll*(big[v].size())*(small[v].size());
		for(auto it : big[v]){
			total -= 1ll*(small[it].size())*(big[it].size());
			small[it].erase(v);
			big[it].insert(v);
			total += 1ll*(small[it].size())*(big[it].size());
			small[v].insert(it);
		}
		big[v].clear();
		val[v] = value;
		cout << total << "\n";
	}
	//fclose(stdout);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define INF 1000000000ll
#define MAX 100005

set<int> adj[MAX], big[MAX], small[MAX];
vector<ll> val(MAX);

void init(){
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	init();
	int n, m;
	cin >> n >> m;
	for(int i = 1; i<=n; i++){
		val[i] = i;
	}
	int u, v;
	for(int i = 0; i<m; i++){
		cin >> u >> v;
		adj[u].insert(v);
		adj[v].insert(u);
	}

	for(int i = 1; i<=n; i++){
		for(auto it : adj[i]){
			if(val[it] < val[i]){
				small[i].insert(it);
			}
			else if(val[it] > val[i]){
				big[i].insert(it);
			}
		}
	}

	ll total = 0ll;
	for(int i = 1; i<=n; i++){
		total += 1ll*(big[i].size())*(small[i].size());
	}
	int q;
	cin >> q;
	cout << total << "\n";
	for(int d = 1; d<=q; d++){
		cin >> v;
		int value = d+n;
		total -= 1ll*(big[v].size())*(small[v].size());
		for(auto it : big[v]){
			total -= 1ll*(small[it].size())*(big[it].size());
			small[it].erase(v);
			big[it].insert(v);
			total += 1ll*(small[it].size())*(big[it].size());
		}
		big[v].clear();
		small[v] = adj[v];
		val[v] = value;
		cout << total << "\n";
	}
	//fclose(stdout);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define INF 1000000000ll
#define MAX 100005

vector<int> adj[MAX], big[MAX], small[MAX], small_cnt(MAX, 0);
vector<ll> val(MAX);

void init(){
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	init();
	int n, m;
	cin >> n >> m;
	for(int i = 1; i<=n; i++){
		val[i] = i;
	}
	int u, v;
	for(int i = 0; i<m; i++){
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	for(int i = 1; i<=n; i++){
		for(auto it : adj[i]){
			if(val[it] < val[i]){
				small[i].pb(it);
			}
			else if(val[it] > val[i]){
				big[i].pb(it);
			}
		}
	}

	ll total = 0ll;
	for(int i = 1; i<=n; i++){
		total += 1ll*(big[i].size())*(small[i].size());
		small_cnt[i] = small[i].size();
	}
	int q;
	cin >> q;
	cout << total << "\n";
	for(int d = 1; d<=q; d++){
		cin >> v;
		int value = d+n;
		total -= 1ll*(big[v].size())*small_cnt[v];
		for(auto it : big[v]){
			total -= 1ll*(small_cnt[it])*(big[it].size());
			small_cnt[it]--;
			big[it].pb(v);
			total += 1ll*(small_cnt[it])*(big[it].size());
			small_cnt[v]++;
		}
		big[v].clear();
		val[v] = value;
		cout << total << "\n";
	}
	//fclose(stdout);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define lim 100005
#define nfs_mw ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

int main()
{
	nfs_mw;
	int i,j,n,m,a,b,q,v;
	cin>>n>>m;
	ll ans = 0;
	vector<int> in[n+1];
	vector<ll> out_deg(n+1,0);
	for(i=0; i<m; i++){
		cin>>a>>b;
		if(a>b) swap(a,b);
		in[a].pb(b);
		out_deg[b]++;
	}

	for(i=1; i<=n; i++)
		ans += out_deg[i]*in[i].size();

	cout<<ans<<"\n";

	cin>>q;
	while (q--) {
		cin>>v;
		for (int u : in[v])	{
			ans += out_deg[u] - in[u].size() - 1;
			out_deg[u]--;
			in[u].pb(v);
		}
		ans -= in[v].size() * out_deg[v];
		out_deg[v] += in[v].size();
		in[v].clear();
		cout<<ans<<"\n";
	}
}


/**
 * Niyaz Nigmatullin
 */

#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> deg(n);
	vector<set<int>> edges(n);
	for (int i = 0; i < m; i++) {
		int v, u;
		cin >> v >> u;
		--v;
		--u;
		if (v > u) swap(v, u);
		edges[v].insert(u);
		deg[v]++;
		deg[u]++;
	}
	long long all = 0;
	long long have = 0;
	auto get = [&](int v) {
		int x = (int) edges[v].size();
		int y = deg[v] - x;
		return ((long long) x * (x - 1) + (long long) y * (y - 1)) >> 1;
	};
	for (int i = 0; i < n; i++) {
		all += (long long) deg[i] * (deg[i] - 1) / 2;
		have += get(i);
	}
	int q;
	cin >> q;
	cout << all - have << '\n';
	for (int i = 0; i < q; i++) {
		int v;
		cin >> v;
		--v;
		have -= get(v);
		for (int u : edges[v]) {
			have -= get(u);
			edges[u].insert(v);
			have += get(u);
		}
		edges[v].clear();
		have += get(v);
		cout << all - have << '\n';
	}
}


/**
 * Niyaz Nigmatullin
 */

#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> deg(n);
	vector<vector<int>> edges(n);
	for (int i = 0; i < m; i++) {
		int v, u;
		cin >> v >> u;
		--v;
		--u;
		if (v > u) swap(v, u);
		edges[v].push_back(u);
		deg[v]++;
		deg[u]++;
	}
	long long all = 0;
	long long have = 0;
	auto get = [&](int v) {
		int x = (int) edges[v].size();
		int y = deg[v] - x;
		return ((long long) x * (x - 1) + (long long) y * (y - 1)) >> 1;
	};
	for (int i = 0; i < n; i++) {
		all += (long long) deg[i] * (deg[i] - 1) / 2;
		have += get(i);
	}
	int q;
	cin >> q;
	cout << all - have << '\n';
	for (int i = 0; i < q; i++) {
		int v;
		cin >> v;
		--v;
		have -= get(v);
		for (int u : edges[v]) {
			have -= get(u);
			edges[u].push_back(v);
			have += get(u);
		}
		edges[v].clear();
		have += get(v);
		cout << all - have << '\n';
	}
}


/* ****devACE**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005
vector<ll> adj[N];
set<ll> bigs[N];
set<ll> small[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		ll n,m,i,j,k;
		cin>>n>>m;
		for(i=0;i<m;i++){
			ll x,y;
			cin>>x>>y;
			if(x<y)swap(x,y);
			small[x].insert(y);
			bigs[y].insert(x);
		}

		ll ans=0,q;
		for(i=1;i<=n;i++)ans+=(sz(small[i])*sz(bigs[i]));
		cin>>q;

		ll v[q];
		cout<<ans<<"\n";
		while(q--){
			ll num;
			cin>>num;
			ans-=(sz(bigs[num])*sz(small[num]));
			for(auto i:bigs[num]){
				ans-=sz(bigs[i]);
				ans+=(sz(small[i])-1);
				small[i].erase(num);
				bigs[i].insert(num);
			}

			for(auto i:bigs[num])small[num].insert(i);
			bigs[num].clear();

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


/* ****devACE**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005
vector<ll> adj[N];
vector<ll> bigs[N];
ll tot[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		ll n,m,i,j,k;
		cin>>n>>m;
		for(i=0;i<m;i++){
			ll x,y;
			cin>>x>>y;
			if(x<y)swap(x,y);
			tot[x]++,tot[y]++;
			bigs[y].pb(x);
		}

		ll ans=0,q;
		for(i=1;i<=n;i++)ans+=((tot[i]-sz(bigs[i]))*sz(bigs[i]));
		cin>>q;

		cout<<ans<<"\n";
		while(q--){
			ll num;
			cin>>num;
			ans-=((tot[num]-sz(bigs[num]))*sz(bigs[num]));

			while(!bigs[num].empty()){
				ll x = bigs[num].back();
				ans-=(sz(bigs[x]));
				bigs[x].pb(num);
				ans+=(tot[x]-sz(bigs[x]));
				bigs[num].pop_back();
			}

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


/* ****devACE**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005
vector<ll> adj[N];
unordered_set<ll> bigs[N];
unordered_set<ll> small[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		ll n,m,i,j,k;
		cin>>n>>m;
		for(i=0;i<m;i++){
			ll x,y;
			cin>>x>>y;
			if(x<y)swap(x,y);
			small[x].insert(y);
			bigs[y].insert(x);
		}

		ll ans=0,q;
		for(i=1;i<=n;i++)ans+=(sz(small[i])*sz(bigs[i]));
		cin>>q;

		cout<<ans<<"\n";
		while(q--){
			ll num;
			cin>>num;
			ans-=(sz(bigs[num])*sz(small[num]));
			for(auto i:bigs[num]){
				ans-=sz(bigs[i]);
				ans+=(sz(small[i])-1);
				small[i].erase(num);
				bigs[i].insert(num);
			}

			for(auto i:bigs[num])small[num].insert(i);
			bigs[num].clear();

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


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

vector<int > g[N];
int c[N], z[N], w[N], p[N];
main()
{
	int n, m, x, y;
	cin >> n >> m;
	for(int i = 1;i <= m; i++){
		scanf("%d%d", &x, &y);
		if(x < y){
			swap(x, y);
		}
		g[y].pb(x);
		p[x]++;
	}
	int ans = 0;
	for(int i = 2;i <= n; i++){
		ans += p[i] * int(g[i].size());
	}
	cout << ans << endl;
	int q;
	scanf("%d", &q);
	while(q--){
		scanf("%d", &x);
		ans -= p[x] * int(g[x].size());
		for(int i = 0; i < g[x].size();i++){
			int to = g[x][i];
			ans -= g[to].size();
			g[to].pb(x);
			p[to]--;
			ans += p[to];
		}
		p[x] += int(g[x].size());
		g[x].clear();
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

vector<int > g[N];
int c[N], z[N], w[N], p[N];
main()
{
	int n, m, x, y;
	cin >> n >> m;
	for(int i = 1;i <= m; i++){
		scanf("%d%d", &x, &y);
		if(x < y){
			swap(x, y);
		}
		g[y].pb(x);
		p[x]++;
	}
	long long ans = 0;
	for(int i = 2;i <= n; i++){
		ans += p[i] * int(g[i].size());
	}
	cout << ans << endl;
	int q;
	scanf("%d", &q);
	while(q--){
		scanf("%d", &x);
		ans -= p[x] * int(g[x].size());
		for(int i = 0; i < g[x].size();i++){
			int to = g[x][i];
			ans -= g[to].size();
			g[to].pb(x);
			p[to]--;
			ans += p[to];
		}
		p[x] += int(g[x].size());
		g[x].clear();
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long

using namespace std;

const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

vector<int > g[N];
long long c[N], z[N], w[N], p[N];
main()
{
	int n, m, x, y;
	cin >> n >> m;
	for(int i = 1;i <= m; i++){
		scanf("%lld%lld", &x, &y);
		if(x < y){
			swap(x, y);
		}
		g[y].pb(x);
		p[x]++;
	}
	long long ans = 0;
	for(int i = 2;i <= n; i++){
		ans += p[i] * g[i].size();
	}
	cout << ans << endl;
	int q;
	scanf("%lld", &q);
	while(q--){
		scanf("%lld", &x);
		ans -= p[x] * g[x].size();
		for(int i = 0; i < g[x].size();i++){
			int to = g[x][i];
			ans -= g[to].size();
			g[to].pb(x);
			p[to]--;
			ans += p[to];
		}
		p[x] += g[x].size();
		g[x].clear();
		cout << ans << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define F first
#define S second
#define pb emplace_back
#define mt make_tuple
#define gcd __gcd
#define PI 3.141592653589
// Input
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define llin(a) cin >> a
#define inl(a) scanf("%lld",&a)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)in(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
// Output
#define pr(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d\n",a,b)
#define pr3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out(a) printf("%d\n",a)
#define outl(a) printf("%lld\n",a)
#define llpr(a) cout << a << " "
#define llout(a) cout << a << "\n"
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
// Iterator
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define all(vec) vec.begin(),vec.end()
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
// Debug
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define what(x) cerr << #x << " : " << x << "\n"
#define ck printf("continue\n")
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cerr << *it << " = " << a << "\n";
	err(++it, args...);
}
// Data Type
#define ll long long int
#define ii pair<int,int>
#define pli pair<ll,int>
#define triple tuple<int,int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
#define M 1000000007
#define N 100005  //check the limits, man
vvi ad(N);
vii vp(N);
ll ans;
void decrease(int x)
{
    ll temp;
    int a,b;
    tie(a,b)=vp[x];
    temp=a,temp*=b;
    ans-=temp;
    a++,b--;
    temp=a,temp*=b;
    ans+=temp;
    vp[x]={a,b};
}
void update(int x)
{
    ll temp;
    temp=vp[x].F,temp*=vp[x].S;
    ans-=temp;
    temp=vp[x].F+vp[x].S;
    vp[x]={0,temp};
}
int main()
{
    int n,m,i,a,b,q;
    in2(n,m);
    while(m--)
    {
        in2(a,b);
        a--,b--;
        if(a>b)
            swap(a,b);
        ad[a].pb(b);
        vp[a].F++,vp[b].S++;
    }
    ll temp;
    rep(i,n)
        temp=vp[i].F,temp*=vp[i].S,ans+=temp;
    in(q);
    llout(ans);
    while(q--)
    {
        in(a);
        a--;
        m=ad[a].size();
        lp(i,0,m)
        {
            b=ad[a][i];
            ad[b].pb(a);
            decrease(b);
        }
        ad[a].clear();
        update(a);
        llout(ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define F first
#define S second
#define pb emplace_back
#define mt make_tuple
#define gcd __gcd
#define PI 3.141592653589
// Input
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define llin(a) cin >> a
#define inl(a) scanf("%lld",&a)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)in(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
// Output
#define pr(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d\n",a,b)
#define pr3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out(a) printf("%d\n",a)
#define outl(a) printf("%lld\n",a)
#define llpr(a) cout << a << " "
#define llout(a) cout << a << "\n"
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
// Iterator
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define all(vec) vec.begin(),vec.end()
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
// Debug
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define what(x) cerr << #x << " : " << x << "\n"
#define ck printf("continue\n")
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cerr << *it << " = " << a << "\n";
	err(++it, args...);
}
// Data Type
#define ll long long int
#define ii pair<int,int>
#define pli pair<ll,int>
#define triple tuple<int,int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
#define M 1000000007
#define N 100005  //check the limits, man
vvi ad(N);
vii vp(N);
int ptr[N];
ll ans;
void decrease(int x)
{
    ll temp;
    int a,b;
    tie(a,b)=vp[x];
    temp=a,temp*=b;
    ans-=temp;
    a++,b--;
    temp=a,temp*=b;
    ans+=temp;
    vp[x]={a,b};
}
void update(int x)
{
    ll temp;
    temp=vp[x].F,temp*=vp[x].S;
    ans-=temp;
    temp=vp[x].F+vp[x].S;
    vp[x]={0,temp};
}
int main()
{
    int n,m,i,a,b,q;
    in2(n,m);
    while(m--)
    {
        in2(a,b);
        a--,b--;
        if(a>b)
            swap(a,b);
        ad[a].pb(b);
        vp[a].F++,vp[b].S++;
    }
    ll temp;
    rep(i,n)
        temp=vp[i].F,temp*=vp[i].S,ans+=temp;
    in(q);
    llout(ans);
    while(q--)
    {
        in(a);
        a--;
        lp(i,ptr[a],ad[a].size())
        {
            b=ad[a][i];
            ad[b].pb(a);
            decrease(b);
        }
        ptr[a]=ad[a].size();
        update(a);
        llout(ans);
    }
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=1e5+5;
int n,m,d[N],z[N],q;
vector<int>e[N];
LL a;
LL C(int x)
{
    return (LL)z[x]*(d[x]-z[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1,x,y; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        d[x]++;
        d[y]++;
        if (x>y)
            swap(x,y);
        e[x].push_back(y);
        z[x]++;
    }
    for (int i=1; i<=n; i++)
        a+=C(i);
    printf("%lld\n",a);
    scanf("%d",&q);
    for (int v; q--;)
    {
        scanf("%d",&v);
        a-=C(v);
        for (int u,i=0; i<z[v]; i++)
        {
            u=e[v][i];
            a-=C(u);
            e[u].push_back(v);
            z[u]++;
            a+=C(u);
        }
        z[v]=0;
        e[v].clear();
        printf("%lld\n",a);
    }
    return 0;
}


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

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)

#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;

const int MAX_N = 100100;
int n, m;
set<int> to[MAX_N];
set<int> frm[MAX_N];

ll getVal(int u) {
    return (ll)sz(to[u]) * (ll)sz(frm[u]);
}

int main() { FAST
    cin >> n >> m;
    M00(i, m) {
        int u, v; cin >> u >> v;
        u--; v--;
        if(u > v) swap(u, v);
        to[v].insert(u);
        frm[u].insert(v);
    }
    int q; cin >> q;
    ll cur = 0;
    M00(i, n) cur += getVal(i);
    cout << cur << "\n";
    M00(i, q) {
        int u; cin >> u; u--;
        cur -= getVal(u);
        vi r;
        for(int v: frm[u]) {
            cur -= getVal(v);
            to[v].erase(u);
            frm[v].insert(u);
            cur += getVal(v);

            r.pb(v);
        }
        for(int v: r) {
            frm[u].erase(v);
            to[u].insert(v);
        }
        cout << cur << "\n";
    }
}


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

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)

#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;

const int MAX_N = 100100;
int n, m;
int outdeg[MAX_N];
vi frm[MAX_N];

ll getVal(int u) {
    return (ll)outdeg[u] * (ll)sz(frm[u]);
}

int main() { FAST
    cin >> n >> m;
    M00(i, m) {
        int u, v; cin >> u >> v;
        u--; v--;
        if(u > v) swap(u, v);
        outdeg[v]++;
        frm[u].pb(v);
    }
    int q; cin >> q;
    ll cur = 0;
    M00(i, n) cur += getVal(i);
    cout << cur << "\n";
    M00(i, q) {
        int u; cin >> u; u--;
        cur -= getVal(u);
        for(int v: frm[u]) {
            cur -= getVal(v);
            outdeg[v]--;
            frm[v].pb(u);
            cur += getVal(v);
        }
        outdeg[u] += sz(frm[u]);
        frm[u].clear();
        cout << cur << "\n";
    }
}


#include <iostream>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
vector<int>E[200005];
long long rd[100005],cd[100005],ls[100005];
int main(int argc, char** argv) {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		if(u<v)
		{
		rd[u]++;
		cd[v]++;
		E[u].push_back(v);
		}
		else
		{
			rd[v]++;
			cd[u]++;
			E[v].push_back(u);
		}
	}
	long long now=0;
	for(int i=1;i<=n;i++)
	{
		ls[i]=rd[i]*cd[i];
		now+=ls[i];
	}
	printf("%lld\n",now);
	int q;

	cin>>q;
	while(q--)
	{
		int u;
		scanf("%d",&u);
		for(int i=0;i<E[u].size();i++)
		{
			int v=E[u][i];
			rd[u]--;
			cd[u]++;
			rd[v]++;
			cd[v]--;
			now+=rd[v]*cd[v]-ls[v];
			ls[v]=rd[v]*cd[v];
			E[v].push_back(u);
		}
		E[u].clear();
		now+=rd[u]*cd[u]-ls[u];
		ls[u]=rd[u]*cd[u];
		printf("%lld\n",now);
	}

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
vector<int>E[200005];
int rd[100005],cd[100005],ls[100005];
int main(int argc, char** argv) {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		if(u<v)
		{
		rd[u]++;
		cd[v]++;
		E[u].push_back(v);
		}
		else
		{
			rd[v]++;
			cd[u]++;
			E[v].push_back(u);
		}
	}
	long long now=0;
	for(int i=1;i<=n;i++)
	{
		ls[i]=rd[i]*cd[i];
		now+=ls[i];
	}
	printf("%lld\n",now);
	int q;

	cin>>q;
	while(q--)
	{
		int u;
		scanf("%d",&u);
		for(int i=0;i<E[u].size();i++)
		{
			int v=E[u][i];
			rd[u]--;
			cd[u]++;
			rd[v]++;
			cd[v]--;
			now+=rd[v]*cd[v]-ls[v];
			ls[v]=rd[v]*cd[v];
			E[v].push_back(u);
		}
		E[u].clear();
		now+=rd[u]*cd[u]-ls[u];
		ls[u]=rd[u]*cd[u];
		printf("%lld\n",now);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for(int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

const int N = 1e5;

vi g[N];
int in[N], out[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    int u, v;
    forn(i, m) {
        cin >> u >> v;
        u--, v--;
        if (u < v) swap(u, v);
        g[v].pb(u);
        out[u]++, in[v]++;
    }
    ll ans = 0;
    forn(i, n) ans += out[i] * 1ll * in[i];
    cout << ans << "\n";
    int q;
    cin >> q;
    while (q--) {
        cin >> v;
        v--;
        ans -= out[v] * 1ll * in[v];
        out[v] += in[v], in[v] = 0;
        while (!g[v].empty()) {
            u = g[v].back();
            g[v].pop_back();
            ans -= out[u] * 1ll * in[u];
            out[u]--, in[u]++;
            ans += out[u] * 1ll * in[u];
            g[u].pb(v);
        }
        cout << ans << "\n";
    }
    return 0;
}


#ifndef HOME
#pragma GCC optimize "-O3"
#endif

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

const int MX = 100 * 1000 + 7;
const int SQRT = 307;

vector<int> out[MX];
int indeg[MX], outdeg[MX];
int sal[MX];

ll f(int v) {
    return 1ll * indeg[v] * outdeg[v];
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        sal[i] = i;
    }
    // u --> v <=> sal[u] < sal[v]
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        if (u > v) {
            swap(u, v);
        }
        outdeg[u]++;
        indeg[v]++;
        out[u].push_back(v);
    }
    vector<int> all;
    for (int i = 1; i <= n; i++) {
        all.push_back(i);
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += f(i);
    }
    cout << ans << "\n";
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++) {
        // if (i % SQRT == 0) {
        //     for (int v = 1; v <= n; v++) {
        //         vector<int> real;
        //         for (int u : out[v]) {
        //             if (sal[u] > sal[v]) {
        //                 real.push_back(u);
        //             }
        //         }
        //         out[v] = real;
        //     }
        // }
        int v;
        cin >> v;
        ans -= f(v);
        for (int u : out[v]) {
            if (sal[u] > sal[v]) {
                ans -= f(u);
                outdeg[v]--;
                indeg[v]++;
                outdeg[u]++;
                indeg[u]--;
                out[u].push_back(v);
                ans += f(u);
            }
        }
        sal[v] = n + i;
        out[v].clear();
        cout << ans << "\n";
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 100001;
vector<int> g[N];
ll cc[N], cl[N], cr[N], used[N], x[N];

int main() {
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, m, u, v, q;
    cin >> n >> m;
    rep(i, m) {
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin >> q;
    ll ca = 0;
    rep(i, n) {
        x[i] = i;
        for (int u : g[i]) {
            if (u < i)
                cl[i]++;
            else
                cr[i]++;
        }
        ca += cl[i] * cr[i];
    }
    cout << ca << '\n';
    rep(i, q) {
        cin >> v;
        v--;
        if (used[v]) {
            cout << ca << '\n';
            continue;
        }
        used[v] = 1;
        ca -= cl[v] * cr[v];
        for (int j : g[v])
            if (x[j] > v && x[j] < n + v) {
                ca += cl[j] - 1 - cr[j];
                cl[j]--;
                cr[j]++;
                cl[v]++;
                cr[v]--;
            }
        x[v] = n + v;
        cout << ca << '\n';
    }
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 100001, R = 300;
vector<int> g[N];
unordered_set<int> qq[N];
ll cc[N], cl[N], cr[N], used[N], x[N];
int ql[N], qc[N];

int main() {
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, m, u, v, q;
    cin >> n >> m;
    rep(i, m) {
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin >> q;
    rep(i, q) {
        cin >> ql[i];
        ql[i]--;
        qc[ql[i]]++;
    }
    ll ca = 0;
    rep(i, n) {
        x[i] = i;
        for (int u : g[i]) {
            if (u < i) {
                cl[i]++;
                if (qc[u] >= R)
                    qq[u].insert(i);
            }
            else
                cr[i]++;
        }
        ca += cl[i] * cr[i];
    }
    cout << ca << '\n';
    rep(i, q) {
        v = ql[i];
        ca -= cl[v] * cr[v];
        int nx = n + i;
        if (qc[v] < R) {
            for (int j : g[v])
                if (x[j] > x[v] && x[j] < nx) {
                    ca += cl[j] - 1 - cr[j];
                    cl[j]--;
                    cr[j]++;
                    cl[v]++;
                    cr[v]--;
                    if (qc[j] >= R)
                        qq[j].insert(v);
                }
        }
        else {
            for (int j : qq[v])
                if (x[j] > x[v] && x[j] < nx) {
                    ca += cl[j] - 1 - cr[j];
                    cl[j]--;
                    cr[j]++;
                    cl[v]++;
                    cr[v]--;
                    if (qc[j] >= R)
                        qq[j].insert(v);
                }
            qq[v].clear();
        }
        x[v] = nx;
        cout << ca << '\n';
    }
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 100001;
vector<int> g[N];
ll cc[N], cl[N], cr[N], used[N], x[N];

int main() {
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, m, u, v, q;
    cin >> n >> m;
    rep(i, m) {
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin >> q;
    ll ca = 0;
    rep(i, n) {
        x[i] = i;
        for (int u : g[i]) {
            if (u < i)
                cl[i]++;
            else
                cr[i]++;
        }
        ca += cl[i] * cr[i];
    }
    cout << ca << '\n';
    rep(i, q) {
        cin >> v;
        v--;
        if (used[v]) {
            cout << ca << '\n';
            continue;
        }
        used[v] = 1;
        ca -= cl[v] * cr[v];
        int nx = n + i;
        for (int j : g[v])
            if (x[j] > x[v] && x[j] < nx) {
                ca += cl[j] - 1 - cr[j];
                cl[j]--;
                cr[j]++;
                cl[v]++;
                cr[v]--;
            }
        x[v] = nx;
        cout << ca << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m;
	cin >> n >> m;
	vector<ll> ind(n, 0);
	vector<ll> outd(n, 0);
	vector<int> g[n];
	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		a--, b--;
		if (a < b)
			swap(a, b);
		g[b].push_back(a);
		ind[b]++;
		outd[a]++;
	}
	ll ans = 0;
	for (int i = 0; i < n; i++)
		ans += ind[i] * outd[i];
	cout << ans << endl;
	int q;
	cin >> q;
	while (q--)
	{
		int v;
		cin >> v;
		v--;
		ans -= outd[v] * ind[v];
		outd[v] += ind[v];
		ind[v] = 0;
		for (auto e : g[v])
		{
			g[e].push_back(v);
			ans += -ind[e] + outd[e] - 1;
			ind[e]++;
			outd[e]--;
		}
		g[v].clear();
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define D(x) cout<<#x<<"="<<x<<endl
using namespace std;
typedef long long ll;
int d[200005];
vector<int> a[200005];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		a[min(x,y)].push_back(max(x,y));
		d[x]++,d[y]++;
	}
	ll ans=0;
	for (int i=1;i<=n;i++)
		ans+=a[i].size()*(d[i]-a[i].size());
	printf("%lld\n",ans);
	int T;
	scanf("%d",&T);
	for (int i=n+1;i<=n+T;i++)
	{
		int x;
		scanf("%d",&x);
		ans-=a[x].size()*(d[x]-a[x].size());
		for (int j=0;j<a[x].size();j++)
		{
			int y=a[x][j];
			ans-=a[y].size()*(d[y]-a[y].size());
			a[y].push_back(x);
			ans+=a[y].size()*(d[y]-a[y].size());
		}
		a[x].clear();
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define fr first
#define sc second

long long n,m,dg[100069],cd[100069];
queue<long long> al[100069];

int main()
{
	long long t,rr,i,k,l,z=0;

	scanf("%lld%lld",&n,&m);
	for(i=0;i<m;i++)
	{
		scanf("%lld%lld",&k,&l);
		if(k>l)
		{
			swap(k,l);
		}
		al[k].push(l);
		dg[k]++;
		dg[l]++;
		cd[l]++;
	}
	for(i=1;i<=n;i++)
	{
		z+=cd[i]*(dg[i]-cd[i]);
	}
	printf("%lld\n",z);
	scanf("%lld",&t);
	for(rr=1;rr<=t;rr++)
	{
		scanf("%lld",&k);
		z-=cd[k]*(dg[k]-cd[k]);
		for(;!al[k].empty();)
		{
			l=al[k].front();
			al[k].pop();
			z-=dg[l]-cd[l];
			al[l].push(k);
			cd[l]--;
			z+=cd[l];
		}
		cd[k]=dg[k];
		printf("%lld\n",z);
	}
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define gadd(x,y) x=add(x,y)
#define gmul(x,y) x=mul(x,y)
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef vector<int> vi;

const int N = 1e5 + 5;
vi g[N], in[N];

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	int n,m,q;
	cin >> n >> m;
	rep(i,0,m){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		in[min(x,y)].push_back(max(x,y));
	}
	ll ans = 0;
	rep(i,1,n+1){
		ans += 1LL * (g[i].size() - in[i].size()) * in[i].size();
	}
	cin >> q;
	cout << ans << '\n';
	while(q--){
		int u;
		cin >> u;
		ans -= 1LL * (g[u].size() - in[u].size()) * in[u].size();
		while(in[u].size()){
			int to = in[u].back();
			in[u].pop_back();
			ans -= 1LL * (g[to].size() - in[to].size()) * in[to].size();
			in[to].push_back(u);
			ans += 1LL * (g[to].size() - in[to].size()) * in[to].size();
		}
		cout << ans << '\n';
	}
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[]={2,2,-2,-2,1,1,-1,-1},ddy[]={1,-1,1,-1,2,-2,2,-2};

int n,m;
Vi v[100001];
ll ans;
ll in[100001],out[100001];
void re(int x)
{
	ans-=in[x]*out[x];
	out[x]--,in[x]++;
	ans+=in[x]*out[x];
}
void upd(int x)
{
	ans-=in[x]*out[x];
	out[x]+=in[x];
	in[x]=0;
	while(!v[x].empty())
	{
		int y=v[x].back();
		re(y);
		v[x].pop_back();
		v[y].pb(x);
	}
	printf("%lld\n",ans);
}
int main(){
	scanf("%d%d",&n,&m);
	fup(i,0,m-1,1)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if(x>y)swap(x,y);
		v[x].pb(y);
		in[x]++,out[y]++;
	}
	fup(i,1,n,1)ans+=in[i]*out[i];
	printf("%lld\n",ans);
	int q;
	scanf("%d",&q);
	while(q--)
	{
		int x;
		scanf("%d",&x);
		upd(x);
	}
}


#include <iostream> // C++ I/O
#include <string>   // C++ string
#include <fstream>  // File I/O
#include <sstream>  // String stream I/O
#include <iomanip>  // C++ I/O manipulator

#include <cstdlib> // C library
#include <cstdio>  // C I/O
#include <ctime>   // C time
#include <cmath>   // Math library
#include <cstring> // C strings

#include <vector>    // Vector
#include <queue>     // Queue
#include <stack>     // Stack
#include <map>       // Map
#include <set>       // Set
#include <bitset>    // Bitset
#include <algorithm> // Algorithms

using namespace std;

#define INF 0x3f3f3f3f
#define EPS 1e-8

typedef long long ll;
typedef unsigned long long ull;

#define memclr(_var) memset(_var, 0, sizeof(_var))
#define maximize(_var, _cur) _var = max(_var, _cur)
#define minimize(_var, _cur) _var = min(_var, _cur)
#define reps(_var, _begin, _end, _step) for (int _var = (_begin); _var <= (_end); _var += (_step))
#define reps_(_var, _end, _begin, _step) for (int _var = (_end); _var >= (_begin); _var -= (_step))
#define rep(_var, _begin, _end) reps(_var, _begin, _end, 1)
#define rep_(_var, _end, _begin) reps_(_var, _end, _begin, 1)

inline int read()
{
    char ch = getchar();
    int x = 0, f = 1;
    while (ch < '0' || ch > '9')
        f = ch == '-' ? -1 : 1, ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

const int MAXN = 100010;
vector<int> G[MAXN];
int indeg[MAXN], outdeg[MAXN];

int main(int argc, char *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = read(), m = read();

    rep (_, 1, m)
    {
        int u = read(), v = read();
        if (u < v)
            swap(u, v);
        G[u].push_back(v);
        outdeg[u]++;
        indeg[v]++;
    }

    ll res = 0;
    rep (i, 1, n)
        res += indeg[i] * outdeg[i];

    printf("%lld\n", res);

    int q = read();

    rep (_, 1, q)
    {
        int u = read();
        res -= indeg[u] * outdeg[u];

        for (auto v : G[u])
        {
            res -= indeg[v] * outdeg[v];
            indeg[v]--;
            outdeg[v]++;
            G[v].push_back(u);
            res += indeg[v] * outdeg[v];
        }

        indeg[u] += outdeg[u];
        outdeg[u] = 0;
        G[u].clear();

        printf("%lld\n", res);
    }

    return 0;
}


#include <iostream> // C++ I/O
#include <string>   // C++ string
#include <fstream>  // File I/O
#include <sstream>  // String stream I/O
#include <iomanip>  // C++ I/O manipulator

#include <cstdlib> // C library
#include <cstdio>  // C I/O
#include <ctime>   // C time
#include <cmath>   // Math library
#include <cstring> // C strings

#include <vector>    // Vector
#include <queue>     // Queue
#include <stack>     // Stack
#include <map>       // Map
#include <set>       // Set
#include <bitset>    // Bitset
#include <algorithm> // Algorithms

using namespace std;

#define INF 0x3f3f3f3f
#define EPS 1e-8

typedef long long ll;
typedef unsigned long long ull;

#define memclr(_var) memset(_var, 0, sizeof(_var))
#define maximize(_var, _cur) _var = max(_var, _cur)
#define minimize(_var, _cur) _var = min(_var, _cur)
#define reps(_var, _begin, _end, _step) for (int _var = (_begin); _var <= (_end); _var += (_step))
#define reps_(_var, _end, _begin, _step) for (int _var = (_end); _var >= (_begin); _var -= (_step))
#define rep(_var, _begin, _end) reps(_var, _begin, _end, 1)
#define rep_(_var, _end, _begin) reps_(_var, _end, _begin, 1)

inline int read()
{
    char ch = getchar();
    int x = 0, f = 1;
    while (ch < '0' || ch > '9')
        f = ch == '-' ? -1 : 1, ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

const int MAXN = 100010;
vector<int> G[MAXN];
ll indeg[MAXN], outdeg[MAXN];

int main(int argc, char *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = read(), m = read();

    rep (_, 1, m)
    {
        int u = read(), v = read();
        if (u > v)
            swap(u, v);
        G[u].push_back(v);
        outdeg[u]++;
        indeg[v]++;
    }

    ll res = 0;
    rep (i, 1, n)
        res += indeg[i] * outdeg[i];

    printf("%lld\n", res);

    int q = read();

    rep (_, 1, q)
    {
        int u = read();
        res -= indeg[u] * outdeg[u];

        for (auto v : G[u])
        {
            res -= indeg[v] * outdeg[v];
            indeg[v]--;
            outdeg[v]++;
            G[v].push_back(u);
            res += indeg[v] * outdeg[v];
        }

        indeg[u] += outdeg[u];
        outdeg[u] = 0;
        G[u].clear();

        printf("%lld\n", res);
    }

    return 0;
}


#include<bits/stdc++.h>
#define INT long long
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int ot[N],in[N];
INT ans;

main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[min(u,v)].push_back(max(v,u));
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
        ans+=(INT)in[i]*ot[i];

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=(INT)in[u]*ot[u];
        for(int v:g[u])
        {
            ans-=(INT)ot[v]*in[v];
            ot[v]--,in[v]++;
            ans+=(INT)ot[v]*in[v];
            g[v].push_back(u);
        }
        ot[u]+=in[u];in[u]=0;
        g[u].clear();
    }

    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int ot[N],in[N],ans;

main()
{

    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[min(u,v)].push_back(max(v,u));
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
        id[i]=i;
    }

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=in[u]*ot[u];
        for(int v:g[u])
        {
            ans-=ot[v]*in[v];
            ot[v]--,in[v]++;
            ans+=ot[v]*in[v];
            g[v].push_back(u);
        }
        ot[u]+=in[u];in[u]=0;
        g[u].clear();
    }

    cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int in[N],ot[N],id[N],ans;

main()
{
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
        id[i]=i;
    }

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=in[u]*ot[u];
        for(int v:g[u])
            if(id[v]>id[u])
            {
                ans-=ot[v]*in[v];
                ot[v]--;
                in[v]++;
                in[u]--;
                ot[u]++;
                ans+=ot[v]*in[v];
            }
        id[u]=n+i;
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int ot[N],id[N],ans;

main()
{
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[min(u,v)].push_back(max(v,u));
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
        id[i]=i;
    }

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=in[u]*ot[u];
        for(int v:g[u])
        {
            ans-=ot[v]*in[v];
            ot[v]--,in[v]++;
            ans+=ot[v]*in[v];
            g[v].push_back(u);
        }
        ot[u]+=in[u];in[u]=0;
        g[u].clear();
    }

    cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+7;
set<int>g[N];
int in[N],ot[N],id[N],ans;

main()
{
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[min(u,v)].insert(max(v,u));
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
        id[i]=i;
    }

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=in[u]*ot[u];
        for(int v:g[u])
        {
            ans-=ot[v]*in[v];
            ot[v]--,in[v]++;
            ans+=ot[v]*in[v];
            g[v].insert(u);
            in[u]--,ot[u]++;
        }
        g[u].clear();
        id[u]=n+i;
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int in[N],ot[N],id[N],ans;

main()
{
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
        id[i]=i;
    }

    cout<<ans<<endl;

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        int u;cin>>u;
        ans-=in[u]*ot[u];
        for(int v:g[u])
            if(id[v]>id[u])
            {
                ans-=ot[v]*in[v];
                ot[v]--;
                in[v]++;
                in[u]--;
                ot[u]++;
                ans+=ot[v]*in[v];
            }
        id[u]=n+i;
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
vector<int>g[N];
int ot[N],in[N],ans;

main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;cin>>u>>v;
        g[min(u,v)].push_back(max(v,u));
        in[min(u,v)]++;
        ot[max(u,v)]++;
    }

    for(int i=1;i<=n;i++)
    {
        ans+=in[i]*ot[i];
    }

    int q;cin>>q;
    for(int i=1;i<=q;i++)
    {
        cout<<ans<<endl;
        int u;cin>>u;
        if(in[u]==0)
            continue;
        ans-=in[u]*ot[u];
        for(int v:g[u])
        {
            ans-=ot[v]*in[v];
            ot[v]--,in[v]++;
            ans+=ot[v]*in[v];
            g[v].push_back(u);
        }
        ot[u]+=in[u];in[u]=0;
        g[u].clear();
    }

    cout<<ans<<endl;

}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define FORD(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define Debug(x) cerr << #x << " = " << x << '\n';
#define HERE cerr << "HERE\n\n";
#define X first
#define Y second
#define pb push_back
#define FASTER { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }

using namespace std;

typedef pair <int, int> ii;
typedef vector <int> vi;

int const Nmax =  1e5 + 5;
vi Adj[Nmax];
int n, m, q;
int deg[Nmax];
int val[Nmax];
int64_t Count = 0;

void Solve(){
    cin >> q;
    cout << Count << '\n';
    FOR(k, 1, q){
        int u;
        cin >> u;
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            Count -= (deg[u] - Adj[u].size());
            Count += (deg[v] - Adj[v].size() - 1);
            Count -= Adj[v].size();
        }
        Adj[u].clear();
        cout << Count << '\n';
    }
}

void Enter(){
    cin >> n >> m;
    FOR(i, 1, m){
        int x, y;
        cin >> x >> y;
        if (x > y) swap(x, y);
        Adj[x].pb(y);
        deg[x]++;
        deg[y]++;
    }
    FOR(i, 1, n) Count += (deg[i] - Adj[i].size()) * Adj[i].size();
}

int main(){
    FASTER
//    freopen("x.INP", "r", stdin);
//    freopen("x.OUT", "w", stdout);
    Enter();
    Solve();
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define FORD(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define Debug(x) cerr << #x << " = " << x << '\n';
#define HERE cerr << "HERE\n\n";
#define X first
#define Y second
#define pb push_back
#define FASTER { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }

using namespace std;

typedef pair <int, int> ii;
typedef vector <int> vi;

int const Nmax =  1e5 + 5;
vi Adj[Nmax];
int n, m, q;
int deg[Nmax];
int val[Nmax];
int64_t Count = 0;

void Solve(){
    cin >> q;
    cout << Count << '\n';
    FOR(k, 1, q){
        int u;
        cin >> u;
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            Count -= (deg[u] - Adj[u].size());
            Count += (deg[v] - Adj[v].size() - 1);
            Count -= Adj[v].size();
            Adj[v].pb(u);
        }
        Adj[u].clear();
        cout << Count << '\n';
    }
}

void Enter(){
    cin >> n >> m;
    FOR(i, 1, m){
        int x, y;
        cin >> x >> y;
        if (x > y) swap(x, y);
        Adj[x].pb(y);
        deg[x]++;
        deg[y]++;
    }
    FOR(i, 1, n) Count += (deg[i] - Adj[i].size()) * Adj[i].size();
}

int main(){
    FASTER
//    freopen("x.INP", "r", stdin);
//    freopen("x.OUT", "w", stdout);
    Enter();
    Solve();
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define FORD(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define Debug(x) cerr << #x << " = " << x << '\n';
#define HERE cerr << "HERE\n\n";
#define X first
#define Y second
#define pb push_back
#define FASTER { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }

using namespace std;

typedef pair <int, int> ii;
typedef vector <int> vi;

int const Nmax =  1e5 + 5;
vi Adj[Nmax];
int n, m, q;
int degIn[Nmax], degOut[Nmax];
int val[Nmax];
int64_t Count = 0;

void Solve(){
    cin >> q;
    cout << Count << '\n';
    FOR(k, 1, q){
        int u;
        cin >> u;
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            if (val[v] > val[u]){
                Count -= degOut[u];
                Count -= degIn[v];
                Count += degOut[v] - 1;
            }
        }
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            if (val[v] > val[u]) degOut[u]++, degOut[v]--, degIn[v]++, degIn[u]--;
        }
        val[u] = n + k;
        cout << Count << "\n";
//        FOR(i, 1, n) cerr << degOut[i] << ' ';
//        cerr << '\n';
    }
}

void Enter(){
    cin >> n >> m;
    FOR(i, 1, m){
        int x, y;
        cin >> x >> y;
        Adj[x].pb(y);
        Adj[y].pb(x);
    }
    FOR(i, 1, n) val[i] = i;
    FOR(u, 1, n){
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            if (v < u) degOut[u]++;
            else degIn[u]++;
        }
    }
    FOR(u, 1, n){
        REP(i, Adj[u].size()){
            int v = Adj[u][i];
            if (v < u) Count += degOut[v];
        }
    }
}

int main(){
    FASTER
//    freopen("x.INP", "r", stdin);
//    freopen("x.OUT", "w", stdout);
    Enter();
    Solve();
}


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

typedef long long ll;
const int N=1e5+10;
int n,m,q;
vector<int> G[N];
ll res,out[N],in[N];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        if(u>v) swap(u,v);
        in[u]++,out[v]++;
        G[u].pb(v);
    }
    for(int i=1;i<=n;i++){
        res+=(ll)out[i]*in[i];
    }
    scanf("%d",&q);
    printf("%I64d\n",res);
    for(int i=1;i<=q;i++){
        int x;
        scanf("%d",&x);
        for(int v:G[x]){
            G[v].pb(x);
            ll last=out[v]*in[v];
            in[v]++,out[v]--;
            res+=(ll)in[v]*out[v]-last;
        }
        G[x].clear();
        res-=(ll)out[x]*in[x];
        out[x]+=in[x],in[x]=0;
        printf("%I64d\n",res);
    }
    return 0;
}


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

typedef long long ll;
const int N=1e5+10;
int n,m,q,in[N],out[N];
vector<int> G[N];
ll res;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        if(u>v) swap(u,v);
        in[u]++,out[v]++;
        G[u].pb(v);
    }
    for(int i=1;i<=n;i++){
        res+=(ll)out[i]*in[i];
    }
    scanf("%d",&q);
    printf("%I64d\n",res);
    for(int i=1;i<=q;i++){
        int x;
        scanf("%d",&x);
        for(int v:G[x]){
            G[v].pb(x);
            ll last=out[v]*in[v];
            in[v]++,out[v]--;
            res+=(ll)in[v]*out[v]-last;
        }
        G[x].clear();
        res-=(ll)out[x]*in[x];
        out[x]=n-1,in[x]=0;
        printf("%I64d\n",res);
    }
    return 0;
}


#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 100005;
const int DIM = 20000;
int in[NMAX];
int out[NMAX];
int val[NMAX];
vector <int> vec[NMAX];
char buff[DIM];
int poz=0;
vector <int> g[NMAX];
vector <int> gv[NMAX];

void citeste(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

int main() {
    int n, m, i, a, b, p, qq;
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    citeste(n);
    citeste(m);
    for(i = 1; i <= m; i++) {
        citeste(a);
        citeste(b);
        vec[min(a, b)].push_back(max(a, b));
        out[min(a, b)]++;
        in[max(a, b)]++;
    }
    long long sol = 0;
    for(i = 1; i <= n; i++) {
        val[i] = i;
        sol = sol + in[i] * out[i];
    }
    printf("%I64d\n", sol);
    int q;
    citeste(q);
    for(qq = 1; qq <= q; qq++) {
        citeste(p);
        for(auto u : vec[p]) {
            if(val[u] > val[p]) {
                sol = sol - in[u] * out[u];
                in[u]--;
                out[u]++;
                sol = sol + in[u] * out[u];
                vec[u].push_back(p);
            }
        }
        sol = sol - in[p] * out[p];
        in[p] += out[p];
        out[p] = 0;
        val[p] = n + qq;
        vec[p].clear();
        printf("%I64d\n", sol);
    }
    return 0;
}


#include <vector>
#include <cstdio>
using namespace std;
const int NMAX = 100005;
const int DIM = 20000;
int in[NMAX];
int out[NMAX];
int val[NMAX];
vector <int> vec[NMAX];
char buff[DIM];
int poz=0;

void citeste(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

int main() {
    int n, m, i, a, b, p, qq;
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    citeste(n);
    citeste(m);
    for(i = 1; i <= m; i++) {
        citeste(a);
        citeste(b);
        vec[a].push_back(b);
        vec[b].push_back(a);
    }
    long long sol = 0;
    for(i = 1; i <= n; i++) {
        for(int u : vec[i]) {
            if(i < u) {
                out[i]++;
            }
            else {
                in[i]++;
            }
        }
    }
    for(i = 1; i <= n; i++) {
        val[i] = i;
        sol = sol + in[i] * out[i];
    }
    printf("%I64d\n", sol);
    int q;
    citeste(q);
    for(qq = 1; qq <= q; qq++) {
        citeste(p);
        for(auto u : vec[p]) {
            if(val[u] > val[p]) {
                sol = sol - in[u] * out[u];
                in[u]--;
                out[u]++;
                sol = sol + in[u] * out[u];
            }
        }
        sol = sol - in[p] * out[p];
        in[p] += out[p];
        out[p] = 0;
        val[p] = n + qq;
        printf("%I64d\n", sol);
    }
    return 0;
}


#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 100005;
const int DIM = 20000;
unsigned long long in[NMAX];
unsigned long long out[NMAX];
unsigned long long val[NMAX];
vector <int> vec[NMAX];
char buff[DIM];
int poz=0;
vector <int> g[NMAX];
vector <int> gv[NMAX];

void citeste(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

int main() {
    int n, m, i, a, b, p, qq;
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    citeste(n);
    citeste(m);
    for(i = 1; i <= m; i++) {
        citeste(a);
        citeste(b);
        vec[min(a, b)].push_back(max(a, b));
        out[min(a, b)]++;
        in[max(a, b)]++;
    }
    unsigned long long sol = 0;
    for(i = 1; i <= n; i++) {
        val[i] = i;
        sol = sol + in[i] * out[i];
    }
    printf("%I64d\n", sol);
    int q;
    citeste(q);
    for(qq = 1; qq <= q; qq++) {
        citeste(p);
        for(auto u : vec[p]) {
            if(val[u] > val[p]) {
                sol = sol - in[u] * out[u];
                in[u]--;
                out[u]++;
                sol = sol + in[u] * out[u];
                vec[u].push_back(p);
            }
        }
        sol = sol - in[p] * out[p];
        in[p] += out[p];
        out[p] = 0;
        val[p] = n + qq;
        vec[p].clear();
        printf("%I64d\n", sol);
    }
    return 0;
}


#include <vector>
#include <cstdio>
using namespace std;
const int NMAX = 100005;
int in[NMAX];
int out[NMAX];
int val[NMAX];
vector <int> vec[NMAX];

int main() {
    int n, m;
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        vec[a].push_back(b);
        vec[b].push_back(a);
    }
    long long sol = 0;
    for(int i = 1; i <= n; i++) {
        for(int u : vec[i]) {
            if(i < u) {
                out[i]++;
            }
            else {
                in[i]++;
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        val[i] = i;
        sol = sol + in[i] * out[i];
    }
    printf("%lld\n", sol);
    int q;
    scanf("%d", &q);
    for(int qq = 1; qq <= q; qq++) {
        int p;
        scanf("%d", &p);
        for(int u : vec[p]) {
            if(val[u] > val[p]) {
                sol = sol - in[u] * out[u];
                in[u]--;
                out[u]++;
                sol = sol + in[u] * out[u];
            }
        }
        sol = sol - in[p] * out[p];
        in[p] += out[p];
        out[p] = 0;
        val[p] = n + qq;
        printf("%lld\n", sol);
    }
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <deque>
#define ll long long
#define db double
using namespace std;
inline int read(){
    int SUM=0,ff=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
      if(ch=='-')
        ff=-1;
      ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
      SUM=(SUM<<3)+(SUM<<1)+(ch^48);
      ch=getchar();
    }
    return SUM*ff;
}
const int N=100010;
vector <int> g[N];
int in[N],out[N];
ll ans=0;
int n,m,Q;
int main(){
    //freopen("CF1210D Konrad and Company Evaluation.in","r",stdin);
    //freopen("CF1210D Konrad and Company Evaluation.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=m;i++){
	   int u=read(),v=read();
	   if(u>v) swap(u,v);
	   in[u]++;
	   out[v]++;
	   g[u].push_back(v);
	}
	for(int i=1;i<=n;i++)
	   ans+=1ll*in[i]*out[i];
	cout<<ans<<endl;
	int Q=read();
	while(Q--){
	   int u=read();
	   ans-=1ll*in[u]*out[u];
	   for(auto v:g[u]){
	      ans-=1ll*in[v]*out[v];
		  in[v]++,out[v]--;
	      ans+=1ll*in[v]*out[v];
	      in[u]--,out[u]++;
	      g[v].push_back(u);
	   }
	   g[u].clear();
	   cout<<ans<<endl;
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , m ;
int in[N],out[N];
vi g[N],p[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m;
	int u , v;
	rep(i , 1 , m + 1){
		cin>>u>>v;
		g[v].pb(u);
		p[u].pb(v);
		in[v]++;
		out[u]++;
	}
	ll ans = 0;
	rep(i , 1 , n + 1){
		ans += (ll)in[i]*out[i];
	}
	cout<<ans<<"\n";
	int q;
	cin>>q;
	while(q--){
		cin>>u;
		ans -= (ll)in[u]*out[u];
		for(auto v:g[u]){
			ans -= (ll)in[v]*out[v];
			out[v]--;
			in[v]++;
			out[u]++;
			g[v].pb(u);
			ans += (ll)in[v]*out[v];
		}
		g[u].clear();
		in[u] = 0;
		cout<<ans<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , m ;
int in[N],out[N];
vi g[N],p[N];
map<int,int> link[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m;
	int u , v;
	rep(i , 1 , m + 1){
		cin>>u>>v;
		if(u < v)swap(u,v);
		g[v].pb(u);
		in[v]++;
		out[u]++;
	}
	ll ans = 0;
	rep(i , 1 , n + 1){
		ans += (ll)in[i]*out[i];
	}

	cout<<ans<<"\n";
	int q;
	cin>>q;
	while(q--){
		cin>>u;
		ans -= (ll)in[u]*out[u];
		for(auto v:g[u]){
			ans -= (ll)in[v]*out[v];
			out[v]--;
			in[v]++;
			out[u]++;
			g[v].pb(u);
			ans += (ll)in[v]*out[v];
		}
		g[u].clear();
		in[u] = 0;
		cout<<ans<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define L long long
const int N=5e5+5;
int n,m,W[N],q;
vector <int> v[N];
L C(int x) {
	int u=v[x].size();
	return 1ll*u*(W[x]-u);
}
void Init() {
	scanf("%d%d",&n,&m);
	L S=0;
	for(int i=1,x,y;i<=m;i++) {
		scanf("%d%d",&x,&y);
		if(x>y) swap(x,y);
		v[x].push_back(y);
		W[x]++;W[y]++;
	}
	for(int i=1;i<=n;i++) S+=C(i);
	printf("%I64d\n",S);
	for(scanf("%d",&q);q--;) {
		int x,u;
		scanf("%d",&x);
		u=v[x].size();
		S-=C(x);
		for(int i=u-1;~i;i--) {
			int k=v[x][i];
			S-=C(k);
			v[k].push_back(x);
			S+=C(k);
		}
		v[x].clear();
		printf("%I64d\n",S);
	}
}
int main() {
	Init();
return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e5+5;
int n,m,d[M];ll now;
vector<int>t[M];
inline ll ans(int x){return (ll)t[x].size()*(d[x]-t[x].size());}
int main()
{
	scanf("%d%d",&n,&m);
	int x,y,T;
	for(int i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		t[min(x,y)].push_back(max(x,y));
		d[x]++,d[y]++;
	}
	for(int i=1;i<=n;i++) now+=ans(i);
	printf("%lld\n",now);
	scanf("%d",&T);
	while(T--){
		scanf("%d",&x);
		now-=ans(x);
		for(int i=0;i<t[x].size();i++){
			now-=ans(t[x][i]);
			t[t[x][i]].push_back(x);
			now+=ans(t[x][i]);
		}
		t[x].clear();
		printf("%lld\n",now);
	}
	return 0;
}


#include<bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
using namespace std;

inline ll read(){
	ll num=0,neg=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')neg=-1;c=getchar();}
	while(isdigit(c)){num=(num<<3)+(num<<1)+c-'0';c=getchar();}
	return num*neg;
}

vector<int>in[100010];
int n,m;
ll ans,deg[100010];

int main(){
	n=read(),m=read();
	for(int i=1,x,y;i<=m;i++){
		x=read(),y=read();
		if(x>y) swap(x,y);
		in[x].pb(y);
		deg[x]++,deg[y]++;
	}
	for(int i=1;i<=n;i++) ans+=in[i].size()*(deg[i]-in[i].size());
	printf("%lld\n",ans);
	int Q=read();
	while(Q--){
		int u=read();
		ans-=in[u].size()*(deg[u]-in[u].size());
		for(int i=0;i<in[u].size();i++){
			int v=in[u][i];
			ans+=deg[v]-2*in[v].size()-1;
			in[v].pb(u);
		}in[u].clear();
		printf("%lld\n",ans);
	}return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) ll(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXN = 100005;
const ll MOD = 1000000007;

int n, m, q;
ll ans, deg[MAXN];
vector<int> in[MAXN];

void upd (int nd, int add) {
    ans -= (deg[nd] - SZ(in[nd])) * SZ(in[nd]);
    in[nd].emplace_back(add);
    ans += (deg[nd] - SZ(in[nd])) * SZ(in[nd]);
}
/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> n >> m;
    REP (i, m) {
        int f, t;
        cin >> f >> t;
        if (f < t) {
            swap(f, t);
        }
        deg[f]++;
        deg[t]++;
        in[t].emplace_back(f);
    }
    REP1 (i, n) {
        ans += (deg[i] - SZ(in[i])) * SZ(in[i]);
    }

    cin >> q;
    cout << ans << endl;
    while (q--) {
        int d;
        cin >> d;
        ans -= (deg[d] - SZ(in[d])) * SZ(in[d]);
        for (auto v : in[d]) {
            upd(v, d);
        }

        cout << ans << endl;
        in[d].clear();
    }
    return 0;
}


#include <iostream>
#include <vector>

const int MAX_N = 100000;

std::vector<int> G[1 + MAX_N];
long long val[1 + MAX_N];
int in[1 + MAX_N];
int out[1 + MAX_N];
long long ans;

int main() {

  int n, m;
  std::cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    std::cin >> u >> v;
    G[std::min(u, v)].push_back(std::max(u, v));
    out[std::min(u, v)]++;
    in[std::max(u, v)]++;
  }
  for (int i = 1; i <= n; i++) {
    ans += 1LL * in[i] * out[i];
    val[i] = i;
  }
  std::cout << ans << '\n';
  int Q;
  std::cin >> Q;
  for (int q = 1; q <= Q; q++) {
    int u;
    std::cin >> u;
    for (int v : G[u])
      if (val[v] > val[u]) {
        ans -= 1LL * in[v] * out[v];
        in[v]--;
        out[v]++;
        ans += 1LL * in[v] * out[v];
        G[v].push_back(u);
      }
    ans -= 1LL * in[u] * out[u];
    in[u] += out[u];
    out[u] = 0;
    val[u] += 1LL * n;
    G[u].clear();
    std::cout << ans << '\n';
  }

  return 0;
}