#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=0;

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));
    }
    for(i=1;i<=m;i++)
    {
        cin>>t>>d;
        ans=0;
        //case 1:- I got the required no. of dishes of type t
        if(d<=remain[t])
        {
            remain[t]-=d;
            long long int pp=1LL *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 -= remain[ne.second];
                    remain[ne.second]=0;
                    pq.pop();
                }

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

        }

    }

}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
LL a[101010], c[101010];
vector<pair<int, int>> v;
LL t, d;
LL sum = 0;
int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
        sum += a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        if(sum < d){
            cout << 0 << endl;
            continue;
        }
        sum -= d;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;

            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                    idx++;
                }
            }
        }
        cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
LL a[101010], c[101010];
vector<pair<int, int>> v;
LL t, d;

int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;
            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                }
            }
        }
        if(d)cout << 0 << endl;
        else cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
LL a[101010], c[101010];
vector<pair<int, int>> v;
LL t, d;
LL sum = 0;
int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
        sum += a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        sum -= d;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;
            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                }
            }
        }
        cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
int a[101010], c[101010];
vector<pair<int, int>> v;
int t, d;
LL sum = 0;
int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
        sum += a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        if(sum < d){
            cout << 0 << endl;
            continue;
        }
        sum -= d;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;

            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                    idx++;
                }
            }
        }
        cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
int a[101010], c[101010];
vector<pair<int, int>> v;
int t, d;

int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;
            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                    idx++;
                }
            }
        }
        if(d)cout << 0 << endl;
        else cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
LL a[101010], c[101010];
vector<pair<int, int>> v;
LL t, d;
LL sum = 0;
int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
        sum += a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        sum -= d;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;
            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                }
            }
        }
        if(d)cout << 0 << endl;
        cout << ret << endl;
    }
}


# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, m;
LL a[101010], c[101010];
vector<pair<int, int>> v;
LL t, d;

int main(){
    INIT;
    cin >> n >> m;
    REP(i, n){
        cin >> a[i];
    }
    REP(i, n){
        cin >> c[i];
        v.emplace_back(make_pair(c[i], i));
    }
    sort(ALL(v));
    int idx = 0;
    REP(i, m){
        cin >> t >> d;
        LL ret = 0;
        t--;
        if(a[t] >= d){
            a[t] -= d;
            ret += c[t]*d;
            d = 0;
        }else{
            ret += c[t]*a[t];
            d -= a[t];
            a[t] = 0;
            while(d >= 1){
                if(idx == n)break;
                if(a[v[idx].second] == 0){
                    idx++;
                    continue;
                }
                if(a[v[idx].second] >= d){
                    a[v[idx].second] -= d;
                    ret += c[v[idx].second]*d;
                    d = 0;
                }else{
                    d -= a[v[idx].second];
                    ret += c[v[idx].second]*a[v[idx].second];
                    a[v[idx].second] = 0;
                }
            }
        }
        if(d)cout << 0 << endl;
        cout << ret << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1,0);

    /*for (int i = 0; i<n; i++)
        cout<<as[i].a<<" "<<as[i].c<<" "<<as[i].rnk<<endl;
    cout<<endl;*/

    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, temp, t, d;
    long long tc = 0;
    for (int i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d==0)
            cout<<tc<<endl;
        else
            cout<<0<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    if (x.c==y.c)
        return x.rnk < y.rnk;
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1);
    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc;
    for (int i = 0; i<m; i++) {
        int t, d;
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        int temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d!=0)
            tc = 0;
        cout<<tc<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1,0);

    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc, temp, t, d;
    for (int i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d==0)
            cout<<tc<<endl;
        else
            cout<<0<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    if (x.c==y.c)
        return x.rnk < y.rnk;
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1);
    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc;
    for (int i = 0; i<m; i++) {
        int t, d;
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        int temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
            dish--;
            if (d>0)
                tc = 0;
        }
        cout<<tc<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1,0);

    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc, temp, t, d;
    for (int i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d!=0)
            tc = 0;
        cout<<tc<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    if (x.c == y.c)
        return x.rnk < y.rnk;

    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1);
    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc;
    for (int i = 0; i<m; i++) {
        int t, d;
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        int temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                dish++;
            }
            dish--;
            if (d>0)
                tc = 0;
        }
        cout<<tc<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1,0);

    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc, temp, t, d;
    for (int i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d==0)
            cout<<tc<<endl;
        else
            cout<<0<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    if (x.c==y.c)
        return x.rnk < y.rnk;
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1);
    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc;
    for (int i = 0; i<m; i++) {
        int t, d;
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        int temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
            dish--;
        }
        if (d!=0)
            tc = 0;
        cout<<tc<<endl;

    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 998244353;

struct node {
    int a;
    int c;
    int rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    int n, m;
    cin>>n>>m;

    struct node as[n];

    for (int i = 0; i<n; i++)
        cin>>as[i].a;
    for (int i = 0; i<n; i++)
        cin>>as[i].c;
    for (int i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1);
    for (int i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    int rank, dish = 0, cost, tc, temp, t, d;
    for (int i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d!=0)
            tc = 0;
        cout<<tc<<endl;

    }
	return 0;
}


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

struct node {
    long long a;
    long long c;
    long long rnk;
};

bool compare1 (struct node x, struct node y)
{
    return x.c < y.c;
}

int main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    long long n, m, i;
    cin>>n>>m;

    struct node as[n];

    for (i = 0; i<n; i++)
        cin>>as[i].a;
    for (i = 0; i<n; i++)
        cin>>as[i].c;
    for (i = 0; i<n; i++)
        as[i].rnk = i+1;

    sort (as, as+n, compare1);
    vector <int> pos(n+1,0);

    /*for (int i = 0; i<n; i++)
        cout<<as[i].a<<" "<<as[i].c<<" "<<as[i].rnk<<endl;
    cout<<endl;*/

    for (i = 0; i<n; i++)
        pos[as[i].rnk] = i;

    long long rank, dish = 0, cost, tc, temp, t, d;
    for (i = 0; i<m; i++) {
        cin>>t>>d;

        rank = pos[t];
        cost = as[rank].c;
        tc = 0;

        temp = min(as[rank].a,d);
        tc += temp*cost;
        as[rank].a -= temp;
        d -= temp;

        if (d>0) {
            while (dish<n && d>0) {
                temp = min(as[dish].a,d);
                cost = as[dish].c;
                tc += temp*cost;
                as[dish].a -= temp;
                d -= temp;
                if (as[dish].a==0)
                    dish++;
            }
        }
        if (d==0)
            cout<<tc<<endl;
        else
            cout<<0<<endl;
    }
	return 0;
}


