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 int LIM = 60;
long long sum(long long l, long long r) {
  long long sum1 = l + r;
  long long sum2 = r - l + 1;
  if (sum1 % 2 == 0) {
    sum1 /= 2;
  } else {
    sum2 /= 2;
  }
  sum1 %= mod, sum2 %= mod;
  return (sum1 * sum2) % mod;
}
struct State {
  long long path;
  long long vid;
  long long l, r;
  State() {}
  State(long long p, long long v, long long l, long long r)
      : path(p), vid(v), l(l), r(r) {}
  long long len() { return r - l + 1; }
  pair<long long, pair<long long, long long> > get_len(long long targetlen) {
    State cur(path, vid, l, r);
    while (cur.len() != targetlen) {
      cur = cur.parent();
    }
    return make_pair(cur.path, make_pair(cur.l, cur.r));
  }
  State parent() {
    long long nvid = vid / 2;
    long long npath = path / 2;
    long long clen = len();
    if (vid % 2 == 0) {
      return State(npath, nvid, l, r + clen);
    } else {
      return State(npath, nvid, l - clen, r);
    }
  }
  int dep() {
    long long clen = len();
    int ret = 0;
    while (clen < (1LL << LIM)) {
      ++ret;
      clen *= 2;
    }
    return ret;
  }
  pair<long long, long long> merge(long long nl, long long nr,
                                   long long npath) {
    long long merged = path ^ (npath);
    long long extra = merged << (LIM - dep());
    return make_pair(extra, extra + len() - 1);
  }
};
vector<State> current;
void fill_state(long long v, long long l, long long r, long long path,
                long long tl, long long tr) {
  if (l == tl && r == tr) {
    current.push_back(State(path, v, l, r));
    return;
  }
  long long mid = (l + r) / 2;
  if (tl > mid) {
    fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, tl, tr);
  } else if (tr <= mid) {
    fill_state(2 * v, l, mid, path * 2, tl, tr);
  } else {
    fill_state(2 * v, l, mid, path * 2, tl, mid);
    fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, mid + 1, tr);
  }
}
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
  return a.second < b.second;
}
vector<pair<long long, long long> > ret;
void merge(vector<State>& a, vector<State>& b) {
  for (long long i = LIM; i >= 0; --i) {
    vector<State> cur;
    for (auto& s : a) {
      if (s.len() == (1LL << i)) {
        cur.push_back(s);
      }
    }
    set<pair<long long, pair<long long, long long> > > st;
    for (auto& s : b) {
      if (s.len() > (1LL << i)) {
        continue;
      }
      auto v = s.get_len(1LL << i);
      st.insert(v);
    }
    for (auto& s : cur) {
      for (auto& p : st) {
        ret.push_back(s.merge(p.second.first, p.second.second, p.first));
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  current.clear();
  int x1 = -1, y1 = -1;
  for (int i = 0; i < n; ++i) {
    long long x, y;
    cin >> x >> y;
    x1 = x, y1 = y;
    fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
  }
  vector<State> a = current;
  current.clear();
  int m;
  cin >> m;
  int x2 = -1, y2 = -1;
  for (int i = 0; i < m; ++i) {
    long long x, y;
    cin >> x >> y;
    x2 = x, y2 = y;
    fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
  }
  vector<State> b = current;
  merge(a, b);
  merge(b, a);
  ret.reserve(5000000);
  sort(ret.begin(), ret.end());
  long long res = 0;
  long long cl = ret[0].first;
  long long cr = ret[0].second;
  for (int i = 1; i < ret.size(); ++i) {
    if (ret[i].first > cr) {
      res += sum(cl, cr);
      res %= mod;
      cl = ret[i].first;
      cr = ret[i].second;
    } else {
      cr = max(cr, ret[i].second);
    }
  }
  res += sum(cl, cr);
  res %= mod;
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  t = 1;
  for (int tt = 0; tt < t; ++tt) {
    solve();
  }
  return 0;
}
