#include <bits/stdc++.h>
using namespace std;
const int maxn = 30005;
struct jz {
  long long x;
  int id;
  bool operator<(const jz &b) const { return x < b.x; }
} a[maxn], b[maxn];
struct mt {
  long long x[3][3];
  void cl() { memset(x, 192, sizeof(x)); }
  mt operator*(const mt &b) const {
    mt c;
    c.cl();
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++)
          c.x[i][j] = max(c.x[i][j], x[i][k] + b.x[k][j]);
    return c;
  }
} t[maxn * 4];
int n, q, ha[maxn], hb[maxn], nt[maxn];
mt work(int i) {
  mt c;
  c.cl();
  c.x[1][0] = c.x[2][1] = 0;
  if (i >= 1)
    if (i != nt[i]) c.x[2][2] = a[i].x * b[i].x;
  if (i >= 2)
    if (i - 1 != nt[i] && i != nt[i - 1])
      c.x[1][2] = a[i - 1].x * b[i].x + a[i].x * b[i - 1].x;
  if (i >= 3) {
    if (nt[i] != i - 1 && nt[i - 1] != i - 2 && nt[i - 2] != i)
      c.x[0][2] = max(c.x[0][2], a[i].x * b[i - 1].x + a[i - 1].x * b[i - 2].x +
                                     a[i - 2].x * b[i].x);
    if (nt[i] != i - 2 && nt[i - 1] != i && nt[i - 2] != i - 1)
      c.x[0][2] = max(c.x[0][2], a[i].x * b[i - 2].x + a[i - 1].x * b[i].x +
                                     a[i - 2].x * b[i - 1].x);
  }
  return c;
}
void up(int x, int L, int R, int y) {
  if (L == R) {
    t[x] = work(L);
    return;
  }
  int mid = L + (R - L >> 1);
  if (y <= mid)
    up(x * 2, L, mid, y);
  else
    up(x * 2 + 1, mid + 1, R, y);
  t[x] = t[x * 2] * t[x * 2 + 1];
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x), a[i].id = i;
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i].x), b[i].id = i;
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) ha[a[i].id] = i, hb[b[i].id] = i;
  for (int i = 1; i <= n; i++) nt[i] = hb[a[i].id];
  for (int i = 1; i <= n; i++) up(1, 1, n, i);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = ha[x];
    y = ha[y];
    swap(nt[x], nt[y]);
    for (int i = max(1, x - 2); i <= min(n, x + 2); i++) up(1, 1, n, i);
    for (int i = max(1, y - 2); i <= min(n, y + 2); i++) up(1, 1, n, i);
    printf("%lld\n", t[1].x[2][2]);
  }
  return 0;
}