// Problem Code: 1106/B

#include <bits/stdc++.h>
// #include <vector>
// #include <queue>
// #include <utility>

using namespace std;

typedef priority_queue< pair<int, int>,
                        vector< pair<int, int> >,
                        greater< pair<int, int> > > PQ;

int findCheapest(vector<int> a, PQ &pq) {
    while (!pq.empty() && a[pq.top().second] == 0)
        pq.pop();
    return (pq.empty()) ? 0 : pq.top().second;
}

long long calculateCost(int u, int v, vector<int> &a, vector<int> c, vector<int> &d) {
    long long cost;
    if (d[u] <= a[v]) {
        a[v] -= d[u];
        cost = (long long) d[u] * c[v];
        d[u] = 0;
    } else {
        d[u] -= a[v];
        cost = (long long) a[v] * c[v];
        a[v] = 0;
    }
    return cost;
}

vector<long long> foodOrdering(int n, int m, vector<int> &a, vector<int> c, vector<int> t, vector<int> &d) {
    int pos;
    vector<long long> cost(m + 1);
    PQ pq;

    for (int i = 1; i <= n; i++)
        pq.push(make_pair(c[i], i));

    for (int i = 1; i <= m; i++) {
        cost[i] = calculateCost(i, t[i], a, c, d);
        if (d[i] == 0)
            continue;
        pos = findCheapest(a, pq);
        while (pos != 0 && d[i] != 0) {
            cost[i] += calculateCost(i, pos, a, c, d);
            pos = findCheapest(a, pq);
        }
        if (d[i] != 0)
            cost[i] = 0;
    }

    return cost;
}

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

    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    vector<long long> cost;

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

    cost = foodOrdering(n, m, a, c, t, d);
    for (int i = 1; i <= m; i++)
        printf("%lld\n", cost[i]);

    return 0;
}


// Problem Code: B

#include <bits/stdc++.h>

using namespace std;

int findCheapest(int n, vector<int> a, vector<int> c) {
    int min, pos;
    pos = -1;
    min = numeric_limits<int>::max();
    for (int i = 1; i <= n; i++)
        if (c[i] < min && a[i] != 0) {
            min = c[i];
            pos = i;
        }
    return pos;
}

long long calculateCost(int u, long long &r, vector<int> &a, vector<int> c, vector<int> t, vector<int> &d) {
    long long cost;
    if (d[u] <= a[t[u]]) {
        a[t[u]] -= d[u];
        r -= (long long) d[u];
        cost = (long long) d[u] * c[t[u]];
        d[u] = 0;
    } else {
        d[u] -= a[t[u]];
        r -= (long long) a[t[u]];
        cost = (long long) a[t[u]] * c[t[u]];
        a[t[u]] = 0;
    }
    return cost;
}

long long calculateCheapCost(int u, int v, long long &r, vector<int> &a, vector<int> c, vector<int> t, vector<int> &d) {
    long long cost;
    if (d[u] <= a[v]) {
        a[v] -= d[u];
        r -= (long long) d[u];
        cost = (long long) d[u] * c[v];
        d[u] = 0;
    } else {
        d[u] -= a[v];
        r -= (long long) a[v];
        cost = (long long) a[v] * c[v];
        a[v] = 0;
    }
    return cost;
}

void foodOrdering(int n, int m, vector<int> &a, vector<int> c, vector<int> t, vector<int> &d) {
    int pos;
    long long r, cost;
    r = accumulate(a.begin(), a.end(), (long long) 0);
    for (int i = 1; i <= m; i++) {
        cost = calculateCost(i, r, a, c, t, d);
        if (d[i] == 0) {
            cout << cost << endl;
            continue;
        }
        pos = findCheapest(n, a, c);
        while (pos != -1 && r >= d[i] && d[i] != 0) {
            cost += calculateCheapCost(i, pos, r, a, c, t, d);
            pos = findCheapest(n, a, c);
        }
        if (d[i] != 0)
            cost = 0;
        cout << cost << endl;
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= m; i++)
        cin >> t[i] >> d[i];
    foodOrdering(n, m, a, c, t, d);
    return 0;
}


// Problem Code: B

#include <bits/stdc++.h>

using namespace std;

int findCheapest(int n, vector<int> a, vector<int> c) {
    int min, pos;
    pos = 0;
    min = numeric_limits<int>::max();
    for (int i = 1; i <= n; i++)
        if (c[i] < min && a[i] != 0) {
            min = c[i];
            pos = i;
        }
    return pos;
}

void foodOrdering(int n, int m, vector<int> a, vector<int> c, vector<int> t, vector<int> d) {
    int pos;
    long long cost;
    for (int i = 1; i <= m; i++) {
        if (d[i] <= a[t[i]]) {
            a[t[i]] -= d[i];
            cost = (long long) d[i] * c[t[i]];
            d[i] = 0;
        } else {
            d[i] -= a[t[i]];
            cost = (long long) a[t[i]] * c[t[i]];
            a[t[i]] = 0;
            pos = findCheapest(n, a, c);
            while (pos != 0 && d[i] != 0) {
                if (d[i] <= a[pos]) {
                    a[pos] -= d[i];
                    cost += (long long) d[i] * c[pos];
                    d[i] = 0;
                } else {
                    d[i] -= a[pos];
                    cost += (long long) a[pos] * c[pos];
                    a[pos] = 0;
                }
                pos = findCheapest(n, a, c);
            }
            if (d[i] != 0)
                cost = 0;
        }
        cout << cost << endl;
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= m; i++)
        cin >> t[i] >> d[i];
    foodOrdering(n, m, a, c, t, d);
    return 0;
}


// Problem Code: 1106/B

#include <bits/stdc++.h>
// #include <vector>
// #include <queue>
// #include <utility>

using namespace std;

typedef priority_queue< pair<int, int>,
                        vector< pair<int, int> >,
                        greater< pair<int, int> > > PQ;

int findCheapest(vector<int> a, PQ &pq) {
    while (!pq.empty() && a[pq.top().second] == 0)
        pq.pop();
    return (pq.empty()) ? 0 : pq.top().second;
}

long long calculateCost(int u, int v, vector<int> &a, vector<int> c, int &d) {
    long long cost;
    if (d <= a[v]) {
        a[v] -= d;
        cost = (long long) d * c[v];
        d = 0;
    } else {
        d -= a[v];
        cost = (long long) a[v] * c[v];
        a[v] = 0;
    }
    return cost;
}

