// I can't tell you what it really is,
// I can only tell you what it feels like.
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define F first
#define S second
#define rep(i,a,n) for (int i = a; i <= n; ++i)
#define fo(i, n)   for (int i = 1; i <= n; ++i)
#define all(v)  v.begin(), v.end()
#define pb push_back
#define P pair < int, int >
#define fast  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

const int N = 3e5 + 5;
int a[N], c[N];
set < P > s;
int fun(int qty) {
	int cost(0);
	while(qty && !s.empty()) {
		auto it = s.begin();
		int type = it->S;
		if(a[type] >= qty) {
			cost += qty * c[type];
			a[type] -= qty;
			if(!a[type])
				s.erase(s.begin());
			break;
		}else {
			cost += a[type] * c[type];
			qty -= a[type];
			a[type] = 0;
			s.erase(s.begin());
		}
	}
	return cost;
}
signed main(){
   fast;
   int n, m, type, qty, sum(0);
   cin >> n >> m;
   fo(i, n)	{
   	cin >> a[i];
   	sum += a[i];
   }
   fo(i, n) {
   	cin >> c[i];
   	s.insert({c[i], i});
   }
   int x(0);//cout << sum << '\n';
   while(m--) {
   	int cost(0);
   	cin >> 	type >> qty;
   	// cout << x << ' ' << qty << ' ';
   	if((sum - x < qty))  {
      	cout << 0 << '\n';
   		continue;
   	}
   	   	x += qty;

   	if(a[type] >= qty) {
   		cost = c[type] * qty;
   		a[type] -= qty;
   		qty = 0;
   		if(!a[type]) {
   			s.erase({c[type], type});
   		}
   	}
   	else{
   		cost += c[type] * a[type];
   		qty -= a[type];
   		a[type] = 0;
   		s.erase({c[type], type});
   	}

   	int let = fun(qty);
   	cout << cost + let << '\n';
   }

	return 0;
}


// I can't tell you what it really is,
// I can only tell you what it feels like.
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define F first
#define S second
#define rep(i,a,n) for (int i = a; i <= n; ++i)
#define fo(i, n)   for (int i = 1; i <= n; ++i)
#define all(v)  v.begin(), v.end()
#define pb push_back
#define P pair < int, int >
#define fast  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

const int N = 3e5 + 5;
int a[N], c[N];
set < P > s;
int fun(int qty) {
	int cost(0);
	// cout << qty << ' ';
	if(!qty)
	return 0;
	while(qty && !s.empty()) {
		auto it = s.begin();
		int type = it->S;
		if(a[type] >= qty) {
			cost += qty * c[type];
			a[type] -= qty;
			if(!a[type])
				s.erase(s.begin());
			break;
		}else {
			cost += a[type] * c[type];
			qty -= a[type];
			a[type] = 0;
			s.erase(s.begin());
		}
	}
	return cost;
}
signed main(){
   fast;
   int n, m, type, qty, sum(0);
   cin >> n >> m;
   fo(i, n)	{
   	cin >> a[i];
   	sum += a[i];
   }
   fo(i, n) {
   	cin >> c[i];
   	s.insert({c[i], i});
   }
   while(m--) {
   	int cost(0);
   	cin >> 	type >> qty;
   	if(sum < qty) {
   		cout << 0 << '\n';
   		continue;
   	}
   	if(a[type] >= qty) {
   		cost = c[type] * qty;
   		a[type] -= qty;
   		qty = 0;
   		if(!a[type]) {
   			s.erase({c[type], type});
   		}
   	}
   	else{
   		cost += c[type] * a[type];
   		qty -= a[type];
   		a[type] = 0;
   		s.erase({c[type], type});
   	}
   	cout << cost + fun(qty) << '\n';
   	// fo(i, n)
   	// 	cout << a[i] << ' ';
   	// 	cout << '\n';
   }

	return 0;
}


// I can't tell you what it really is,
// I can only tell you what it feels like.
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define F first
#define S second
#define rep(i,a,n) for (int i = a; i <= n; ++i)
#define fo(i, n)   for (int i = 1; i <= n; ++i)
#define all(v)  v.begin(), v.end()
#define pb push_back
#define P pair < int, int >
#define fast  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

const int N = 3e5 + 5;
int a[N], c[N];
set < P > s;
int fun(int qty) {
	int cost(0);
	while(qty && !s.empty()) {
		auto it = s.begin();
		int type = it->S;
		if(a[type] >= qty) {
			cost += qty * c[type];
			a[type] -= qty;
			qty = 0;
			if(!a[type])
				s.erase(s.begin());
			break;
		}else {
			cost += a[type] * c[type];
			qty -= a[type];
			a[type] = 0;
			s.erase(s.begin());
		}
	}
	// cout << qty << ' ';
	if(qty)
		return 0;
	return cost;
}
signed main(){
   fast;
   int n, m, type, qty, sum(0);
   cin >> n >> m;
   fo(i, n)	{
   	cin >> a[i];
   	sum += a[i];
   }
   fo(i, n) {
   	cin >> c[i];
   	s.insert({c[i], i});
   }
   while(m--) {
   	int cost(0);
   	cin >> 	type >> qty;

   	if(a[type] >= qty) {
   		cost = c[type] * qty;
   		a[type] -= qty;
   		qty = 0;
   		if(!a[type]) {
   			s.erase({c[type], type});
   		}
   	}
   	else{
   		cost += c[type] * a[type];
   		qty -= a[type];
   		a[type] = 0;
   		s.erase({c[type], type});
   	}

   	int let = fun(qty);
   	if(let || !qty)
   	cout << cost + let << '\n';
   	else
   		cout << 0 << '\n';
   }

	return 0;
}


// I can't tell you what it really is,
// I can only tell you what it feels like.
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define F first
#define S second
#define rep(i,a,n) for (int i = a; i <= n; ++i)
#define fo(i, n)   for (int i = 1; i <= n; ++i)
#define all(v)  v.begin(), v.end()
#define pb push_back
#define P pair < int, int >
#define fast  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

