You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.

For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.

Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.

The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.

Input

The first line contains a single integer n_A (1 ≤ n_A ≤ 100).

The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.

The next line contains a single integer n_B (1 ≤ n_B ≤ 100).

The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.

Note that segments in both sets may intersect.

Output

Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.

Examples

Input


2
3 5
5 8
3
1 2
1 9
2 9


Output


112


Input


1
1 9
2
2 4
2 10


Output


120

Note

In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = mod * mod;
const long double eps = 1e-6;
const long double pi = acos(-1.0);
vector<pair<long long, long long>> resol(long long l, long long r) {
  vector<pair<long long, long long>> res;
  for (int i = 0; i < 60; i++) {
    long long k = (long long)1 << i;
    if (l & k) {
      long long s = l + k;
      if (s - 1 <= r) {
        res.push_back({l, i});
        l = s;
      } else {
        break;
      }
    }
  }
  if (l > r) return res;
  for (int i = 60 - 1; i >= 0; i--) {
    long long k = (long long)1 << i;
    long long s = l + k;
    if (s - 1 <= r) {
      res.push_back({l, i});
      l = s;
    }
    if (l > r) break;
  }
  return res;
}
long long calc(long long x) {
  x %= mod;
  long long res = x * (x + 1) / 2;
  return res % mod;
}
long long calc(long long x, long long y) {
  long long res = calc(y) - calc(x - 1);
  if (res < 0) res += mod;
  return res;
}
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.second != b.second) return a.second > b.second;
  return a.first < b.first;
}
void solve() {
  int n, m;
  cin >> n;
  vector<vector<pair<long long, long long>>> ra(n);
  vector<pair<long long, long long>> pa(n);
  for (int i = 0; i < n; i++) {
    long long l, r;
    cin >> l >> r;
    ra[i] = resol(l, r);
    sort(ra[i].begin(), ra[i].end(), comp);
    pa[i] = {l, r};
  }
  cin >> m;
  vector<vector<pair<long long, long long>>> rb(m);
  vector<pair<long long, long long>> pb(m);
  for (int i = 0; i < m; i++) {
    long long l, r;
    cin >> l >> r;
    rb[i] = resol(l, r);
    sort(rb[i].begin(), rb[i].end(), comp);
    pb[i] = {l, r};
  }
  vector<pair<long long, long long>> allsegs;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      vector<pair<long long, short>> segs;
      for (int k = 0; k < ra[i].size(); k++) {
        int ma = ra[i][k].second;
        long long z = (long long)1 << ma;
        long long za = z * (ra[i][k].first / z);
        long long zb = z * (pb[j].first / z);
        long long zz = za ^ zb;
        segs.push_back({zz, ma});
        zb = z * (pb[j].second / z);
        zz = za ^ zb;
        segs.push_back({zz, ma});
      }
      for (int k = 0; k < rb[j].size(); k++) {
        int ma = rb[j][k].second;
        long long z = (long long)1 << ma;
        long long za = z * (rb[j][k].first / z);
        long long zb = z * (pa[i].first / z);
        long long zz = za ^ zb;
        segs.push_back({zz, ma});
        zb = z * (pa[i].second / z);
        zz = za ^ zb;
        segs.push_back({zz, ma});
      }
      int le = 0;
      for (int k = 0; k < ra[i].size(); k++) {
        int ma = ra[i][k].second;
        while (le < rb[j].size() && rb[j][le].second > ma) {
          le++;
        }
        long long z = (long long)1 << ma;
        long long za = z * (ra[i][k].first / z);
        if (le < rb[j].size() && rb[j][le].second == ma) {
          long long zb = z * (rb[j][le].first / z);
          long long zz = za ^ zb;
          segs.push_back({zz, ma});
        }
        if (le + 1 < rb[j].size() && rb[j][le + 1].second == ma) {
          long long zb = z * (rb[j][le + 1].first / z);
          long long zz = za ^ zb;
          segs.push_back({zz, ma});
        }
      }
      sort(segs.begin(), segs.end());
      long long l = 0, r = 0;
      long long ans = 0;
      for (int g = 0; g < segs.size(); g++) {
        long long k = (long long)1 << segs[g].second;
        long long le = segs[g].first;
        long long ri = le + k - 1;
        if (r >= le) {
          r = max(r, ri);
        } else {
          allsegs.push_back({l, r});
          l = le, r = ri;
        }
      }
      allsegs.push_back({l, r});
    }
  }
  sort(allsegs.begin(), allsegs.end());
  long long l = 0, r = 0;
  long long ans = 0;
  for (int i = 0; i < allsegs.size(); i++) {
    long long le = allsegs[i].first;
    long long ri = allsegs[i].second;
    if (r >= le) {
      r = max(r, ri);
    } else {
      ans += calc(l, r);
      ans %= mod;
      l = le, r = ri;
    }
  }
  ans += calc(l, r);
  ans %= mod;
  cout << ans << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