void foodOrdering(int n, int m, vector<int> &a, vector<int> c) {
    int t, d, pos;
    long long cost;
    PQ pq;

    for (int i = 1; i <= n; i++)
        pq.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;
            cost = (long long) d * c[t];
            printf("%lld\n", cost);
        } else {
            d -= a[t];
            cost = (long long) a[t] * c[t];
            a[t] = 0;
            pos = findCheapest(a, pq);
            while (pos != 0 && d != 0) {
                if (d <= a[pos]) {
                    a[pos] -= d;
                    cost += (long long) d * c[pos];
                    d = 0;
                    printf("%lld\n", cost);
                } else {
                    d -= a[pos];
                    cost += (long long) a[pos] * c[pos];
                    a[pos] = 0;
                    pos = findCheapest(a, pq);
                }
            }
            if (d != 0)
                puts("0");
        }
    }
}

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

    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    vector<long long> cost;

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

    foodOrdering(n, m, a, c);
    return 0;
}


// Problem Code: 1106/B

#include <bits/stdc++.h>
// #include <vector>
// #include <queue>
// #include <utility>

using namespace std;

typedef priority_queue< pair<int, int>,
                        vector< pair<int, int> >,
                        greater< pair<int, int> > > PQ;

int findCheapest(vector<int> a, PQ &pq) {
    while (!pq.empty() && a[pq.top().second] == 0)
        pq.pop();
    return (pq.empty()) ? 0 : pq.top().second;
}

long long calculateCost(int u, int v, vector<int> &a, vector<int> c, int &d) {
    long long cost;
    if (d <= a[v]) {
        a[v] -= d;
        cost = (long long) d * c[v];
        d = 0;
    } else {
        d -= a[v];
        cost = (long long) a[v] * c[v];
        a[v] = 0;
    }
    return cost;
}

void foodOrdering(int n, int m, vector<int> &a, vector<int> c) {
    int t, d, pos;
    long long cost;
    PQ pq;

    for (int i = 1; i <= n; i++)
        pq.push(make_pair(c[i], i));

    for (int i = 1; i <= m; i++) {
        scanf("%d %d", &t, &d);
        cost = calculateCost(i, t, a, c, d);
        if (d == 0) {
            printf("%lld\n", cost);
            continue;
        }
        pos = findCheapest(a, pq);
        while (pos != 0 && d != 0) {
            cost += calculateCost(i, pos, a, c, d);
            pos = findCheapest(a, pq);
        }
        if (d != 0)
            cost = 0;
        printf("%lld\n", cost);
    }
}

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

    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    vector<long long> cost;

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

    foodOrdering(n, m, a, c);
    return 0;
}


// Problem Code: 1106/B

#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

typedef priority_queue< pair<int, int>,
                        vector< pair<int, int> >,
                        greater< pair<int, int> > > PQ;

int findCheapest(vector<int> a, PQ pq) {
    int pos = 0;
    pair<int, int> top;
    while (!pq.empty()) {
        top = pq.top();
        pq.pop();
        if (a[top.second] != 0) {
            pos = top.second;
            break;
        }
    }
    return pos;
}

long long calculateCost(int u, int v, vector<int> &a, vector<int> c, vector<int> &d) {
    long long cost;
    if (d[u] <= a[v]) {
        a[v] -= d[u];
        cost = (long long) d[u] * c[v];
        d[u] = 0;
    } else {
        d[u] -= a[v];
        cost = (long long) a[v] * c[v];
        a[v] = 0;
    }
    return cost;
}

vector<long long> foodOrdering(int n, int m, vector<int> &a, vector<int> c, vector<int> t, vector<int> &d) {
    int pos;
    vector<long long> cost(m + 1);
    PQ pq;

    for (int i = 1; i <= n; i++)
        pq.push(make_pair(c[i], i));

    for (int i = 1; i <= m; i++) {
        cost[i] = calculateCost(i, t[i], a, c, d);
        if (d[i] == 0)
            continue;
        pos = findCheapest(a, pq);
        while (pos != 0 && d[i] != 0) {
            cost[i] += calculateCost(i, pos, a, c, d);
            pos = findCheapest(a, pq);
        }
        if (d[i] != 0)
            cost[i] = 0;
    }

    return cost;
}

int main() {
    int n, m;
    cin >> n >> m;

    vector<int> a(n + 1), c(n + 1), t(m + 1), d(m + 1);
    vector<long long> cost;

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

    cost = foodOrdering(n, m, a, c, t, d);
    for (int i = 1; i <= m; i++)
        cout << cost[i] << endl;

    return 0;
}


#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;

//macros_______________________________________________________________________________________________________________________________
#define PI  2*acos(0.0)
#define ll  long long
#define pf  printf
#define sc  scanf
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
#define vi  std::vector<int>
#define pi  pair<int , int>
#define vll vector<long long>
#define pll pair<long long , long long>
#define FOR(i,a,n)    for(int i = a; i < n; i++)
#define TEST(test)  for(int c = 1; c <= test; c++)
//____________________________________________________________________________________________________________________________________
unordered_map<string,bool> mep;

