#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
pair<int, int> p[N];
int a[N], ind[N], match[N];
map<int, int> mp;
vector<int> v[N];
bitset<N> viz;
bool cmp(int i, int j) { return p[i].first > p[j].first; }
bool dfs(int x) {
  viz[x] = 1;
  for (int u : v[x]) {
    if (match[u] == -1 || (!viz[match[u]] && dfs(match[u]))) {
      match[u] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    mp[p[i].second] = i;
    ind[i] = i;
  }
  sort(ind, ind + n, cmp);
  cin >> m;
  for (int i = 0; i < m; i++) {
    int l, d;
    cin >> d >> l;
    if (mp.count(l)) {
      if (p[mp[l]].first <= d) {
        v[mp[l]].emplace_back(i);
      }
    }
    if (mp.count(l + 1)) {
      if (p[mp[l + 1]].first <= d) {
        v[mp[l + 1]].emplace_back(i);
      }
    }
  }
  long long int ans = 0;
  memset(match, -1, sizeof(match));
  viz.reset();
  for (int i = 0; i < n; i++) {
    if (dfs(ind[i])) {
      viz.reset();
      ans += (long long int)p[ind[i]].first;
    }
  }
  vector<pair<int, int>> ans2;
  cout << ans << '\n';
  ;
  for (int i = 0; i < m; i++) {
    if (match[i] != -1) ans2.emplace_back(make_pair(match[i] + 1, i + 1));
  }
  cout << (int)ans2.size() << '\n';
  for (auto u : ans2) cout << u.second << ' ' << u.first << '\n';
}
