#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 10;
map<int, int> mp;
int c[MX], s[MX], d[MX], l[MX];
vector<int> adj[MX];
bitset<MX> mrk;
int match1[MX], match2[MX];
bool dfs(int v) {
  if (mrk[v]) return 0;
  mrk[v] = 1;
  for (int u : adj[v]) {
    if (!match2[u] || dfs(match2[u])) {
      match1[v] = u;
      match2[u] = v;
      return 1;
    }
  }
  return 0;
}
int main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int> > v;
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> s[i];
    v.push_back(make_pair(c[i], i));
    mp[s[i]] = i;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> d[i] >> l[i];
    if (mp[l[i]] != 0 && d[i] >= c[mp[l[i]]]) adj[mp[l[i]]].push_back(i);
    if (mp[l[i] + 1] != 0 && d[i] >= c[mp[l[i] + 1]])
      adj[mp[l[i] + 1]].push_back(i);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  long long javab = 0;
  int fnd = 1;
  while (fnd) {
    fnd = 0;
    for (int i = 0; i < n; i++) {
      mrk.reset();
      int u = v[i].second;
      if (!mrk[u] && !match1[u] && dfs(u)) {
        javab += c[u];
        fnd = 1;
      }
    }
  }
  vector<pair<int, int> > ans;
  for (int i = 1; i <= n; i++) {
    if (match1[i]) ans.push_back(make_pair(match1[i], i));
  }
  cout << javab << '\n';
  cout << ans.size() << '\n';
  for (auto p : ans) cout << p.first << " " << p.second << '\n';
  return 0;
}