void gen(string& str)
{
    for(int i = 0;i<4;i++)
    {
      for(int j = 0;i<10;j++)
      {
          char ch= str[i];
          str[i] = '0'+j;

          if(mep.find(str) == mep.end())
          {
            mep[str] = 1;
            return;
          }
          str[i] = ch;
      }
    }
}
int main(){

   #ifdef PARTHO
       freopen("C:\\Users\\parth\\Desktop\\input.txt","r",stdin);
       freopen("C:\\Users\\parth\\Desktop\\output.txt","w",stdout);
       int start_time = clock();
   #endif

       int test;

       cin >> test;

       while(test--)
       {
          std::vector<string> vstr;
          string str;
          int n;
          cin >> n;

          for(int i = 0;i<n;i++)
          {
              cin >> str;
              vstr.pb(str);
          }
          sort(vstr.begin(), vstr.end());

          int cnt = 0;

          for(int i = vstr.size()-1;i>=1;i--)
          {
              if(vstr[i] == vstr[i-1] )
              {
                cnt++;
                gen(vstr[i]);
              }
              mep[vstr[i]] = 1;
          }
          mep[vstr[0]] = 1;

          cout << cnt << endl;

          for(auto it : mep)
          {
              cout << it.ff << endl;
          }

          mep.clear();
       }

  #ifdef PARTHO
       int end_time = clock();
       printf("Time = %f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
    #endif
    return 0;
}

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//macros_______________________________________________________________________________________________________________________________
#define PI  2*acos(0.0)
#define ll  long long
#define pf  printf
#define sc  scanf
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
#define vi  std::vector<int>
#define pi  pair<int , int>
#define vll vector<long long>
#define pll pair<long long , long long>
#define FOR(i,a,n)    for(int i = a; i < n; i++)
#define TEST(test)  for(int c = 1; c <= test; c++)
//____________________________________________________________________________________________________________________________________
unordered_map<string,int> mep;

int cnt = 0;
string gen(map<string,int> &mep,string str,int x)
{
  for(int i = 0;i<4;i++)
  {
    char ch = str[i];
    for(int j = 0;j<10;j++)
    {
        str[i] = '0'+j;
        if(mep.find(str) == mep.end())
        {
            cnt++;
            mep[str] = x;
            return str;
        }
        str[i] = ch;
    }
  }

  return str;

}

int main(){

   #ifdef PARTHO
       freopen("C:\\Users\\parth\\Desktop\\input.txt","r",stdin);
       freopen("C:\\Users\\parth\\Desktop\\output.txt","w",stdout);
       int start_time = clock();
   #endif

       int test;

       cin >> test;

       while(test--)
       {

          int n;
          string str;
          map<string,int> mep;

          std::vector<string> v;
          int cnt = 0;
          cin >> n;

          for(int i = 0;i<n;i++)
          {
            cin >> str;
             v.pb(str);
             mep[str] = i;
          }
          std::vector<string> pv;
          for(int i = 0;i<n;i++)
          {
              if(mep[v[i]] != -1)
              {
                pv.pb(v[i]);
                mep[v[i]] = -1;
              }
              else
              {
                pv.pb(gen(mep,v[i],i));
                cnt++;
              }
          }

          cout << cnt << endl;

          for(auto it : pv)
            cout << it << endl;

       }

  #ifdef PARTHO
       int end_time = clock();
       printf("Time = %f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
    #endif
    return 0;
}

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//macros_______________________________________________________________________________________________________________________________
#define PI  2*acos(0.0)
#define ll  long long
#define pf  printf
#define sc  scanf
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
#define vi  std::vector<int>
#define pi  pair<int , int>
#define vll vector<long long>
#define pll pair<long long , long long>
#define FOR(i,a,n)    for(int i = a; i < n; i++)
#define TEST(test)  for(int c = 1; c <= test; c++)
//____________________________________________________________________________________________________________________________________
unordered_map<string,int> mep;

int cnt = 0;
void gen(pair<string,int> str)
{

    for(int i = 0;i<4;i++)
    {
      for(int j = 0;i<10;j++)
      {
          char ch = str.ff[i];
          str.ff[i] = '0'+j;

          if(mep.find(str.ff) == mep.end())
          {
            cnt++;
            mep[str.ff] = str.ss;
            return;
          }
          str.ff[i] = ch;
      }
    }
}

bool compFunctor(pair<string,int> a,pair<string,int> b)
{
    return a.ss<b.ss;
}

bool cmp(pair<string,int> a,pair<string,int> b)
{
  return a.ff<b.ff;
}
int main(){

   #ifdef PARTHO
       freopen("C:\\Users\\parth\\Desktop\\input.txt","r",stdin);
       freopen("C:\\Users\\parth\\Desktop\\output.txt","w",stdout);
       int start_time = clock();
   #endif

       int test;

       cin >> test;

       while(test--)
       {
          cnt = 0;
          std::vector< pair<string,int> > vstr;
          string str;
          int n;
          cin >> n;

          for(int i = 0;i<n;i++)
          {
              cin >> str;
              vstr.pb({str,i});
          }
          sort(vstr.begin(), vstr.end());

          for(int i = vstr.size()-1;i>=1;i--)
          {
              if(mep.find(vstr[i].ff) == mep.end())
              {
                mep[vstr[i].ff] = vstr[i].ss;
                continue;
              }
              if(vstr[i].ff == vstr[i-1].ff )
              {
                gen(vstr[i]);
              }
          }

          gen(vstr[0]);

            vector<pair<string, int>> vec;

  // copy key-value pairs from the map to the vector

          for (auto it2=mep.begin(); it2!=mep.end(); it2++)
          {
            vec.push_back(make_pair(it2->first, it2->second));
          }

          // // sort the vector by increasing order of its pair's second value
          sort(vec.begin(), vec.end(),compFunctor);

         cout << cnt << endl;
          for (int i = 0; i < vec.size(); i++)
          {
            cout << vec[i].first << endl;
          }

          mep.clear();
       }

  #ifdef PARTHO
       int end_time = clock();
       printf("Time = %f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
    #endif
    return 0;
}

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        pair<string,int> arr[n];
        forn(i,n) cin >> arr[i].first;
        forn(i,n) arr[i].second = i;
        sort ( arr, arr+n );
        int ans = 0;
        forn(i,n-1){
            if ( arr[i].first==arr[i+1].first ){
                string tmp = arr[i].first;
                tmp[3] = '0';
                if ( i && arr[i-1].first>=tmp ) tmp[3] = arr[i-1].first[3]+1;
                if ( tmp==arr[i+1].first ) continue;
                ans++;
                arr[i].first = tmp;
            }
        }
        if ( n>=2 && arr[n-1].first<=arr[n-2].first ){
            ans++;
            arr[n-1].first = arr[n-2].first;
            arr[n-1].first[3]++;
        }
        cout << ans << edl;
        string ansr[n];
        forn(i,n) ansr[ arr[i].second ] = arr[i].first;
        forn(i,n) cout << ansr[i] << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        pair<string,int> arr[n];
        forn(i,n) cin >> arr[i].first;
        forn(i,n) arr[i].second = i;
        sort ( arr, arr+n );
        int ans = 0;
        forn(i,n-1){
            if ( arr[i].first==arr[i+1].first ){
                string tmp = arr[i].first;
                tmp[3] = '0';
                if ( i && arr[i-1].first>=arr[i-1].first ) tmp[3] = arr[i-1].first[3]+1;
                if ( tmp==arr[i+1].first ) continue;
                ans++;
                arr[i].first = tmp;
            }
        }
        cout << ans << edl;
        string ansr[n];
        forn(i,n) ansr[ arr[i].second ] = arr[i].first;
        forn(i,n) cout << ansr[i] << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        pair<string,int> arr[n];
        forn(i,n) cin >> arr[i].first;
        forn(i,n) arr[i].second = i;
        sort ( arr, arr+n );
        int ans = 0;
        forn(i,n-1){
            if ( arr[i].first==arr[i+1].first ){
                string tmp = arr[i].first;
                tmp[3] = '0';
                if ( i && arr[i-1].first>=tmp ) tmp[3] = arr[i-1].first[3]+1;
                if ( tmp==arr[i+1].first ) continue;
                ans++;
                arr[i].first = tmp;
            }
        }
        cout << ans << edl;
        string ansr[n];
        forn(i,n) ansr[ arr[i].second ] = arr[i].first;
        forn(i,n) cout << ansr[i] << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        pair<string,int> arr[n];
        forn(i,n) cin >> arr[i].first;
        forn(i,n) arr[i].second = i;
        sort ( arr, arr+n );
        int ans = 0;
        forn(i,n-1){
            if ( arr[i].first==arr[i+1].first ){
                string tmp = arr[i].first;
                tmp[3] = '0';
                if ( i && arr[i-1].first>=tmp ) tmp[3] = arr[i-1].first[3]+1;
                if ( tmp==arr[i+1].first ) continue;
                ans++;
                arr[i].first = tmp;
            }
            if ( i && arr[i].first<=arr[i-1].first ){
                ans++;
                arr[i].first = arr[i-1].first;
                arr[i].first[3]++;
            }
        }
        if ( n>=2 && arr[n-1].first<=arr[n-2].first ){
            arr[n-1].first = arr[n-2].first;
            arr[n-1].first[3]++;
        }
        cout << ans << edl;
        string ansr[n];
        forn(i,n) ansr[ arr[i].second ] = arr[i].first;
        forn(i,n) cout << ansr[i] << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        string arr[n];
        forn(i,n) cin >> arr[i];
        sort ( arr, arr+n );
        int ans = 0;
        forn(i,n-1){
            if ( arr[i]==arr[i+1] ){
                string tmp = arr[i];
                tmp[3] = '0';
                if ( i && arr[i-1]>=arr[i-1] ) tmp[3] = arr[i-1][3]+1;
                if ( tmp==arr[i+1] ) continue;
                ans++;
                arr[i] = tmp;
            }
        }
        cout << ans << edl;
        forn(i,n) cout << arr[i] << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        vector<int> ans;
        ans.pb(0);
        int bs,nx;
        for ( int i=1; i*i<=n; i++ ){
            bs = n/i;
            if ( n/bs==i ) ans.pb(i);
            else if ( n/(bs+1)==i ) ans.pb(i);
        }
        if ( ans[ans.size()-1] != n ) ans.pb(n);
        cout << ans.size() << edl;
        forn(i,ans.size()){
            cout << ans[i] << " ";
        }
        cout << edl;

    }

}


#include <bits/stdc++.h>

using namespace std;

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

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())

