#include <bits/stdc++.h>
const long long inf = 1ll << 60;
const long long maxn = 30010;
using namespace std;
struct lsg {
  long long x, y;
} a[maxn], b[maxn];
signed n, q, ffa[maxn], ffb[maxn], ban[maxn], x, y;
long long f[maxn], w1[maxn], w2[maxn], w3[maxn];
bool pd(lsg a, lsg b) { return a.x < b.x; }
inline void doit(long long i) {
  w1[i] = w2[i] = w3[i] = -inf;
  if (i >= 1 && ban[i] != i) w1[i] = a[i].x * b[i].x;
  if (i >= 2 && ban[i] != i - 1 && ban[i - 1] != i)
    w2[i] = a[i].x * b[i - 1].x + a[i - 1].x * b[i].x;
  if (i >= 3) {
    if (ban[i] != i - 2 && ban[i - 1] != i && ban[i - 2] != i - 1)
      w3[i] =
          a[i].x * b[i - 2].x + a[i - 1].x * b[i].x + a[i - 2].x * b[i - 1].x;
    if (ban[i] != i - 1 && ban[i - 1] != i - 2 && ban[i - 2] != i)
      w3[i] = max(w3[i], a[i].x * b[i - 1].x + a[i - 1].x * b[i - 2].x +
                             a[i - 2].x * b[i].x);
  }
}
inline signed read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  signed k = 1, kk = 0;
  if (c == '-')
    k = -1;
  else
    kk = c - '0';
  c = getchar();
  while (c >= '0' && c <= '9') kk = kk * 10 + c - '0', c = getchar();
  return k * kk;
}
inline long long max(long long x, long long y) { return x > y ? x : y; }
inline long long min(long long x, long long y) { return x < y ? x : y; }
signed main() {
  n = read();
  q = read();
  for (signed i = 1; i <= n; i++) a[i].x = read(), a[i].y = i;
  for (signed i = 1; i <= n; i++) b[i].x = read(), b[i].y = i;
  sort(a + 1, a + 1 + n, pd), sort(b + 1, b + 1 + n, pd);
  for (signed i = 1; i <= n; i++) ffa[a[i].y] = i, ffb[b[i].y] = i;
  for (signed i = 1; i <= n; i++) ban[i] = ffb[a[i].y];
  for (signed i = 1; i <= n; i++) doit(i);
  for (signed j = 1; j <= q; j++) {
    x = ffa[read()], y = ffa[read()], swap(ban[x], ban[y]);
    for (signed i = max(1, x - 5); i <= min(n, x + 5); i++) doit(i);
    for (signed i = max(1, y - 5); i <= min(n, y + 5); i++) doit(i);
    f[0] = 0;
    for (signed i = 1; i <= n; i++) {
      if (i >= 1) f[i] = f[i - 1] + w1[i];
      if (i >= 2) f[i] = max(f[i], f[i - 2] + w2[i]);
      if (i >= 3) f[i] = max(f[i], f[i - 3] + w3[i]);
    }
    cout << f[n] << endl;
  }
}