const int N = 3e5 + 5;
int a[N], c[N];
set < P > s;
int fun(int qty) {
	int cost(0);
	while(qty && !s.empty()) {
		auto it = s.begin();
		int type = it->S;
		if(a[type] >= qty) {
			cost += qty * c[type];
			a[type] -= qty;
			if(!a[type])
				s.erase(s.begin());
			break;
		}else {
			cost += a[type] * c[type];
			qty -= a[type];
			a[type] = 0;
			s.erase(s.begin());
		}
	}
	return cost;
}
signed main(){
   fast;
   int n, m, type, qty, sum(0);
   cin >> n >> m;
   fo(i, n)	{
   	cin >> a[i];
   	sum += a[i];
   }
   fo(i, n) {
   	cin >> c[i];
   	s.insert({c[i], i});
   }
   while(m--) {
   	int cost(0);
   	cin >> 	type >> qty;
   	if(sum - qty <= 0) {
   		sum -= qty;
   		cout << 0 << '\n';
   		continue;
   	}
   	if(a[type] >= qty) {
   		cost = c[type] * qty;
   		a[type] -= qty;
   		qty = 0;
   		if(!a[type]) {
   			s.erase({c[type], type});
   		}
   	}
   	else{
   		cost += c[type] * a[type];
   		qty -= a[type];
   		a[type] = 0;
   		s.erase({c[type], type});
   	}
   	int let = fun(qty);
   	cout << cost + let << '\n';
   }

	return 0;
}


// I can't tell you what it really is,
// I can only tell you what it feels like.
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define F first
#define S second
#define rep(i,a,n) for (int i = a; i <= n; ++i)
#define fo(i, n)   for (int i = 1; i <= n; ++i)
#define all(v)  v.begin(), v.end()
#define pb push_back
#define P pair < int, int >
#define fast  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define mp make_pair
#define PP pair < int, P >
const int N = 3e5 + 5;
int a[N], c[N];
int vis[N];
set < P > s;
int fun(int y) {
	int cost (0);
	while(y && !s.empty()) {
		auto it = s.begin();
		int x = it->S;
		if(a[it->S] > y) {
			a[it->S] -= y;
			return cost + y * c[x];
		}
		else {
			cost += it->F * a[x];
			y -= a[x];
			a[x] = 0;
			s.erase(s.begin());
		}
	}
	return cost;
}
signed main(){
   fast;
   int sum(0), n, m;
	cin >> n >> m;
	fo(i, n) {
		cin >> a[i];
		sum += a[i];
	}
	fo(i, n)
		cin >> c[i];
	int x, y;
	fo(i, n) {
		s.insert({c[i], i});
	}

	while(m--) {
		cin >> x >> y;int temp = y;
		if(sum - y < 0) {
			cout << 0 << '\n';
			continue;
		}
		sum -= y;
		int cost (0);
		if(a[x] > y) {
				cout << y * c[x] << '\n';
				a[x] -= y;
				continue;
		}
		if(a[x] <= y) {
				cost += a[x] * c[x];
				y -= a[x];
				a[x] = 0;
				s.erase({c[x], x});
		}
		cout << cost + fun(y) << '\n';
	}

	return 0;
}


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

typedef long long ll;

const int N = 2 * 1e2 + 10;

ll n, t;
ll res;
ll a[N], b[N];
std::multimap<pair<ll, ll>, ll> mp;

void solve(){
    ll item_type, total_item;
    cin >> item_type >> total_item;
    ll x = min(total_item, a[item_type]);
    a[item_type] -= x;
    total_item -= x;
    res += x * b[item_type];
    if(!a[item_type]) {
    	auto it = mp.find({b[item_type], item_type});
    	if(it != mp.end())
    	mp.erase({b[item_type], item_type});
	}

    while(total_item){
        auto itr = mp.begin();
        if(mp.size()){
            ll q = min(a[(*itr).first.second], total_item);
            total_item -= q;
            a[(*itr).first.second] -= q;
            res += q * b[(*itr).first.second];
            if(!a[(*itr).first.second])mp.erase(itr);
        }else{
            total_item = 0;
            res = 0;
        }
    }
    cout << res << "\n";
    res = 0;
}