#define edl '\n'
#define ll long long int

#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')

#define filecode ifstream cin("input.txt");ofstream cout("output.txt");
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;

#define MOD 1000000007
#define PI 3.14159265

int main() {

    IOFAST;
    int t;
    cin >> t;
    while ( t-- ){

        int n;
        cin >> n;
        string arr[n];
        forn(i,n) cin >> arr[i];
        map<string,int> mp;
        forn(i,n) mp[arr[i]]++;
        int ans = 0;
        forn(i,n){
            string tmp = arr[i];
            if ( mp[arr[i]]==1 ) continue;
            mp[tmp]--;
            tmp[3] = '0';
            ans++;
            forn(j,10){
                if ( mp.find(tmp)==mp.end() ) break;
                tmp[3]++;
            }
            mp[tmp] = 1;
            arr[i] = tmp;
        }
        cout << ans << edl;
        forn(i,n) cout << arr[i] << edl;

    }

}


//code by Piyush Anand
#include <bits/stdc++.h>
using namespace std;
typedef long long  int ll;
#define PI 3.14159265
#define pii pair<ll,ll>
#define ff first
#define ss second
#define mm " "
#define frev for(ll i=n-1;i>=0;i--)
#define inloop for(ll i=0;i<n;i++)cin>>a[i];
#define fa(x,v) for(auto x:v)
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define f1 for(ll i=1;i<=n;i++)
#define VP vector< pii >
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define pb push_back
#define pr cout<<'\n'
 const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
#define vec vector<ll>
#define vecs vector<string>
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}

ll strTo(string s)
{
    stringstream tmp(s);
    ll x = 0;
    tmp >> x;

return x;
}

string NoTo(ll n)
{
    ostringstream str1;
    str1 << n;
    string ans = str1.str();
    return ans;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

  ll lcm(ll a, ll b)
 {
    return (a*b)/gcd(a, b);
 }

ll arrlcm(ll arr[], ll n)
{
    // Initialize result
    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]
    // after i'th iteration,
    for (ll i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}

ll arrgcd(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

ll nCr(ll n, ll r)
{
    ll ans=1;
    f(i,0,r)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;

}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

vec v;
void Divisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {

            if (n/i == i)
               v.pb(i);
            else
                {
                    v.pb(i);
                    v.pb(n/i);
                }
        }

    }
}

