#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
const int inv2 = 499122177;
const int M = 61;
vector<pair<long long, int> > vec;
vector<long long> x[2][62], y[2][62];
void work(long long l, long long r, long long L, long long R, int d, int o) {
  if (L <= l && r <= R) {
    x[o][d].push_back(l);
    return;
  }
  y[o][d].push_back(l);
  long long mid = (l + r) >> 1;
  if (L <= mid) work(l, mid, L, R, d - 1, o);
  if (R > mid) work(mid + 1, r, L, R, d - 1, o);
  return;
}
int Sum(long long n) {
  n %= Mod;
  return n * (n + 1) % Mod * inv2 % Mod;
}
int Sum(long long l, long long r) {
  return ((r - l) % Mod) * ((r + l - 1) % Mod) % Mod * inv2 % Mod;
}
void Ins(long long l, long long r) {
  vec.push_back(make_pair(l, 1));
  vec.push_back(make_pair(r, -1));
  return;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    long long l, r;
    scanf("%lld %lld", &l, &r);
    work(0, (1ll << M) - 1, l, r, M, 0);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    long long l, r;
    scanf("%lld %lld", &l, &r);
    work(0, (1ll << M) - 1, l, r, M, 1);
  }
  for (int i = 0; i <= M; i++) {
    for (int j = 0; j < x[0][i].size(); j++) {
      long long a = x[0][i][j];
      for (int k = 0; k < x[1][i].size(); k++) {
        long long b = x[1][i][k];
        Ins(a ^ b, (a ^ b) + (1ll << i));
      }
      for (int k = 0; k < y[1][i].size(); k++) {
        long long b = y[1][i][k];
        Ins(a ^ b, (a ^ b) + (1ll << i));
      }
    }
    for (int j = 0; j < x[1][i].size(); j++) {
      long long a = x[1][i][j];
      for (int k = 0; k < y[0][i].size(); k++) {
        long long b = y[0][i][k];
        Ins(a ^ b, (a ^ b) + (1ll << i));
      }
    }
  }
  sort(vec.begin(), vec.end());
  int ans = 0;
  long long l = 0, r = 0;
  int cnt = 0;
  long long last = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (cnt > 0) {
      ans = (ans + Sum(last, vec[i].first)) % Mod;
    }
    cnt += vec[i].second;
    last = vec[i].first;
  }
  printf("%d\n", ans);
  return 0;
}
