#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long pot(long long a, int wyk) {
  long long ret = 1LL;
  while (wyk > 0) {
    if (wyk % 2 == 1) ret = a * ret % mod;
    a = a * a % mod;
    wyk /= 2;
  }
  return ret;
}
const int L = 61;
const int t = 20;
const int T = 1 << t;
vector<long long> A[2][L], Aux[2][L];
vector<pair<long long, long long> > dzialy;
void przelicz(int lvl, vector<long long> &w1, vector<long long> &w2) {
  for (auto pp : w1)
    for (auto q : w2) {
      auto p = pp;
      int l = lvl;
      p -= p & ((1LL << l) - 1);
      q -= q & ((1LL << l) - 1);
      long long pref = p ^ q;
      dzialy.push_back(make_pair(pref, pref + (1LL << l)));
    }
}
int main() {
  long long dwa = pot(2, mod - 2);
  for (int k = 0; k < 2; k++) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      long long a, b;
      scanf("%lld %lld", &a, &b);
      while (a <= b) {
        int l = 59;
        for (; l > 0; l--)
          if ((a & ((1LL << l) - 1)) == 0LL) {
            if (a + (1LL << l) <= b + 1LL) break;
          }
        A[k][l].push_back(a);
        a += (1LL << l);
      }
    }
    for (int i = 0; i < 60; i++) {
      if (!A[k][i].empty()) {
        sort(A[k][i].begin(), A[k][i].end());
        vector<long long> rob;
        rob.push_back(A[k][i][0]);
        for (int j = 1; j < (int)A[k][i].size(); j++)
          if (A[k][i][j - 1] < A[k][i][j]) {
            rob.push_back(A[k][i][j]);
          }
        A[k][i] = rob;
      }
      for (long long x : A[k][i]) {
        for (int j = i + 1; j < 60; j++) {
          long long pref = x;
          pref -= x & ((1LL << j) - 1);
          Aux[k][j].push_back(pref);
        }
      }
      if (!Aux[k][i].empty()) {
        sort(Aux[k][i].begin(), Aux[k][i].end());
        vector<long long> rob;
        rob.push_back(Aux[k][i][0]);
        for (int j = 1; j < (int)Aux[k][i].size(); j++)
          if (Aux[k][i][j - 1] < Aux[k][i][j]) {
            rob.push_back(Aux[k][i][j]);
          }
        Aux[k][i] = rob;
      }
    }
  }
  dzialy.reserve(1000000);
  for (int l = 0; l < 60; l++) {
    przelicz(l, A[1][l], Aux[0][l]);
    przelicz(l, A[0][l], Aux[1][l]);
    przelicz(l, A[0][l], A[1][l]);
  }
  sort(dzialy.begin(), dzialy.end());
  long long res = 0LL, from = 0LL;
  for (auto p : dzialy) {
    p.first = max(p.first, from);
    if (p.second > p.first) {
      res += (p.first + p.second - 1LL) % mod * ((p.second - p.first) % mod) %
             mod * dwa % mod;
      res %= mod;
    }
    from = max(from, p.second);
  }
  printf("%lld\n", res);
}