ll no_of_divisors(ll n){
 ll c = 0;
 for (ll i = 1; i <= sqrt(n); i++) {
  if (n % i == 0){
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c;
}

 ll pwrn(ll x,ll y){
 if (y == 0)
  return 1;
 else if (y % 2 == 0)
  return pwrn(x, y / 2) * pwrn(x, y / 2);
 else
  return x * pwrn(x, y / 2) * pwrn(x, y / 2);
}

 ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

ll digits(ll n)
{
    ll x = floor(log10(n)) + 1;
    return x;
}

vec prf;
void primeFactors(ll n)
{
    if(n%2==0)
    prf.pb(2);
    while (n % 2 == 0)
    {
      //  cout << 2 << " ";
        n = n/2;
    }

    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n

        if(n%i==0)
        prf.pb(i);
        while (n % i == 0)
        {
          //  cout << i << " ";
            n = n/i;
        }
    }
    if (n > 1)
        prf.pb(n);
}

int main()
{

    fast;

   test{

       ll n;
       cin>>n;
       map<string,ll> m;
       vector <string> v;
       string tmp;
       f(i,0,n)
       {
           cin>>tmp;
           v.pb(tmp);
           m[tmp]++;
       }
       ll ans=0;
       for(ll i=0;i<v.size();i++)
       {
           if(m[v[i]]>1)
           {

                   string tmp=v[i];
                   string tmp1=v[i];
                   m[v[i]]--;
                   ll c1=0,c2=0;
                   char ch1=tmp[3];
                   while(m.find(tmp)!=m.end()&&tmp[3]<57)
                   {
                       c1++;
                         tmp[3]=(char)(tmp[3]+c1);
                         // tmp.pop_back();

                   }
                    char ch=tmp[3];
                    while(m.find(tmp1)!=m.end()&&tmp1[3]>48)
                   {
                        c2++;
                         tmp1[3]=(char)(ch-c2);

                       //  cout<<tmp[3]<<"\n";

                   }
                  if(c1>0){
                  m[tmp]++;
                  v[i]=tmp;}
                  else{
                  m[tmp1]++;
                  v[i]=tmp1;}
          //  cout<<tmp1<<'\n';
                    ans+=1;

           }
       }
       cout<<ans<<"\n";
       f(i,0,v.size())
       cout<<v[i]<<"\n";

   }

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
   return 0;

}

/*

   ll n;
        cin>>n;
       string a,b;
       cin>>a>>b;
       if(a==b)
       cout<<"0\n";
       else
       {
           vec v;

           rf(i,n-1,0)
           {
               if(a[i]!=b[i]){
                 //  cout<<i<<endl;
               if(b[i]==a[0]){
               // v.pb(1);
                v.pb(1);
                v.pb(i+1);
                if(a[0]=='1')
                a[0]='0';
                else
                a[0]='1';

                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);

                }
                else
                {
                   // v.pb(1);
                    v.pb(i+1);
                  //  a[0]=b[i];
                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);
                }

               }

           }
           cout<<v.size()<<" ";
           for(auto it: v)
           cout<<it<<" ";
           cout<<"\n";
       }

*/


//code by Piyush Anand
#include <bits/stdc++.h>
using namespace std;
typedef long long  int ll;
#define PI 3.14159265
#define pii pair<ll,ll>
#define ff first
#define ss second
#define mm " "
#define frev for(ll i=n-1;i>=0;i--)
#define inloop for(ll i=0;i<n;i++)cin>>a[i];
#define fa(x,v) for(auto x:v)
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define f1 for(ll i=1;i<=n;i++)
#define VP vector< pii >
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define pb push_back
#define pr cout<<'\n'
 const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
#define vec vector<ll>
#define vecs vector<string>
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}

ll strTo(string s)
{
    stringstream tmp(s);
    ll x = 0;
    tmp >> x;

return x;
}

string NoTo(ll n)
{
    ostringstream str1;
    str1 << n;
    string ans = str1.str();
    return ans;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

  ll lcm(ll a, ll b)
 {
    return (a*b)/gcd(a, b);
 }

ll arrlcm(ll arr[], ll n)
{
    // Initialize result
    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]
    // after i'th iteration,
    for (ll i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}

ll arrgcd(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

ll nCr(ll n, ll r)
{
    ll ans=1;
    f(i,0,r)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;

}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

vec v;
void Divisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {

            if (n/i == i)
               v.pb(i);
            else
                {
                    v.pb(i);
                    v.pb(n/i);
                }
        }

    }
}

ll no_of_divisors(ll n){
 ll c = 0;
 for (ll i = 1; i <= sqrt(n); i++) {
  if (n % i == 0){
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c;
}

 ll pwrn(ll x,ll y){
 if (y == 0)
  return 1;
 else if (y % 2 == 0)
  return pwrn(x, y / 2) * pwrn(x, y / 2);
 else
  return x * pwrn(x, y / 2) * pwrn(x, y / 2);
}

 ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

ll digits(ll n)
{
    ll x = floor(log10(n)) + 1;
    return x;
}

vec prf;
void primeFactors(ll n)
{
    if(n%2==0)
    prf.pb(2);
    while (n % 2 == 0)
    {
      //  cout << 2 << " ";
        n = n/2;
    }

    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n

        if(n%i==0)
        prf.pb(i);
        while (n % i == 0)
        {
          //  cout << i << " ";
            n = n/i;
        }
    }
    if (n > 1)
        prf.pb(n);
}

int main()
{

    fast;

   test{

       ll n;
       cin>>n;
       map<string,ll> m;
       vector <string> v;
       string tmp;
       f(i,0,n)
       {
           cin>>tmp;
           v.pb(tmp);
           m[tmp]++;
       }
       ll ans=0;
       for(ll i=0;i<v.size();i++)
       {
           if(m[v[i]]>1)
           {

                   string tmp=v[i];
                   string tmp1=v[i];
                   m[v[i]]--;
                   ll c1=0,c2=0;
                   char ch1='0';
                   while(m.find(tmp)!=m.end())
                   {

                        tmp[3]=ch1+c1;
                        c1++;
                         // tmp.pop_back();

                   }

                  m[tmp]++;
                  v[i]=tmp;
                    ans+=1;

           }
       }
       cout<<ans<<"\n";
       f(i,0,v.size())
       cout<<v[i]<<"\n";

   }

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
   return 0;

}

/*

   ll n;
        cin>>n;
       string a,b;
       cin>>a>>b;
       if(a==b)
       cout<<"0\n";
       else
       {
           vec v;

           rf(i,n-1,0)
           {
               if(a[i]!=b[i]){
                 //  cout<<i<<endl;
               if(b[i]==a[0]){
               // v.pb(1);
                v.pb(1);
                v.pb(i+1);
                if(a[0]=='1')
                a[0]='0';
                else
                a[0]='1';

                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);

                }
                else
                {
                   // v.pb(1);
                    v.pb(i+1);
                  //  a[0]=b[i];
                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);
                }

               }

           }
           cout<<v.size()<<" ";
           for(auto it: v)
           cout<<it<<" ";
           cout<<"\n";
       }

*/


//code by Piyush Anand
#include <bits/stdc++.h>
using namespace std;
typedef long long  int ll;
#define PI 3.14159265
#define pii pair<ll,ll>
#define ff first
#define ss second
#define mm " "
#define frev for(ll i=n-1;i>=0;i--)
#define inloop for(ll i=0;i<n;i++)cin>>a[i];
#define fa(x,v) for(auto x:v)
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define f1 for(ll i=1;i<=n;i++)
#define VP vector< pii >
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define pb push_back
#define pr cout<<'\n'
 const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
#define vec vector<ll>
#define vecs vector<string>
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}

ll strTo(string s)
{
    stringstream tmp(s);
    ll x = 0;
    tmp >> x;

return x;
}