int main(int argc, char const *argv[]){
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    cin >> n >> t;
    for (int i = 1; i <= n; ++i){
        cin >> a[i]; // item
    }
    for (int i = 1; i <= n; ++i){
        cin >> b[i]; // cost
    }
    for (int i = 1; i <= n; ++i){
        mp.insert({{b[i], i}, a[i]});
    }
    while(t--){
        solve();
    }
    return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MX_N 1000006
int n, m;
ll a[MX_N], c[MX_N];
map<ll, ll> mp[MX_N];

int main() {
  ios_base :: sync_with_stdio(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
  {
    cin >> a[i];
  }
  priority_queue< int, vector<int>, greater<int> > pq;
  for(int i = 1; i <= n; i++)
  {
    cin >> c[i];
    mp[c[i]][i] = a[i];
    pq.push(c[i]);
  }
  for(int i = 1; i <= m; i++)
  {
    ll cost = 0;
    int ti, di;
    cin >> ti >> di;
    if( a[ti] >= di )
    {
      cost = (c[ti] * di);
      a[ti] -= di;
      mp[c[ti]][ti] -= di;
      if( a[ti] == 0 )
      {
        mp[c[ti]].erase(ti);
      }
      cout << cost << "\n";
      continue;
    }
    cost = (1LL * c[ti] * a[ti]);
    di -= a[ti];
    a[ti] = 0;
    mp[c[ti]].erase(ti);
    while( !pq.empty() and di > 0 )
    {
      if( !mp[pq.top()].size() )
      {
        pq.pop();
        continue;
      }
      int cc = pq.top();
      vector<int> bad;
      for(auto it = mp[cc].begin(); di > 0 and it != mp[cc].end(); it++)
      {
        int id = it -> first;
        int have = it -> second;
        if( have <= di )
        {
          cost += (cc * have);
          a[id] = 0;
          bad.push_back(id);
          di -= have;
        }
        else
        {
          cost += (1LL * cc * di);
          have -= di;
          a[id] -= di;
          (it -> second) -= di;
          di = 0;
        }
      }
      for(auto& xx : bad)
      {
        mp[cc].erase(xx);
      }
      if( !mp[cc].size() )
      {
        pq.pop();
      }
    }
    if( di > 0 )
    {
      cout << "0\n";
      continue;
    }
    cout << cost << "\n";
  }
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MX_N 1000006
int n, m;
ll a[MX_N], c[MX_N];
map<ll, ll> mp[MX_N];

int main() {
  ios_base :: sync_with_stdio(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
  {
    cin >> a[i];
  }
  priority_queue< int, vector<int>, greater<int> > pq;
  for(int i = 1; i <= n; i++)
  {
    cin >> c[i];
    mp[c[i]][i] = a[i];
    pq.push(c[i]);
  }
  for(int i = 1; i <= m; i++)
  {
    ll cost = 0;
    int ti, di;
    cin >> ti >> di;
    if( a[ti] >= di )
    {
      cost = (c[ti] * di);
      a[ti] -= di;
      mp[c[ti]][ti] -= di;
      if( a[ti] == 0 )
      {
        mp[c[ti]].erase(ti);
      }
      cout << cost << "\n";
      continue;
    }
    cost = (c[ti] * a[ti]);
    di -= a[ti];
    a[ti] = 0;
    if( mp[c[ti]].find(ti) != mp[c[ti]].end() )
    {
      mp[c[ti]].erase(ti);
    }
    while( !pq.empty() and di > 0 )
    {
      if( !mp[pq.top()].size() )
      {
        pq.pop();
        continue;
      }
      int cc = pq.top();
      vector<int> bad;
      for(auto it = mp[cc].begin(); di > 0 and it != mp[cc].end(); it++)
      {
        int id = it -> first;
        int have = it -> second;
        if( have <= di )
        {
          cost += (cc * have);
          a[id] = 0;
          bad.push_back(id);
          di -= have;
        }
        else
        {
          cost += (1LL * cc * di);
          have -= di;
          a[id] -= di;
          (it -> second) -= di;
          di = 0;
        }
      }
      for(auto& xx : bad)
      {
        mp[cc].erase(xx);
      }
      if( !mp[cc].size() )
      {
        pq.pop();
      }
    }
    if( di > 0 )
    {
      cout << "0\n";
      continue;
    }
    cout << cost << "\n";
  }
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MX_N 1000006
int n, m, a[MX_N], c[MX_N];

map<int, int> mp[MX_N];

int main() {
  ios_base :: sync_with_stdio(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
  {
    cin >> a[i];
  }
  priority_queue< int, vector<int>, greater<int> > pq;
  for(int i = 1; i <= n; i++)
  {
    cin >> c[i];
    mp[c[i]][i] = a[i];
    pq.push(c[i]);
  }
  for(int i = 1; i <= m; i++)
  {
    ll cost = 0;
    int ti, di;
    cin >> ti >> di;
    if( a[ti] >= di )
    {
      cost = (1LL * c[ti] * di);
      a[ti] -= di;
      if( a[ti] == 0 )
      {
        mp[c[ti]].erase(ti);
      }
      cout << cost << "\n";
      continue;
    }
    cost = (1LL * c[ti] * a[ti]);
    di -= a[ti];
    a[ti] = 0;
    mp[c[ti]].erase(ti);
    while( !pq.empty() and di > 0 )
    {
      if( !mp[pq.top()].size() )
      {
        pq.pop();
        continue;
      }
      int cc = pq.top();
      bool flg = false;
      vector<int> bad;
      for(auto it = mp[cc].begin(); di > 0 and it != mp[cc].end(); it++)
      {
        int id = it -> first;
        int have = it -> second;
        if( have <= di )
        {
          cost += (1LL * cc * have);
          a[id] = 0;
          bad.push_back(id);
          di -= have;
        }
        else
        {
          cost += (1LL * cc * di);
          have -= di;
          (it -> second) -= di;
          di = 0;
        }
      }
      for(auto& xx : bad)
      {
        mp[cc].erase(xx);
      }
      if( !mp[cc].size() )
      {
        pq.pop();
      }
    }
    if( di > 0 )
    {
      cout << "0\n";
      continue;
    }
    cout << cost << "\n";
  }
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MX_N 1000006
int n, m;
ll a[MX_N], c[MX_N];
map<ll, ll> mp[MX_N];

int main() {
  ios_base :: sync_with_stdio(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
  {
    cin >> a[i];
  }
  priority_queue< ll, vector<ll>, greater<ll> > pq;
  for(int i = 1; i <= n; i++)
  {
    cin >> c[i];
    mp[c[i]][i] = a[i];
    pq.push(c[i]);
  }
  for(int i = 1; i <= m; i++)
  {
    ll cost = 0;
    int ti, di;
    cin >> ti >> di;
    if( a[ti] >= di )
    {
      cost = (c[ti] * di);
      a[ti] -= di;
      mp[c[ti]][ti] -= di;
      if( a[ti] == 0 )
      {
        mp[c[ti]].erase(ti);
      }
      cout << cost << "\n";
      continue;
    }
    cost = (c[ti] * a[ti]);
    di -= a[ti];
    a[ti] = 0;
    if( mp[c[ti]].find(ti) != mp[c[ti]].end() )
    {
      mp[c[ti]].erase(ti);
    }
    while( !pq.empty() and di > 0 )
    {
      if( !mp[pq.top()].size() )
      {
        pq.pop();
        continue;
      }
      ll cc = pq.top();
      vector<int> bad;
      for(auto it = mp[cc].begin(); di > 0 and it != mp[cc].end(); it++)
      {
        ll id = it -> first;
        ll have = it -> second;
        if( have <= di )
        {
          cost += (cc * have);
          a[id] = (it -> second) = 0;
          bad.push_back(id);
          di -= have;
        }
        else
        {
          cost += (cc * di);
          have -= di;
          a[id] -= di;
          (it -> second) -= di;
          di = 0;
        }
      }
      for(auto& xx : bad)
      {
        if( mp[cc].find(xx) != mp[cc].end() )
          mp[cc].erase(xx);
      }
      if( !mp[cc].size() )
      {
        pq.pop();
      }
    }
    if( di > 0 )
    {
      cout << "0\n";
      continue;
    }
    cout << cost << "\n";
  }
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MX_N 1000006
int n, m, a[MX_N], c[MX_N];

map<int, int> mp[MX_N];

int main() {
  ios_base :: sync_with_stdio(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
  {
    cin >> a[i];
  }
  priority_queue< int, vector<int>, greater<int> > pq;
  for(int i = 1; i <= n; i++)
  {
    cin >> c[i];
    mp[c[i]][i] = a[i];
    pq.push(c[i]);
  }
  for(int i = 1; i <= m; i++)
  {
    ll cost = 0;
    int ti, di;
    cin >> ti >> di;
    if( a[ti] >= di )
    {
      cost = (1LL * c[ti] * di);
      a[ti] -= di;
      mp[c[ti]][ti] -= di;
      if( a[ti] == 0 )
      {
        mp[c[ti]].erase(ti);
      }
      cout << cost << "\n";
      continue;
    }
    cost = (1LL * c[ti] * a[ti]);
    di -= a[ti];
    a[ti] = 0;
    mp[c[ti]].erase(ti);
    while( !pq.empty() and di > 0 )
    {
      if( !mp[pq.top()].size() )
      {
        pq.pop();
        continue;
      }
      int cc = pq.top();
      bool flg = false;
      vector<int> bad;
      for(auto it = mp[cc].begin(); di > 0 and it != mp[cc].end(); it++)
      {
        int id = it -> first;
        int have = it -> second;
        if( have <= di )
        {
          cost += (1LL * cc * have);
          a[id] = 0;
          bad.push_back(id);
          di -= have;
        }
        else
        {
          cost += (1LL * cc * di);
          have -= di;
          (it -> second) -= di;
          di = 0;
        }
      }
      for(auto& xx : bad)
      {
        mp[cc].erase(xx);
      }
      if( !mp[cc].size() )
      {
        pq.pop();
      }
    }
    if( di > 0 )
    {
      cout << "0\n";
      continue;
    }
    cout << cost << "\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int x,int y)
{
    if(c[x]==c[y])
        return x<y;
    return c[x]<c[y];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;                  //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);            //先取这个饭的总量和客人所要饭的数量的最小值
        ans=(long long)now*c[x];    //价钱先记上,要用强制类型转换，否则会溢出
        a[x]-=now;                  //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=(long long)now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
long long a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int a,int b)
{
    if(c[a]!=c[b])
        return c[a]<c[b];
    return a<b;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;            //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);      //先取这个饭的总量和客人所要饭的数量的最小值
        ans=now*c[x];         //价钱先记上
        a[x]-=now;            //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int a,int b)
{
    if(c[a]==c[b])
        return a<b;
    return c[a]<c[b];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;            //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);      //先取这个饭的总量和客人所要饭的数量的最小值
        ans=(long long)now*c[x];         //价钱先记上
        a[x]-=now;            //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=(long long)now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    long long a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.id<b.id;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            long long x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int a,int b)
{
    if(c[a]!=c[b])
        return c[a]<c[b];
    return a<b;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;            //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);      //先取这个饭的总量和客人所要饭的数量的最小值
        ans=(long long)now*c[x];         //价钱先记上
        a[x]-=now;            //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=(long long)now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.a<b.a;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.a<b.a;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            long long x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.a<b.a;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            int sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int x,int y)
{
    if(c[x]==c[y])
        return a[x]<a[y];
    return c[x]<c[y];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;                  //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);            //先取这个饭的总量和客人所要饭的数量的最小值
        ans=(long long)now*c[x];    //价钱先记上,要用强制类型转换，否则会溢出
        a[x]-=now;                  //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=(long long)now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int a,int b)
{
    if(c[a]!=c[b])
        return c[a]<c[b];
    return a<b;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;            //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);      //先取这个饭的总量和客人所要饭的数量的最小值
        ans=now*c[x];         //价钱先记上
        a[x]-=now;            //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int a,int b)
{
    if(c[a]!=c[b])
        return c[a]<c[b];
    return a<b;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;            //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);      //先取这个饭的总量和客人所要饭的数量的最小值
        ans=1ll*now*c[x];         //价钱先记上
        a[x]-=now;            //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=1ll*now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            int sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;

                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(q[n].a==0)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;

                        }
                        break;
                    }
                }
            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
struct node
{
    long long a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.a<b.a;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            long long x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
long long ans;
int a[N],c[N],p[N],n,m,x,y,k,now;
int cmp(int x,int y)
{
    if(c[x]==c[y])
        return a<b;
    return c[x]<c[y];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        p[i]=i;
    }
    sort(p+1,p+1+n,cmp);
    k=1;
    while(m--)
    {
        cin>>x>>y;                  //x为客人所要的那个饭的id，y为数量
        now=min(y,a[x]);            //先取这个饭的总量和客人所要饭的数量的最小值
        ans=(long long)now*c[x];    //价钱先记上,要用强制类型转换，否则会溢出
        a[x]-=now;                  //分别消去now值
        y-=now;
        while(y&&k<=n)
        {
            while(k<=n&&a[p[k]]==0)
                k++;
            if(k>n)
                break;
            now=min(y,a[p[k]]);
            ans+=(long long)now*c[p[k]];
            a[p[k]]-=now;
            y-=now;
        }
        if(y)
            ans=0;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            int sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(q[n].a==0)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct node
{
    int a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.id<b.id;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            long long x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,m;
int a[N],c[N],p[N];
int t,d,l,cur;
long long ans;
bool cmp(int a,int b)
{
	if (c[a]!=c[b])
		return c[a]<c[b];
	return a<b;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&c[i]);
		p[i]=i;
	}
	sort(p+1,p+n+1,cmp);
	l=1;
	while (m--)
	{
		scanf("%d%d",&t,&d);
		cur=min(d,a[t]);
		ans=1ll*cur*c[t];
		d-=cur;
		a[t]-=cur;
		while (d&&l<=n)
		{
			while (l<=n&&!a[p[l]])
				l++;
			if (l>n)
				break;
			cur=min(d,a[p[l]]);
			ans+=1ll*cur*c[p[l]];
			d-=cur;
			a[p[l]]-=cur;
		}
		if (d)
			ans=0;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
struct node
{
    long long a,c,id;
} q[N];
int cmp(node a,node b)
{
    if(a.c==b.c)
        return a.id<b.id;
    return a.c<b.c;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            q[i].id=i;
        for(int i=1; i<=n; i++)
            cin>>q[i].a;
        for(int i=1; i<=n; i++)
            cin>>q[i].c;
        sort(q+1,q+1+n,cmp);
        int flag=1;
        int k=1;
        for(int i=1; i<=m; i++)
        {
            long long x,y;
            cin>>x>>y;
            if(flag==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            long long sum=0;
            int data=0;
            for(int j=1; j<=n; j++)
            {
                if(q[j].id==x)
                {
                    if(q[j].a>=y)
                    {
                        sum=q[j].c*y;
                        data=1;
                        cout<<sum<<endl;
                        q[j].a-=y;
                        break;
                    }
                    else
                    {
                        sum+=q[j].a*q[j].c;

                        y-=q[j].a;
                        q[j].a=0;
                        //cout<<sum<<" "<<y<<endl;
                        while(1)
                        {
                            if(q[k].a>=y)
                            {
                                sum+=y*q[k].c;
                                q[k].a-=y;
                                break;
                            }
                            else
                            {
                                sum+=q[k].a*q[k].c;

                                y-=q[k].a;
                                q[k].a=0;
                                k++;
                            }
                            if(k==n+1)
                            {
                                flag=0;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            sum=0;
                            break;
                        }
                    }
                }

            }
            if(data==0)
                cout<<sum<<endl;
        }
    }
    return 0;
}


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

typedef long long ll;

int a[100005],c[100005];

struct node
{
    int cnt;
    int id;
    int co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        //cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(ll)(d*c[ty]);
            a[ty]-=d;
            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(ll)(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(ll)(d*np[k].co);
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(ll)(a[np[k].id]*np[k].co);
                       d-=a[np[k].id];
                       a[np[k].id]=0;
                    }
                    else{
                        k++;
                    }
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


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

typedef long long ll;

int a[100005],c[100005];
vector<pair<int,int>>pv;

struct node
{
    int cnt;
    int id;
    int co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(d*c[ty]);
            a[ty]-=d;
            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(d*np[k].co);
                    np[k].cnt-=d;
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(np[k].cnt*np[k].co);
                       d-=np[k].cnt;
                       a[np[k].id]=0;
                    }
                }
                if(a[np[k].id]==0){
                    k++;
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


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

typedef long long ll;

int a[100005],c[100005];
vector<pair<int,int>>pv;

struct node
{
    int cnt;
    int id;
    int co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        //cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(d*c[ty]);
            a[ty]-=d;

            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(d*np[k].co);
                    np[k].cnt-=d;
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(a[np[k].id]*np[k].co);
                       d-=a[np[k].id];
                       a[np[k].id]=0;
                    }
                }
                if(a[np[k].id]==0){
                    k++;
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


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

typedef long long ll;

int a[100005],c[100005];
vector<pair<int,int>>pv;

struct node
{
    int cnt;
    int id;
    int co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        //cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(d*c[ty]);
            a[ty]-=d;
            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(d*np[k].co);
                    np[k].cnt-=d;
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(np[k].cnt*np[k].co);
                       d-=np[k].cnt;
                       a[np[k].id]=0;
                    }
                }
                if(a[np[k].id]==0){
                    k++;
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


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

typedef long long ll;

ll a[100005];
ll c[100005];

struct node
{
    ll cnt;
    ll id;
    ll co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        //cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(ll)(d*c[ty]);
            a[ty]-=d;
            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(ll)(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(ll)(d*np[k].co);
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(ll)(a[np[k].id]*np[k].co);
                       d-=a[np[k].id];
                       a[np[k].id]=0;
                    }
                    else{
                        k++;
                    }
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


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

typedef long long ll;

int a[100005],c[100005];
vector<pair<int,int>>pv;

struct node
{
    int cnt;
    int id;
    int co;
};

bool compare(node A,node B)
{
    if(A.co==B.co){
        return A.id<B.id;
    }
    return A.co<B.co;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,ty,d,k=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    struct node np[100005];
    for(int i=1;i<=n;i++){
        cin>>c[i];
        np[i].cnt=a[i];
        np[i].id=i;
        np[i].co=c[i];
    }
    ll sum=0;
    sort(np+1,np+n+1,compare);
    for(int i=1;i<=m;i++){
        cin>>ty>>d;
        //cout<<a[ty]<<endl;
        if(a[ty]>=d){
            sum+=(d*c[ty]);
            a[ty]-=d;
            d=0;
        }
        else{
           if(a[ty]>0){
                sum+=(a[ty]*c[ty]);
                d-=a[ty];
                a[ty]=0;
           }
           while(d!=0&&k<=n){
               if(a[np[k].id]>=d){
                    sum+=(d*np[k].co);
                    a[np[k].id]-=d;
                    d=0;
                }
                else{
                    if(a[np[k].id]>0){
                       sum+=(a[np[k].id]*np[k].co);
                       d-=a[np[k].id];
                       a[np[k].id]=0;
                    }
                    else{
                        k++;
                    }
                }
           }
        }
        if(d){
            cout<<0<<endl;
        }
        else{
            cout<<sum<<endl;
        }
        sum=0;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}else if(vec[x].first < y && total <= y){
			vec[x].first = 0;
			total -= vec[x].first;
		}
//		cout << vec[x].first << endl;
		cout << count << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
//		cout << total << endl;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}else if(vec[x].first < y && total <= y){
			vec[x].first = 0;
		}
		if(total <= y){
			total = 0;
		}
//		cout << vec[x].first << endl;
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(index == n){
			//skip it.
		}else if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			while(left > 0 && index < n){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
			total -= left;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(index == n){

		}else if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
			total -= left;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	/*for(int i = 0; i < n; i++){
		cout << sorted[i].first << " " << sorted[i].second << endl;
	}*/

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
//		cout << total << endl;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}
		if(total >= y){
			total = 0;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}else if(total < y){
			vec[x].first = 0;
			total = 0;
		}
//		cout << vec[x].first << endl;
		cout << count << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(index = n){

		}else if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			while(left > 0 && index < n){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
			total -= left;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	/*for(int i = 0; i < n; i++){
		cout << sorted[i].first << " " << sorted[i].second << endl;
	}*/

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
//		cout << total << endl;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}
		if(total <= y){
			total = 0;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(index == n){

		}else if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			while(left > 0 && index < n){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
			total -= left;
		}
		cout << count << endl;
	}

}


#include<bits/stdc++.h>

using namespace std;

struct sort_rule {
    bool operator()(const std::pair<long long,long long> &left, const std::pair<long long,long long> &right) {
        return left.second < right.second;
    }
};

int main(){

	long long n,m,total = 0;
	cin >> n >> m;

	vector<pair<long long,long long>> vec(n);
	vector<pair<long long,long long>> sorted(n);

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		total += x;
		vec[i].first = x;
		sorted[i].first = i;
	}

	for(long long i = 0; i < n; i++){
		long long x;
		cin >> x;
		vec[i].second = x;
		sorted[i].second = x;
	}

	std::sort(sorted.begin(), sorted.end(), sort_rule());

	long long index = 0;

	for(long long i = 0; i < m; i++){
		long long x,y;
		cin >> x >> y;
		x--;
		long long count = 0;
		if(vec[x].first >= y){
			total -= y;
			vec[x].first -= y;
			count += y*vec[x].second;
		}else if(vec[x].first < y && total >= y){
			count += vec[x].second * vec[x].first;
			long long left = y - vec[x].first;
			vec[x].first = 0;
			total -= y;
			while(left > 0){
				long long dish = sorted[index].first;
				long long a = min(vec[dish].first,left);
				count += a*vec[dish].second;
				left -= a;
				vec[dish].first -= a;
				if(vec[dish].first == 0) index++;
			}
		}else if(vec[x].first < y && total <= y){
			vec[x].first = 0;
			total -= vec[x].first;
		}
		if(total <= y){
			total = 0;
		}
//		cout << vec[x].first << endl;
		cout << count << endl;
	}

}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD = 1e6 + 131;
const int INF = 1e9 + 7;
const int base = 1e9;
const int MAX = 1e6+131;
const double EPS = 1e-9;
const double PI = acos(-1.);

int main() {
    int n, m;
    cin>>n>>m;
    vector<ll> a(n+1), c(n+1);
    for(int i=1;i<=n;i++) {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++) {
        cin>>c[i];
    }
    for(int i=0;i<m;i++) {
        ll t, d;
        ll sum=0;
        cin>>t>>d;
        if(d<=a[t]) {
            sum+=c[t]*d;
            a[t]-=d;
            d=0;
        }
        else {
            sum+=c[t]*a[t];
            d-=a[t];
            a[t]=0;
            while(d) {
                int mn=MAX, x=-1;
                for(int j=1;j<=n;j++) {

                    if(c[j]<mn && a[j]!=0) {
                        mn=c[j];
                        x=j;
                    }
                }
                    if(x==-1) {
                        sum=0;
                        break;
                    }
                    if(d<=a[x]) {
                        sum+=c[x]*d;
                        a[x]-=d;
                        d=0;
                    }
                    else {
                        sum+=c[x]*a[x];
                        d-=a[x];
                        a[x]=0;
                    }

            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD =  998244353;
const ll INF = 3e9 + 7;
const int base = 1e9;
const int MAX = 1e6+2;
const double EPS = 1e-9;
const double PI = acos(-1.);

bool by_cost(vector<int> x, vector<int> y)
{
    return x[0]<y[0];
}

int main() {
    int n, m;
    cin>>n>>m;
    vector<int> a(n), c(n);
    vector<vector<int>> v(n);
    for(int i=0;i<n;i++) {
        cin>>a[i];

    }
    for(int i=0;i<n;i++) {
        cin>>c[i];
        v[i].pb(c[i]);
        v[i].pb(i);
    }
    sort(v.begin(),v.end(), by_cost);
    int take=0;
    for(int i=0;i<m;i++) {
        if(take==n) {
            cout<<0<<endl;
            continue;
        }
        ull t, d, bill=0;
        cin>>t>>d;
        t--;
        if(a[t]>=d) {
            a[t]-=d;
            bill = c[t]*d;
            d=0;
        }
        else {
            d-=a[t];
            bill+=a[t]*c[t];
            a[t]=0;
            while(d) {
                if(take==n) {
                    bill=0;
                    break;
                }
                if(a[v[take][1]] >= d) {
                    a[v[take][1]]-=d;
                    bill += c[v[take][1]]*d;
                    d=0;
                }
                else {
                    d-=a[v[take][1]];
                    bill+=a[v[take][1]]*c[v[take][1]];
                    a[v[take][1]]=0;
                    take++;

                }
            }
        }
        cout<<bill<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD =  998244353;
const ll INF = 3e9 + 7;
const int base = 1e9;
const int MAX = 1e6+2;
const double EPS = 1e-9;
const double PI = acos(-1.);

bool by_cost(vector<int> x, vector<int> y)
{
    return x[0]<y[0];
}

int main() {
    int n, m;
    cin>>n>>m;
    vector<int> a(n), c(n);
    vector<vector<int>> v(n);
    for(int i=0;i<n;i++) {
        cin>>a[i];

    }
    for(int i=0;i<n;i++) {
        cin>>c[i];
        v[i].pb(c[i]);
        v[i].pb(i);
    }
    sort(v.begin(),v.end(), by_cost);
    int take=0;
    for(int i=0;i<m;i++) {
        if(take==n) {
            cout<<0<<endl;
            continue;
        }
        ll t, d, bill=0;
        cin>>t>>d;
        t--;
        if(a[t]>=d) {
            a[t]-=d;
            bill = c[t]*d;
            d=0;
        }
        else {
            d-=a[t];
            bill+=a[t]*c[t];
            a[t]=0;
            while(d) {
                if(take==n) {
                    bill=0;
                    break;
                }
                if(a[v[take][1]] >= d) {
                    a[v[take][1]]-=d;
                    bill += c[v[take][1]]*d;
                    d=0;
                }
                else {
                    d-=a[v[take][1]];
                    bill+=a[v[take][1]]*c[v[take][1]];
                    a[v[take][1]]=0;
                    take++;

                }
            }
        }
        cout<<bill<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD = 1e6 + 131;
const int INF = 1e9 + 7;
const int base = 1e9;
const int MAX = 1e6+131;
const double EPS = 1e-9;
const double PI = acos(-1.);

int main() {
    int n, m;
    cin>>n>>m;
    vector<int> a(n+1), c(n+1);
    for(int i=1;i<=n;i++) {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++) {
        cin>>c[i];
    }
    for(int i=0;i<m;i++) {
        ll t, d;
        ll sum=0;
        cin>>t>>d;
        if(d<=a[t]) {
            sum+=c[t]*d;
            a[t]-=d;
            d=0;
        }
        else {
            sum+=c[t]*a[t];
            d-=a[t];
            a[t]=0;
            while(d) {
                int mn=MAX, x=-1;
                for(int j=1;j<=n;j++) {

                    if(c[j]<mn && a[j]!=0) {
                        mn=c[j];
                        x=j;
                    }
                }
                    if(x==-1) {
                        sum=0;
                        break;
                    }
                    if(d<=a[x]) {
                        sum+=c[x]*d;
                        a[x]-=d;
                        d=0;
                    }
                    else {
                        sum+=c[x]*a[x];
                        d-=a[x];
                        a[x]=0;
                    }

            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD =  998244353;
const ll INF = 3e9 + 7;
const int base = 1e9;
const int MAX = 1e6+2;
const double EPS = 1e-9;
const double PI = acos(-1.);

bool by_cost(vector<int> x, vector<int> y)
{
    return x[0]<y[0];
}

int main() {
    int n, m;
    cin>>n>>m;
    vector<ull> a(n), c(n);
    vector<vector<int>> v(n);
    for(int i=0;i<n;i++) {
        cin>>a[i];

    }
    for(int i=0;i<n;i++) {
        cin>>c[i];
        v[i].pb(c[i]);
        v[i].pb(i);
    }
    sort(v.begin(),v.end(), by_cost);
    int take=0;
    for(int i=0;i<m;i++) {
        if(take==n) {
            cout<<0<<endl;
            continue;
        }
        ull t, d, bill=0;
        cin>>t>>d;
        t--;
        if(a[t]>=d) {
            a[t]-=d;
            bill = c[t]*d;
            d=0;
        }
        else {
            d-=a[t];
            bill+=a[t]*c[t];
            a[t]=0;
            while(d) {
                if(take==n) {
                    bill=0;
                    break;
                }
                if(a[v[take][1]] >= d) {
                    a[v[take][1]]-=d;
                    bill += c[v[take][1]]*d;
                    d=0;
                }
                else {
                    d-=a[v[take][1]];
                    bill+=a[v[take][1]]*c[v[take][1]];
                    a[v[take][1]]=0;
                    take++;

                }
            }
        }
        cout<<bill<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

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

#define sync                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
const int MOD =  998244353;
const ll INF = 3e9 + 7;
const int base = 1e9;
const int MAX = 1e6+2;
const double EPS = 1e-9;
const double PI = acos(-1.);

bool by_cost(vector<int> x, vector<int> y)
{
    return x[0]<y[0];
}

int main() {
    int n, m;
    cin>>n>>m;
    vector<int> a(n), c(n);
    vector<vector<int>> v(n);
    for(int i=0;i<n;i++) {
        cin>>a[i];

    }
    for(int i=0;i<n;i++) {
        cin>>c[i];
        v[i].pb(c[i]);
        v[i].pb(i);
    }
    sort(v.begin(),v.end(), by_cost);
    int take=0;
    for(int i=0;i<m;i++) {
        if(take==n) {
            cout<<0<<endl;
            continue;
        }
        int t, d, bill=0;
        cin>>t>>d;
        t--;
        if(a[t]>=d) {
            a[t]-=d;
            bill = c[t]*d;
            d=0;
        }
        else {
            d-=a[t];
            bill+=a[t]*c[t];
            a[t]=0;
            while(d) {
                if(take==n) {
                    bill=0;
                    break;
                }
                if(a[v[take][1]] >= d) {
                    a[v[take][1]]-=d;
                    bill += c[v[take][1]]*d;
                    d=0;
                }
                else {
                    d-=a[v[take][1]];
                    bill+=a[v[take][1]]*c[v[take][1]];
                    a[v[take][1]]=0;
                    take++;

                }
            }
        }
        cout<<bill<<endl;
    }
    return 0;
}


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

int main()
{
   int n,m,i,j;
   cin>>n>>m;   //1e5...
   int cost[n+1],remain[n+1];
   for(i=1;i<=n;i++)
   {
       cin>>remain[i];
   }

   for(i=1;i<=n;i++)
   {
       cin>>cost[i];
   }

   vector<pair < pair<int,int> ,int > > v;
   for(i=1;i<=n;i++)
   {
       v.push_back(make_pair(make_pair(cost[i],remain[i]),i));
   }
   sort(v.begin(),v.end());

   /*for(i=0;i<n;i++)
   {
       cout<<v[i].first.first<<" "<<v[i].first.second<<" "<<v[i].second<<endl;
   }*/

   while(m--)
   {
       long long sum=0;
       int flag=0;
       int index,number;
       cin>>index>>number;
       for(i=0;i<n;i++)
       {
           if(index==v[i].second)
           {
               while(v[i].first.second!=0)
               {
                   v[i].first.second-=1;
                   sum =sum + v[i].first.first;
                   number--;
                   if(number==0)
                   {
                       cout<<sum<<endl;
                       flag=1;
                       break;
                   }
               }

               if(flag) break;

               if(number)
               {
                   for(i=0;i<n;i++)
                   {
                       while(v[i].first.second)
                       {
                           v[i].first.second--;
                           number--;
                           sum = sum+v[i].first.first;
                           if(number==0)
                           {
                               cout<<sum<<endl;
                               flag=1;
                               break;
                           }
                       }
                       if(flag) break;
                   }
                   if(flag) break;
               }
           }
       }
       if(number) cout<<0<<endl;
   }

}


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

int main()
{
   int n,m,i,j;
   cin>>n>>m;   //1e5...
   int cost[n+1],remain[n+1];
   for(i=1;i<=n;i++)
   {
       cin>>remain[i];
   }

   for(i=1;i<=n;i++)
   {
       cin>>cost[i];
   }

   vector<pair < pair<int,int> ,int > > v;
   for(i=1;i<=n;i++)
   {
       v.push_back(make_pair(make_pair(cost[i],remain[i]),i));
   }
   sort(v.begin(),v.end());

   /*for(i=0;i<n;i++)
   {
       cout<<v[i].first.first<<" "<<v[i].first.second<<" "<<v[i].second<<endl;
   }*/

   while(m--)
   {
       long long sum=0;
       int flag=0;
       int index,number;
       cin>>index>>number;
       for(i=0;i<n;i++)
       {
           if(index==v[i].second)
           {
               while(v[i].first.second!=0)
               {
                   v[i].first.second-=1;
                   sum =sum + v[i].first.first;
                   number--;
                   if(number==0)
                   {
                       cout<<sum<<endl;
                       flag=1;
                       break;
                   }
               }

               if(flag) break;

               if(number)
               {
                   for(j=0;j<n;j++)
                   {
                       while(v[j].first.second)
                       {
                           v[j].first.second--;
                           number--;
                           sum = sum+v[j].first.first;
                           if(number==0)
                           {
                               cout<<sum<<endl;
                               flag=1;
                               break;
                           }
                       }
                       if(flag) break;
                   }
                   if(flag) break;
               }
           }
       }
       if(number) cout<<0<<endl;
   }

}


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

priority_queue< pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;  //min heap...

int main()
{
    int n,m,i,t,d;
    cin>>n>>m;
    int remain[300010+1],cost[300010+1];
    for(i=1;i<=n;i++)
    {
        cin>>remain[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>cost[i];
        pq.push(make_pair(cost[i],i));
    }
    while(m--)
    {
        cin>>t>>d;
        long long int ans= 0;
        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            cout<<d*cost[t]<<endl;
            remain[t]=remain[t]-d;

        }
        else  //means the number of type t dish is less than d...
        {
            bool flag=false;
             ans = remain[t]*cost[t];
            d-=remain[t];
            remain[t]=0;

            //ab bache hui dishes pq se fulfil krenge...
            while(!pq.empty())
            {
                while(!pq.empty() && remain[pq.top().second]==0) pq.pop();
                if(pq.empty()) break;

                pair<int,int> ne = pq.top();
                if(d<=remain[ne.second])
                {
                    flag=true;
                    remain[ne.second]-=d;
                    ans+=d*ne.first;
                    cout<<ans<<endl;
                    break;
                }
                else
                {
                    ans+=remain[ne.second]*ne.first;
                     d = d-remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

            }
            if(!flag) cout<<0<<endl;

        }

    }

}


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

priority_queue< pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;  //min heap...

int main()
{
    int n,m,i,t,d;
    cin>>n>>m;
    int remain[n+1],cost[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>remain[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>cost[i];
        pq.push(make_pair(cost[i],i));
    }
    while(m--)
    {
        cin>>t>>d;

        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            cout<<d*cost[t]<<endl;
            remain[t]=remain[t]-d;

        }
        else  //means the number of type t dish is less than d...
        {
            bool flag=false;
            long long int ans = remain[t]*cost[t];
            d-=remain[t];
            remain[t]=0;

            //ab bache hui dishes pq se fulfil krenge...
            while(!pq.empty())
            {
                while(!pq.empty() && remain[pq.top().second]==0) pq.pop();
                if(pq.empty()) break;

                auto ne = pq.top();
                if(d<=remain[ne.second])
                {
                    flag=true;
                    remain[ne.second]-=d;
                    ans+=d*ne.first;
                    cout<<ans<<endl;
                    break;
                }
                else
                {
                    ans+=remain[ne.second]*ne.first;
                     d = d-remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

            }
            if(!flag) cout<<0<<endl;

        }

    }

}


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

priority_queue< pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;  //min heap...
 long long int ans;

int main()
{
    int n,m,i,t,d;
    cin>>n>>m;
    int remain[300010+1],cost[300010+1];
    for(i=1;i<=n;i++)
    {
        cin>>remain[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>cost[i];
        pq.push(make_pair(cost[i],i));
    }
    while(m--)
    {
        cin>>t>>d;

        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            remain[t]=remain[t]-d;
            long long int pp=d*cost[t];
            cout<<pp<<endl;

        }
        else  //means the number of type t dish is less than d...
        {
            bool flag=false;
             ans = remain[t]*cost[t];
            d-=remain[t];
            remain[t]=0;

            //ab bache hui dishes pq se fulfil krenge...
            while(!pq.empty())
            {
                while(!pq.empty() && remain[pq.top().second]==0) pq.pop();
                if(pq.empty()) break;

                pair<int,int> ne = pq.top();
                if(d<=remain[ne.second])
                {
                    flag=true;
                    remain[ne.second]-=d;
                    ans+=d*ne.first;
                    cout<<ans<<endl;
                    break;
                }
                else
                {
                    ans+=remain[ne.second]*ne.first;
                     d = d-remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

            }
            if(!flag) cout<<0<<endl;

        }

    }

}


#include <bits/stdc++.h>
#define N 300010
#define PII pair<int, int>
using namespace std;

typedef long long LL;

int n, m, a[N], c[N], t, d;
LL ans = 0;

priority_queue<PII, vector<PII>, greater<PII> > Q;

int main(){
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}

	for (int i = 1; i <= n; i++){
		scanf("%d", &c[i]);
		Q.push(make_pair(c[i], i));
	}

	for (int i = 1; i <= m; i++){
		scanf("%d%d", &t, &d);

		if (d <= a[t])
            {
			a[t] -= d;
			printf("%lld\n", 1LL * d * c[t]);
		    }

		else {
			bool flag = false;
			LL ans = 1LL * a[t] * c[t];
			d -= a[t];
			a[t] = 0;
			while (!Q.empty()){
				while (!Q.empty() && a[Q.top().second] == 0) Q.pop();
				if (Q.empty()) break;
				PII now = Q.top();
				if (d <= a[now.second]){
					a[now.second] -= d;
					ans += 1LL * d * now.first;
					flag = true;
					printf("%lld\n", ans);
					break;
				} else {
					ans += 1LL * a[now.second] * now.first;
					d -= a[now.second];
					a[now.second] = 0;
					Q.pop();
				}
			}

			if (!flag){
				puts("0");
			}
		}
	}
}


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

priority_queue< pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;  //min heap...

int main()
{
    int n,m,i,t,d;
    cin>>n>>m;
    int remain[300010+1],cost[300010+1];
    for(i=1;i<=n;i++)
    {
        cin>>remain[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>cost[i];
        pq.push(make_pair(cost[i],i));
    }
    while(m--)
    {
        cin>>t>>d;

        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            cout<<d*cost[t]<<endl;
            remain[t]=remain[t]-d;

        }
        else  //means the number of type t dish is less than d...
        {
            bool flag=false;
            long long int ans = remain[t]*cost[t];
            d-=remain[t];
            remain[t]=0;

            //ab bache hui dishes pq se fulfil krenge...
            while(!pq.empty())
            {
                while(!pq.empty() && remain[pq.top().second]==0) pq.pop();
                if(pq.empty()) break;

                pair<int,int> ne = pq.top();
                if(d<=remain[ne.second])
                {
                    flag=true;
                    remain[ne.second]-=d;
                    ans+=d*ne.first;
                    cout<<ans<<endl;
                    break;
                }
                else
                {
                    ans+=remain[ne.second]*ne.first;
                     d = d-remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

            }
            if(!flag) cout<<0<<endl;

        }

    }

}


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

priority_queue< pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;  //min heap...
 long long int ans;

int main()
{
    int n,m,i,t,d;
    cin>>n>>m;
    int remain[300010+1],cost[300010+1];
    for(i=1;i<=n;i++)
    {
        cin>>remain[i];
    }
    for(i=1;i<=n;i++)
    {
        cin>>cost[i];
        pq.push(make_pair(cost[i],i));
    }
    while(m--)
    {
        cin>>t>>d;

        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            remain[t]=remain[t]-d;
            long long int pp=d*cost[t];
            cout<<pp<<endl;

        }
        else  //means the number of type t dish is less than d...
        {
            bool flag=false;
             ans = 1LL * remain[t] * cost[t];
            d-=remain[t];
            remain[t]=0;

            //ab bache hui dishes pq se fulfil krenge...
            while(!pq.empty())
            {
                while(!pq.empty() && remain[pq.top().second]==0) pq.pop();
                if(pq.empty()) break;

                pair<int,int> ne = pq.top();
                if(d<=remain[ne.second])
                {
                    remain[ne.second]-=d;
                    flag=true;
                    ans+=1LL *d*(ne.first);
                    cout<<ans<<endl;
                    break;
                }
                else
                {
                    ans+=1LL * remain[ne.second] * ne.first;
                     d = d-remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

            }
            if(!flag) cout<<0<<endl;

        }

    }

}