#include <bits/stdc++.h>   using namespace std;   struct deal {     long num_person;     long num_shoue;     vector<deal> *d;       public:     deal(long _p, long _s, vector<deal> *_d) {       num_person = _p;       num_shoue = _s;       d = _d;     }     deal(long _p, long _s) {       num_person = _p;       num_shoue = _s;       d = NULL;     }   };   long n, m;   vector<vector<long long>> d1;   map<long, vector<long long>> people;   map<long, vector<long>> num_people;   map<long, long long> shoes;   map<long, long> num_shoes;   long long max(long long a, long long b) { return a > b ? a : b; }   int main() {     scanf( %d , &n);     shoes = map<long, long long>();     num_shoes = map<long, long>();     long size, money;     for (long i = 0; i < n; i++) {       scanf( %d%d , &money, &size);       shoes[size] = money;       num_shoes[size] = i;     }     scanf( %d , &m);     people = map<long, vector<long long>>();     for (long i = 0; i < m; i++) {       scanf( %d%d , &money, &size);       if (!people.count(size)) {         people[size] = vector<long long>(2, 0);         num_people[size] = vector<long>(2, -1);       }       if (people[size][0] < money) {         people[size][1] = people[size][0];         num_people[size][1] = num_people[size][0];         people[size][0] = money;         num_people[size][0] = i;       } else if (people[size][1] < money) {         people[size][1] = money;         num_people[size][1] = i;       }     }     map<long, vector<long long>>::iterator i = people.begin();     long money2;     size = -10;     d1 = vector<vector<long long>>(m, vector<long long>(2, 0));     long k = 0;     vector<deal> *p10 = new vector<deal>();     vector<deal> *p11 = new vector<deal>();     vector<deal> *pp10;     vector<deal> *pp11;     long size10 = 0;     long size11 = 0;     long psize10;     long psize11;     long prev_size;     for (i = people.begin(); i != people.end(); i++) {       psize10 = size10;       psize11 = size11;       pp10 = p10;       pp11 = p11;       p10 = new vector<deal>();       p10->reserve(3);       p11 = new vector<deal>();       p11->reserve(3);       prev_size = size;       size = i->first;       money = i->second[0];       money2 = i->second[1];       if (prev_size + 1 < size) {         p10->push_back(deal(-1, -1, pp11));         p11->push_back(deal(-1, -1, pp11));         size10 = psize11;         size11 = psize11;         if (shoes[size] > 0 && shoes[size] <= money) {           d1[k][0] = shoes[size];           p10->push_back(deal(num_people[size][0], num_shoes[size]));           size10++;         } else           d1[k][0] = 0;         d1[k][1] = 0;         if (shoes[size] > 0 && shoes[size + 1] > 0) {           long cost, cost2;           bool p = 0;           if (shoes[size] > shoes[size + 1]) {             cost = shoes[size];             cost2 = shoes[size + 1];             p = 0;           } else {             cost = shoes[size + 1];             cost2 = shoes[size];             p = 1;           }           if (money >= cost) {             d1[k][1] += cost;             if (!p) {               p11->push_back(deal(num_people[size][0], num_shoes[size]));               size11++;             } else {               p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));               size11++;             }             if (money2 >= cost2) {               d1[k][1] += cost2;               if (!p) {                 p11->push_back(deal(num_people[size][1], num_shoes[size + 1]));                 size11++;               } else {                 p11->push_back(deal(num_people[size][1], num_shoes[size]));                 size11++;               }             }           } else {             if (money >= cost2) {               d1[k][1] += cost2;               if (!p) {                 p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));                 size11++;               } else {                 p11->push_back(deal(num_people[size][0], num_shoes[size]));                 size11++;               }             }           }         } else {           if (shoes[size] > 0 && shoes[size] <= money) {             d1[k][1] = shoes[size];             p11->push_back(deal(num_people[size][0], num_shoes[size]));             size11++;           }           if (shoes[size + 1] > 0 && shoes[size + 1] <= money) {             d1[k][1] = shoes[size + 1];             p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));             size11++;           }         }         if (k != 0) {           d1[k][0] += d1[k - 1][1];           d1[k][1] += d1[k - 1][1];         }       } else {         long cost = 0;         if (shoes[size] > 0 && shoes[size] <= money) cost = shoes[size];         d1[k][0] = max(d1[k - 1][0] + cost, d1[k - 1][1]);         if (d1[k - 1][0] + cost > d1[k - 1][1]) {           p10->push_back(deal(-1, -1, pp10));           if (cost) {             p10->push_back(deal(num_people[size][0], num_shoes[size]));             size10++;           }         } else {           p10->push_back(deal(-1, -1, pp11));         }         cost = 0;         long cost2 = 0;         if (shoes[size] > 0 && shoes[size + 1] > 0) {           if (shoes[size] > shoes[size + 1]) {             if (shoes[size] <= money) {               cost = shoes[size];               if (shoes[size + 1] <= money2) cost2 = shoes[size + 1];             } else {               if (shoes[size + 1] <= money) {                 cost2 = shoes[size + 1];               }             }           } else {             if (shoes[size + 1] <= money) {               cost2 = shoes[size + 1];               if (shoes[size] <= money2) cost = shoes[size];             } else {               if (shoes[size] <= money) {                 cost = shoes[size];               }             }           }         } else {           if (shoes[size] > 0 && shoes[size] <= money) cost = shoes[size];           if (shoes[size + 1] > 0 && shoes[size + 1] <= money)             cost2 = shoes[size + 1];         }         long cost3 = 0;         if (shoes[size + 1] > 0 && shoes[size + 1] <= money)           cost3 = shoes[size + 1];         d1[k][1] = 0;         d1[k][1] = max(d1[k - 1][0] + cost + cost2, d1[k - 1][1] + cost3);         if (d1[k - 1][0] + cost + cost2 > d1[k - 1][1] + cost3) {           p11->push_back(deal(-1, -1, pp10));           if (cost != 0 && cost2 != 0) {             if (cost > cost2) {               p11->push_back(deal(num_people[size][0], num_shoes[size]));               p11->push_back(deal(num_people[size][1], num_shoes[size + 1]));               size11++;               size11++;             } else {               p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));               p11->push_back(deal(num_people[size][1], num_shoes[size]));               size11++;               size11++;             }           } else {             if (cost) {               p11->push_back(deal(num_people[size][0], num_shoes[size]));               size11++;             }             if (cost2) {               p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));               size11++;             }           }         } else {           p11->push_back(deal(-1, -1, pp11));           if (cost3) {             p11->push_back(deal(num_people[size][0], num_shoes[size + 1]));             size11++;           }         }       }       k++;     }     printf( %I64d n , d1[k - 1][1]);     printf( %d n , size11);     vector<deal> *p;     p = p11;     while ((*p).size() != 0) {       for (int j = 1; j < (*p).size(); j++) {         if ((*p)[j].num_person + 1 < 0) break;         printf( %d %d n , (*p)[j].num_person + 1, (*p)[j].num_shoue + 1);       }       p = (*p)[0].d;     }     return 0;   }   