string NoTo(ll n)
{
    ostringstream str1;
    str1 << n;
    string ans = str1.str();
    return ans;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

  ll lcm(ll a, ll b)
 {
    return (a*b)/gcd(a, b);
 }

ll arrlcm(ll arr[], ll n)
{
    // Initialize result
    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]
    // after i'th iteration,
    for (ll i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}

ll arrgcd(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

ll nCr(ll n, ll r)
{
    ll ans=1;
    f(i,0,r)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;

}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

vec v;
void Divisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {

            if (n/i == i)
               v.pb(i);
            else
                {
                    v.pb(i);
                    v.pb(n/i);
                }
        }

    }
}

ll no_of_divisors(ll n){
 ll c = 0;
 for (ll i = 1; i <= sqrt(n); i++) {
  if (n % i == 0){
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c;
}

 ll pwrn(ll x,ll y){
 if (y == 0)
  return 1;
 else if (y % 2 == 0)
  return pwrn(x, y / 2) * pwrn(x, y / 2);
 else
  return x * pwrn(x, y / 2) * pwrn(x, y / 2);
}

 ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

ll digits(ll n)
{
    ll x = floor(log10(n)) + 1;
    return x;
}

vec prf;
void primeFactors(ll n)
{
    if(n%2==0)
    prf.pb(2);
    while (n % 2 == 0)
    {
      //  cout << 2 << " ";
        n = n/2;
    }

    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n

        if(n%i==0)
        prf.pb(i);
        while (n % i == 0)
        {
          //  cout << i << " ";
            n = n/i;
        }
    }
    if (n > 1)
        prf.pb(n);
}

int main()
{

    fast;

   test{

       ll n;
       cin>>n;
       map<string,ll> m;
       vector <string> v;
       string tmp;
       f(i,0,n)
       {
           cin>>tmp;
           v.pb(tmp);
           m[tmp]++;
       }
       ll ans=0;
       for(ll i=0;i<v.size();i++)
       {
           if(m[v[i]]>1)
           {
               f(j,0,m[v[i]]-1)
               {
                   string tmp=v[i];
                   string tmp1=v[i];
                   m[v[i]]--;
                   ll c1=0,c2=0;
                   while(m.find(tmp)!=m.end()&&tmp[3]!=9)
                   {
                       c1++;
                       tmp.pop_back();
                         tmp+=(char)(tmp[3]+c1+48);

                   }

                    while(m.find(tmp1)!=m.end()&&tmp1[3]!=0)
                   {
                        c2++;
                        tmp1.pop_back();
                         tmp1+=(char)(tmp1[3]-c2+48);

                   }
                  if(c1>0){
                  m[tmp]++;
                  v[i]=tmp;}
                  else{
                  m[tmp1]++;
                  v[i]=tmp1;}
          //  cout<<tmp1<<'\n';
                    ans+=1;
               }
           }
       }
       cout<<ans<<"\n";
       f(i,0,v.size())
       cout<<v[i]<<"\n";

   }

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
   return 0;

}

/*

   ll n;
        cin>>n;
       string a,b;
       cin>>a>>b;
       if(a==b)
       cout<<"0\n";
       else
       {
           vec v;

           rf(i,n-1,0)
           {
               if(a[i]!=b[i]){
                 //  cout<<i<<endl;
               if(b[i]==a[0]){
               // v.pb(1);
                v.pb(1);
                v.pb(i+1);
                if(a[0]=='1')
                a[0]='0';
                else
                a[0]='1';

                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);

                }
                else
                {
                   // v.pb(1);
                    v.pb(i+1);
                  //  a[0]=b[i];
                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);
                }

               }

           }
           cout<<v.size()<<" ";
           for(auto it: v)
           cout<<it<<" ";
           cout<<"\n";
       }

*/


//code by Piyush Anand
#include <bits/stdc++.h>
using namespace std;
typedef long long  int ll;
#define PI 3.14159265
#define pii pair<ll,ll>
#define ff first
#define ss second
#define mm " "
#define frev for(ll i=n-1;i>=0;i--)
#define inloop for(ll i=0;i<n;i++)cin>>a[i];
#define fa(x,v) for(auto x:v)
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define f1 for(ll i=1;i<=n;i++)
#define VP vector< pii >
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define pb push_back
#define pr cout<<'\n'
 const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
#define vec vector<ll>
#define vecs vector<string>
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}

ll strTo(string s)
{
    stringstream tmp(s);
    ll x = 0;
    tmp >> x;

return x;
}

string NoTo(ll n)
{
    ostringstream str1;
    str1 << n;
    string ans = str1.str();
    return ans;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

  ll lcm(ll a, ll b)
 {
    return (a*b)/gcd(a, b);
 }

ll arrlcm(ll arr[], ll n)
{
    // Initialize result
    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]
    // after i'th iteration,
    for (ll i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}

ll arrgcd(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

ll nCr(ll n, ll r)
{
    ll ans=1;
    f(i,0,r)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;

}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

vec v;
void Divisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {

            if (n/i == i)
               v.pb(i);
            else
                {
                    v.pb(i);
                    v.pb(n/i);
                }
        }

    }
}

ll no_of_divisors(ll n){
 ll c = 0;
 for (ll i = 1; i <= sqrt(n); i++) {
  if (n % i == 0){
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c;
}

 ll pwrn(ll x,ll y){
 if (y == 0)
  return 1;
 else if (y % 2 == 0)
  return pwrn(x, y / 2) * pwrn(x, y / 2);
 else
  return x * pwrn(x, y / 2) * pwrn(x, y / 2);
}

 ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

ll digits(ll n)
{
    ll x = floor(log10(n)) + 1;
    return x;
}

vec prf;
void primeFactors(ll n)
{
    if(n%2==0)
    prf.pb(2);
    while (n % 2 == 0)
    {
      //  cout << 2 << " ";
        n = n/2;
    }

    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n

        if(n%i==0)
        prf.pb(i);
        while (n % i == 0)
        {
          //  cout << i << " ";
            n = n/i;
        }
    }
    if (n > 1)
        prf.pb(n);
}

int main()
{

    fast;

   test{
       ll n;
       cin>>n;
       map<string,ll> m;
       string tmp;
       f(i,0,n)
       {
           cin>>tmp;
           m[tmp]++;
       }
       ll ans=0;
       for(auto it: m)
       {
           if(it.second>1)
           {
               f(j,0,it.second-1)
               {
                   string tmp=it.first;
                   string tmp1=it.first;
                   ll c1=0,c2=0;
                   while(m.find(tmp)!=m.end()&&tmp[3]!=9)
                   {
                       c1++;
                       tmp.pop_back();
                         tmp+=(char)(tmp[3]+c1+48);

                   }

                    while(m.find(tmp1)!=m.end()&&tmp1[3]!=0)
                   {
                        c2++;
                        tmp1.pop_back();
                         tmp1+=(char)(tmp1[3]-c2+48);

                   }
                  if(c1>0)
                  m[tmp]++;
                  else
                  m[tmp1]++;
          //  cout<<tmp1<<'\n';
                    ans+=1;
               }
           }
       }
       cout<<ans<<"\n";
       for(auto it: m)
       cout<<it.first<<"\n";
   }

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
   return 0;

}

/*

   ll n;
        cin>>n;
       string a,b;
       cin>>a>>b;
       if(a==b)
       cout<<"0\n";
       else
       {
           vec v;

           rf(i,n-1,0)
           {
               if(a[i]!=b[i]){
                 //  cout<<i<<endl;
               if(b[i]==a[0]){
               // v.pb(1);
                v.pb(1);
                v.pb(i+1);
                if(a[0]=='1')
                a[0]='0';
                else
                a[0]='1';

                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);

                }
                else
                {
                   // v.pb(1);
                    v.pb(i+1);
                  //  a[0]=b[i];
                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);
                }

               }

           }
           cout<<v.size()<<" ";
           for(auto it: v)
           cout<<it<<" ";
           cout<<"\n";
       }

*/


//code by Piyush Anand
#include <bits/stdc++.h>
using namespace std;
typedef long long  int ll;
#define PI 3.14159265
#define pii pair<ll,ll>
#define ff first
#define ss second
#define mm " "
#define frev for(ll i=n-1;i>=0;i--)
#define inloop for(ll i=0;i<n;i++)cin>>a[i];
#define fa(x,v) for(auto x:v)
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define f1 for(ll i=1;i<=n;i++)
#define VP vector< pii >
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define pb push_back
#define pr cout<<'\n'
 const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
#define vec vector<ll>
#define vecs vector<string>
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}

ll strTo(string s)
{
    stringstream tmp(s);
    ll x = 0;
    tmp >> x;

return x;
}

string NoTo(ll n)
{
    ostringstream str1;
    str1 << n;
    string ans = str1.str();
    return ans;
}

vector<bool>sieve(1000000,true);
void Sieve(){
 sieve[0]=false;
 sieve[1]=false;
 for(ll i=2;i*i<=1000000;i++){
  if(sieve[i]==true){
   for(ll j=i*i;j<1000000;j+=i)
    sieve[j]=false;
  }
 }
}

  ll lcm(ll a, ll b)
 {
    return (a*b)/gcd(a, b);
 }

ll arrlcm(ll arr[], ll n)
{
    // Initialize result
    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]
    // after i'th iteration,
    for (ll i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}

ll arrgcd(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

ll nCr(ll n, ll r)
{
    ll ans=1;
    f(i,0,r)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;

}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    return n * fact(n - 1);
}

vec v;
void Divisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {

            if (n/i == i)
               v.pb(i);
            else
                {
                    v.pb(i);
                    v.pb(n/i);
                }
        }

    }
}

ll no_of_divisors(ll n){
 ll c = 0;
 for (ll i = 1; i <= sqrt(n); i++) {
  if (n % i == 0){
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c;
}

 ll pwrn(ll x,ll y){
 if (y == 0)
  return 1;
 else if (y % 2 == 0)
  return pwrn(x, y / 2) * pwrn(x, y / 2);
 else
  return x * pwrn(x, y / 2) * pwrn(x, y / 2);
}

 ll pwr(ll a, ll b){
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a;
  a*=a;
  b>>=1;
 }
 return res;
}

ll pwrm(ll a, ll b, ll m){
 a%=m;
 ll res=1;
 while(b>0){
  if(b&1)
   res=res*a%m;
  a=a*a%m;
  b>>=1;
 }
 return res;
}

ll digits(ll n)
{
    ll x = floor(log10(n)) + 1;
    return x;
}

vec prf;
void primeFactors(ll n)
{
    if(n%2==0)
    prf.pb(2);
    while (n % 2 == 0)
    {
      //  cout << 2 << " ";
        n = n/2;
    }

    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n

        if(n%i==0)
        prf.pb(i);
        while (n % i == 0)
        {
          //  cout << i << " ";
            n = n/i;
        }
    }
    if (n > 1)
        prf.pb(n);
}

int main()
{

    fast;

   test{

       ll n;
       cin>>n;
       map<string,ll> m;
       vector <string> v;
       string tmp;
       f(i,0,n)
       {
           cin>>tmp;
           v.pb(tmp);
           m[tmp]++;
       }
       ll ans=0;
       for(ll i=0;i<v.size();i++)
       {
           if(m[v[i]]>1)
           {

                   string tmp=v[i];
                   string tmp1=v[i];
                   m[v[i]]--;
                   ll c1=0,c2=0;
                   char ch1=tmp[3];
                   while(m.find(tmp)!=m.end()&&tmp[3]<57)
                   {
                       c1++;
                         tmp[3]=ch1+c1;
                         // tmp.pop_back();

                   }
                    char ch=tmp[3];
                    while(m.find(tmp1)!=m.end()&&tmp1[3]>48)
                   {
                        c2++;
                         tmp1[3]=ch+c2;

                       //  cout<<tmp[3]<<"\n";

                   }
                  if(c1>0){
                  m[tmp]++;
                  v[i]=tmp;}
                  else{
                  m[tmp1]++;
                  v[i]=tmp1;}
          //  cout<<tmp1<<'\n';
                    ans+=1;

           }
       }
       cout<<ans<<"\n";
       f(i,0,v.size())
       cout<<v[i]<<"\n";

   }

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
   return 0;

}

/*

   ll n;
        cin>>n;
       string a,b;
       cin>>a>>b;
       if(a==b)
       cout<<"0\n";
       else
       {
           vec v;

           rf(i,n-1,0)
           {
               if(a[i]!=b[i]){
                 //  cout<<i<<endl;
               if(b[i]==a[0]){
               // v.pb(1);
                v.pb(1);
                v.pb(i+1);
                if(a[0]=='1')
                a[0]='0';
                else
                a[0]='1';

                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);

                }
                else
                {
                   // v.pb(1);
                    v.pb(i+1);
                  //  a[0]=b[i];
                f(j,0,i+1)
                {
                    if(a[j]=='1')
                    a[j]='0';
                    else
                    a[j]='1';
                }
                f(j,0,i/2+1)
                swap(a[j],a[i-j]);
                }

               }

           }
           cout<<v.size()<<" ";
           for(auto it: v)
           cout<<it<<" ";
           cout<<"\n";
       }

*/